+ 1

Function Problem HEEEEEELLLLLLLLLPPPPPPPP

It says Answer isn't defined, though it shouldn't because im assigning it a value JS: function consoleFunc() { var a; var b; var c; var d; var e; var Answer; function Answer() { var Answer = document.getElementById("Input"); } var Console = document.getElementById("Console"); Console.innerHTML="I shall read your mind! (Answer 'Ok')"; if(Answer == "Ok") { Console.innerHTML="Good" } } HTML: <!DOCTYPE html> <html> <

6th Jul 2017, 8:18 PM
Joshua
Joshua - avatar
5 odpowiedzi
+ 5
you declare the variable 'answer' twice. in your function Answer () remove the keyword var to set the value of answer declared in consoleFunc (). And overthink your variable names. a function with the same name of a variable isn't good practice. https://www.w3schools.com/js/js_variables.asp
6th Jul 2017, 8:23 PM
Bumpety
+ 2
Yeah i think you want the value of the input field. at the moment you just store the input tag in the answer variable. write console.log (answer); Then you will see the stored value of answer in the console of your browser (very very useful). and if you want that the answer variable store the user input, try document.getElementById ('input').value ;)
6th Jul 2017, 8:48 PM
Bumpety
+ 1
@Bumpety Thanks, I fixed it but it still says theres a problem
6th Jul 2017, 8:42 PM
Joshua
Joshua - avatar
+ 1
@Bumpety I did both, It still says It is not defined and wont run
6th Jul 2017, 10:15 PM
Joshua
Joshua - avatar
0
Hey here is the right implementation. You have to execute the function and change the name from the function or the variable Answer because of naming conflicts. <!-- an example close to your code --> <html> <head> </head> <body> <input id="Input" type="text"> <output id="Console"></output> <button onclick="consoleFunc()">start</button> <script> function consoleFunc() { var a, b, c, d, e, answer; function Answer() { answer = document.getElementById("Input").value; console.log(answer); } Answer(); var Console = document.getElementById("Console"); Console.innerHTML="I shall read your mind! (Answer 'Ok')"; if(answer == "Ok") { Console.innerHTML="Good" } } </script> </body> </html> <!-- Here is a implementation by myself. Little bit smaller. --> <html> <head> </head> <body> <input id="Input" type="text"> <output id="Console"></output> <button onclick="consoleFunc()">start</button> <script> function consoleFunc() { var output; if (document.getElementById("Input").value === "Ok") { output = "Good" } else { output = "I shall read your mind! (Answer 'Ok')"; } document.getElementById('Console').innerHTML = output; } </script> </body> </html>
6th Jul 2017, 11:00 PM
Bumpety