hi guys, i don’t understand why both can’t work
in writing 1 using temp
using temp and change the first and the last element,
but why it didn’t reverse?
in writing 2 after googling, still don’t know why
thank you for your help~
##writing 1
function solution(str){
var strArr=str.split("");
var temp;
for(i=0;i<str.length;i++)
{temp=strArr[i];
strArr[i]=strArr[str.length-i];
strArr[str.length-i]=temp;
}
return strArr.join("");
}
##writing 2
function solution(str){
var s=;
for(var i=1;i==str.length;i++){
s+=str.charAt(str.length-i);
}
return s;
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.