I am creating a voting system webpage using Javascript. There are several things the user must input before beginning the vote, such as candidates, number of people voting, etc. I have created a function for each one, but when I attempted to add some validating if statements to my enterCandidate function, it would not register an error when the user left the box blank. Here is the function code:
var enterCandidate = prompt("Please enter a candidate");
for (let i = 0; i < enterCandidate.length; i++){
if (isNaN(enterCandidate.charAt(i)) || enterCandidate.charAt(i) == ' '){
if (!enterCandidate.length == 0){
candidates.push(enterCandidate);
console.log(enterCandidate);
candidate1 = candidates[0];
candidate2 = candidates[1];
candidate3 = candidates[2];
candidate4 = candidates[3];
candidate5 = candidates[4];
candidate6 = candidates[5];
} else {
alert('Please do not leave the box blank.');
}
} else {
alert('Please enter a name, without symbols or numbers.');
}
}
}
The part I am having an issue with is the second if statement, where I used !enterCandidate.length == 0. This should only return true if the user has inputted something but instead it returns false and moves to the else alert 'Please do not leave the box blank'. I am aware that even if this code worked, it would allow people to enter just spaces - I am not really sure how to tackle that without registering errors if people entered full names.