Why it's not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why it's not working

var s = prompt("dd"); a = s.length; var c for(i= 0,a;i>a/2;i++,a--){ if(s.length[i]!=s.length[a]){ var c=0; } else{ var c=1; } } if(c===0){ document.write("It's a pallindrome") } if(c===1){ document.write("it's not a pallindrome") }

27th Sep 2018, 5:16 AM
Jayakrishna
3 Answers
+ 6
Here's a fix: var s = prompt("dd"); var a = s.length - 1; var c = 1; for(var i = 0; i < a; i++, a--) { if (s[i] != s[a]) { c = 0; break; } } if (c === 1) { document.write("It's a palindrome"); } else { document.write("It's not a palindrome"); } I tried not to change your logic too much. Let me know if something looks confusing. I'll be happy to explain each of the modifications I made.
27th Sep 2018, 6:12 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
"var" is used everytime we introduce a new variable. "i" was never used before, so I wrote "var" to declare it and also assign it the value 0 at the same time: https://www.w3schools.com/js/js_variables.asp Skipping the "var" also works, but it is considered bad pratice. (Inside a user-defined function, not writing "var" automatically makes it a global variable, which should be avoided. You can read more about it here: https://www.w3schools.com/js/js_scope.asp )
27th Sep 2018, 12:55 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Kishalaya Saha Thanks!But I have a doubt, Why did you written var i = 0 instead of writing i=0. Seems this question is like Nursery standard, But please I didn't understand what it means. Some people will put var and some people doesn't. What's the useful with this "var"?
27th Sep 2018, 12:39 PM
Jayakrishna