+ 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") }
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.
+ 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 )
+ 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"?