使用for循环连接字符串会导致它以’undefined’开头吗?(JavaScript)

huangapple go评论53阅读模式
英文:

Concatenating string using for loop makes it begin with 'undefined'? (javascript)

问题

Where is undefined coming from? I thought I initialised the string correctly. (tried using debugger but can't figure out the issue)

I've tried using concat() method. I've tried +=. Everything results in "undefined"

Why?

const reverseString = function(string) {
    let newWord = "";
    for (i = string.length; i > -1; i--) {
        newWord = newWord.concat(string[i]);
    }
    return newWord
};

Edit: in case it's unclear when running console.log(reverseString("Hello"))

expected output would be "olleH"

actual output is "undefinedolleH"

英文:

Where is undefined coming from? I thought I initialised the string correctly. (tried using debugger but can't figure out the issue)

I've tried using concat() method. I've tried +=. Everything results in "undefined<reversed string>"

Why?

const reverseString = function(string) {
    let newWord = &quot;&quot;;
    for (i = string.length; i &gt; -1; i--) {
        newWord = newWord.concat(string[i]);
    }
    return newWord
};

Edit: in case it's unclear when running console.log(reverseString(&quot;Hello&quot;))

expected output would be "olleH"

actual output is "undefinedolleH"

答案1

得分: 1

如@VLAZ在评论中提到的,您的string.length大于字符串的实际长度,这就是它超出数组的原因。

有多种方法可以消除字符串上的undefined。其中一种是VLAZ提到的,即将长度减1。另一种是在拼接时使用Nullish Coalescing运算符。以下是示例:

const reverseString1 = function(string) {
    let newWord = "";
    for (i = string.length - 1; i > -1; i--) {
        newWord = newWord.concat(string[i]);
    }
    return newWord
};

const reverseString2 = function(string) {
    let newWord = "";
    for (i = string.length; i > -1; i--) {
        newWord = newWord.concat(string[i] ?? "");
    }
    return newWord
};

console.log(reverseString1("Hello1"));
console.log(reverseString2("Hello2"));

希望这对您有所帮助。

英文:

As @VLAZ mentioned in the comment, your string.length is more than the actual length of the string that's why it bounces out of the array.

There are ways you can get rid of the undefined on your string. One is what VLAZ said which is just to -1 the length. The other one is using a Nullish Coalescing Operator on your concatenation. Samples below

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const reverseString1 = function(string) {
    let newWord = &quot;&quot;;
    for (i = string.length - 1; i &gt; -1; i--) {
        newWord = newWord.concat(string[i]);
    }
    return newWord
};

const reverseString2 = function(string) {
    let newWord = &quot;&quot;;
    for (i = string.length; i &gt; -1; i--) {
        newWord = newWord.concat(string[i] ?? &quot;&quot;);
    }
    return newWord
};

console.log(reverseString1(&quot;Hello1&quot;));
console.log(reverseString2(&quot;Hello2&quot;));

<!-- end snippet -->

答案2

得分: 0

Index should start with (n-1), not from n

const reverseString = function(string) {
let newWord = "";
for (i = string.length - 1; i > -1; i--) {
newWord = newWord.concat(string[i]);
}
return newWord
};

英文:

Index should start with (n-1), not from n

const reverseString = function(string) {
    let newWord = &quot;&quot;;
    for (i = string.length - 1; i &gt; -1; i--) {
        newWord = newWord.concat(string[i]);
    }
    return newWord
};

答案3

得分: 0

const reverseString = function (string) {
return string.split('').reverse().join(''));
};
console.log(reverseString("abc")); // "cba"
String (and Array) Methods are a great way to simplify this task.
Check out String.prototype.includes(), String.prototype.replace(), String.prototype.split() and more on MDN.

英文:
const reverseString = function (string){
return string.split(&#39;&#39;).reverse().join(&#39;&#39;))};
console.log(reverseString(&quot;abc&quot;)); // &quot;cba&quot;

String (and Array) Methods are a great way to simplify this task.
Check out String.prototype.includes(), String.prototype.replace(), String.prototype.split() and more on MDN.

huangapple
  • 本文由 发表于 2023年5月10日 12:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定