英文:
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 = "";
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"
答案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 = "";
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"));
<!-- 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 = "";
for (i = string.length - 1; i > -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('').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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论