As part of studying how to use switch, I wrote this .js script that's supposed to obtain user input, evaluate whether the user input a number, a string or mixed output, and then return the appropriate output. The prompt() command works fine but the alert() commands produce empty output. I'm not sure where the problem is.
// Request user input
var inputString = prompt("Type in a number or some words, but not both.");
// Declare variables
var outputString = ""
var switchString = ""
// Declare function that evaluates user input
var eval = function(outputString) {
for (i = 0; i < inputString.length - 1; i++) {
if (isNaN(inputString.substring(i, i + 1)) !== isNaN(inputString.substring(i + 1, i + 2))) {
switchString = "one";
return "You typed in a combination of numbers and letters! Bad person!";
}
else if (isNaN(inputString)) {
switchString = "two";
return "You typed in some words. Thanks!";
}
else {
switchString = "three";
return "You typed in a number. Thanks!";
}
}
}
// Call function
eval();
// Switch statement
switch (switchString) {
case 'one':
alert(outputString);
break;
case 'two':
alert(outputString);
break;
case 'three':
alert(outputString);
break;
default:
alert("This program is buggy and doesn't work!");
}