What's wrong in this code?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's wrong in this code??

<!DOCTYPE html> <html> <head> <title>PRAC</title> </head> <body> <input type="text" id="field"> <input type="button" onclick="test()"> <p id="demo"></p> <script type="text/javascript"> function test(){ var a = document.getElementById("field").value; var b = a.split(" "); for(var i=0; i<b.length; i++){ var x[i] = 0; for(var j=0; j<b[i].length; j++){ if(b[i][j] == 'e' || b[i][j] == 'a' || b[i][j] == 'i' || b[i][j] == 'o' || b[i][j] == 'n' || b[i][j] == 'r' || b[i][j] == 't' || b[i][j] == 'l' || b[i][j] == 's' || b[i][j] == 'u'){ x[i]++; } else if(b[i][j] == 'd' || b[i][j] == 'g'){ x[i]+=2; } else if(b[i][j] == 'b' || b[i][j] == 'c' || b[i][j] == 'm' || b[i][j] == 'p'){ x[i]+=3; } else if(b[i][j] == 'f' || b[i][j] == 'h' || b[i][j] == 'w' || b[i][j] == 'y'){ x[i]+=4; } else if(b[i][j] == 'k'){ x[i]+=5; } else if(b[i][j] == 'j' || b[i][j] == 'x'){ x[i]+=8; } else{ x[i]+=10; } } } document.getElementById("demo").innerHTML = x[0]; /*if(x[0] > x[1]){ document.getElementById("demo").innerHTML = b[0]; } else{ document.getElementById("demo").innerHTML = b[1]; }*/ } </script> </body> </html>

5th Aug 2018, 7:18 AM
Ganesh Bagaria
Ganesh Bagaria - avatar
1 Answer
+ 10
1. You didn't define what is 'x'. 2. The var keyword is not required when assigning values to an element in an array (I assumed 'x' must be an array). It should be - x[i] = 0
5th Aug 2018, 7:45 AM
Nikhil
Nikhil - avatar