【发布时间】:2019-02-20 13:46:09
【问题描述】:
我想在没有内置函数的情况下反转字符串,例如 split、reverse 和 join,我已经尝试了这里的代码 @987654321 @ ,但我不太明白第四行的代码是做什么的,我需要在第四行进行更多解释。这是我的代码和一些解释
function reverseString(str) {
reverseIt = [];
for (i = 0; i < str.length; i++) {
reverseIt = str[i] + reverseIt; // the first way that works
// reverseIt = str[i] + []; // first.. i assume the variable "reverseIt" is equal to "[]", but the result from this line is not equal to the line above
// reverseIt = str[i] + ''; // then i try this way with assume the variable reverseIt is empty string (""), but the result from this line not produce the expected result
// var testing = []; // and then i try to make an empty array variable again
// reverseIt = str[i] + testing; // and try to add the variable above, but this still not realy produce the expected result
/*
So the questions.., why the first way can works..?, what's actualy the code on that line do..?
*/
}
return reverseIt;
}
console.log(reverseString('Javascript'));
【问题讨论】:
标签: javascript arrays node.js for-loop