Day 5: For . . . Of
-
jenishrabadiya 9 years ago Below code worked fine where I declared for loop variable with the keyword var.
for(var str of my_array){ if(str == str.split('').reverse().join('')){ console.log(str); } }
but when I use let keyword it does not work
for(let str of my_array){ if(str == str.split('').reverse().join('')){ console.log(str); } }
What is the reason?
-
monzee 9 years ago 'use strict';
is needed at the start of the enclosing block, but we don't have control over that since the code we submit is pasted in the middle of the actual script being executed by the server. Need to wrap it in a function, declare strict then call the function. Which is too much work, so just usevar
.
EDIT: I'm wrong, you can just add
'use strict';
at the top. Maybe the submission is being enclosed in a function.-
[deleted] 9 years ago Try adding "use strict" at top of your second code.
"use strict" for(let str of my_array){ if(str === str.split('').reverse().join('')){ console.log(str); } }
You can learn more about "use strict" here
-
jenishrabadiya 9 years ago Thanks!
-
karuna24s 9 years ago When I click on the link mdn says not found. Do you recommend another resource?
-
-
jenishrabadiya 9 years ago thanks! that helped me understand correctly. :)
-
-
Odiumediae 9 years ago -- edited -- I removed my comment, to avoid confusion among newbies.
-
jenishrabadiya 9 years ago I wrapped it inside the function but it did not work. we need to write "use strict" in order to make it work.
-
Odiumediae 9 years ago +1 I'm sorry, you're right, I even did that myself. I've mixed that up with something else. Thanks to DOSHI.
-
-
-
-
[deleted] 9 years ago BONUS CHALLENGE
Gratz! If you have solved this challenge.
But
Can you try solving this using
filter
andfor ... of
?-
MikeLDJ 9 years ago This can't be solved... not for the begginers.
-
wzaccone64 9 years ago compare each character at the beginning of the string against each character at the end of the string, and move the beginning and ending indexes as long as it is still a paladrum or until the indices meet each other.
for(var str of my_array){ var ldx = str.length - 1; var pala = true; var idx = 0; while ((pala) && (idx < str.length) && (idx != ldx)) if (str[idx++] != str[ldx--]) pala = false; if (pala) console.log(str); }
-
givonz 9 years ago imho, this is far more intuitive, than using the reverse function. personally, i built a reverse of the string with a concat from the end. then compare the two strings.
-
-
-
MistaTwist 9 years ago //Would something like this work?
"use strict"
function isPalindrome(str) { return str === str.split('').reverse().join(''); }
var palindromes = my_array.filter(isPalindrome);
for(let pal of palindromes){ console.log(pal); }
-
[deleted] 9 years ago Great @MistaTwist, nicely done!! :)
We can make our code more crisp, using arrow functions.
"use strict" for(let palindrome of my_array.filter(str => str === str.split('').reverse().join(''))) console.log(palindrome)
-
MistaTwist 9 years ago Ah yes, of course ;) Thanks!
-
MistaTwist 9 years ago One question though - what's the ideal use of arrow functions, with regards to reuse of functions?
Let's say I wanted to use isPalindrome elsewhere - could I just call isPalindrome(str) in the arrow function like this:
//assume my function is defined ok
for(let palindrome of my_array.filter(str => isPalindrome(str)){ console.log(palindrome); }
Thanks :)
-
jonmcclung 9 years ago This is what I did (except the "use strict", I don't know what that means) and this contest is the first experience I have with Javascript!
-
-
-
JChiquin 7 years ago One-liner solution:
console.log(my_array.filter(a=>a==a.split('').reverse().join('')).join('\n'));
-
-
varadghatage7 2 years ago 'use strict'; for(let i of my_array){ let s = i.split('').reverse().join('') if(s === i){ console.log(i); } }
-
ameyb2018 6 years ago var len = my_array.length; var res=""; var ori=""; for(let i of my_array) { ori = i; res = i.split("").reverse().join(""); if(i==res) { console.log(i); } }
-
tgamerkle 9 years ago for bonus points write it all on one line:
'use strict'; for (let i of my_array) if (i.split("").reverse().join("") == i) console.log(i);```
Sort 6 Discussions, By:
Please Log In in order to post a comment