I'm facing a major issue with my website. As someone who is new to JavaScript, I am attempting to utilize an array for storing different responses based on the input provided in a text box. The template I am working with can be found here.
Although the code works perfectly without any modifications:
var answers = ["harry potter", "donald duck", "shrek", "supercoder"];
/*******************************/
//init() function
function init() {
var myButton = document.getElementById("btnSubmit");
myButton.onclick = registerName;
}
//assign init() function to onload event
onload = init;
/********************************************/
//registerName() function: it executes when user clicks the button
function registerName() {
//set up main vars: Username entered by user,
//a message string to communicate with the user,
//a reference to the paragraph used to display the message,
//and a boolean var (true/false) used as flag:
//if the registration is successful, this is set to true,
//if registration fails, this is set to false. It's initialized as false.
//Notice how you chain getElementById(), value, and toLowerCase
//to store the value entered by the user in lowercase
var newName = document.getElementById("txtName").value.toLowerCase();
var message = "";
var result = document.getElementById("result");
var success = false;
var m1 = false;
var m2 = false;
...
...
...
}
else if(newName == "a=30, b=30") {
answers.push("m5");
answers.push("m6");
answers.push("m7");
answers.push("m8");
answers.push("m9");
answers.push("s2");
}
if(success) {
answers.push(newName);
}
//display Usernames sorted alphabetically on a new line
result.innerHTML += "<br />" + answers.sort();
}
However, after integrating the necessary if-else statements to include specific strings based on the input, the code stops functioning.
I understand that there might be something glaringly obvious that I'm missing.