“Jak znaleźć długość ciągu w JavaScript bez użycia metody długości” Kod odpowiedzi

Jak znaleźć długość ciągu w JavaScript bez użycia metody długości

function strLength(s) {
  var length = 0;
  while (s[length] !== undefined){
    length++;
  } 
  return length;
}

console.log(strLength("Hello")); // 5
console.log(strLength("")); // 0
Ainul Sakib

Jak uzyskać długość w JS bez użycia metody długości.

// Get The Length Of The String.
function strLength(x){
  var counter = 0;
  while(x[counter] !== undefined){
    counter++;
  }
  return counter;
}

var str = prompt("Write your string ...",''); // Get String From User.
var The_Length = strLength(str); // 
var lastChar = counter - 1; // The Index Of Last Letter.
console.log(`The Length Of Your String is ${counter}`);
console.log(`The Last Char In Your String Is ${str[lastChar]}`);
Omar ELmoez

JavaScript Utwórz ciąg o danej długości

var str = new Array(len + 1).join( character );
Frantic Fox

Odpowiedzi podobne do “Jak znaleźć długość ciągu w JavaScript bez użycia metody długości”

Pytania podobne do “Jak znaleźć długość ciągu w JavaScript bez użycia metody długości”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu