+ 3
Could someone explain me how this code works line to line? Thanks.
function func(str){ var len=str.length; for(var i=0;i<Math.floor(len/2);i++){ if (str[i]!==str[len-1-i]) return false; } return true; }
3 Réponses
+ 1
I am asking only if I can't understand how exactly the code works. I am still begginer in Javascript :-).
+ 3
4th time you are asking approx samiler (Code changed) question.
0
That function check if a string is palindrome (a string is palindrome if its equal if readed to contrary like "baninab")
Now line by line:
- get string length
- for any n-th element (character) check if its equals to last nth element, while half string is checked
- if they are different return false (string is not palidndrome)
- if function has not returned from loop mean that string is palindrome then return true