In a switch statement, I want make sure the user did not type a number. I get an error with this:
"case !isNaN(user):"
var user = prompt("Hey! What do you like?","").toLowerCase();
switch(user){
case "":
console.log("Can\'t be blank!");
break;
case !isNaN(user):
console.log("Can\'t be a number");
break;
case "food":
console.log("Food does a body good...sometimes");
break;
default:
console.log("Mmm....Can\'t Make heads or tales of this one.");
}
I tried this too, but not working:
switch(!isNaN(user)){
case true:
console.log("Hey! Can\'t be a number!");
break;
I can get !isNaN to work in an else if, but not in a switch.
var user = prompt("Hey! What do you like?","").toLowerCase();
if(!isNaN(user)){
console.log("Can\'t be a Number!");
}
The switch statement is comparing your user variable to each case. so it has to be read like
does user == "". Yes? do stuff
does user == "food". Yes? do stuff
otherwise, do stuff
when you read a case statement like this, you'll realise why your NaN case doesn't work
does user == isNaN(user) <- will never be true
Your final code is the way you need to do it
var user = prompt("Hey! What do you like?","").toLowerCase();
if(!isNaN(user)){
console.log("Can\'t be a Number!");
}
Related
so i just learned javascript, and learning switch, so i want to know how if i input any number, the alert can know its a number, and the else is anything that not number
var thisVarA = +prompt('a?', '');
switch (thisVarA) {
case 1:
alert('you input number 1');
break
case 2:
alert('you input number 2');
break
case 3:
alert('you input number 3');
break
case 4:
alert('ok this is more than enough');
break
default:
alert('its not just a number');
}
so i want case 4 is if i put any number other than 1,2,3 the alert can know it and put ok its a number
and the else is anything than number even if its has a number ike milano1010
Instead of a switch statement, this would be better for just an if statement.
if (!isNaN(thisVarA)) {
alert('you input number ', thisVarA);
} else {
alert('its not a number');
}
Here we do several things. isNaN checks if it is not a number. We use ! before it to reverse that logic so now we check if it IS a number. The alert starts with 'you input number ' and fills in the number automatically with your variable number instead of needing to check each case. Else is anything that is not just a number like milano1010
Based on your requirements: (switch + determining whether the input is numeric):
<script>
var thisVarA = +prompt('a?', '');
switch (true) {
case thisVarA==1:
alert('you input number 1');
break
case thisVarA==2:
alert('you input number 2');
break
case thisVarA==3:
alert('you input number 3');
break
case Number.isNaN(thisVarA)==false:
alert('ok its a number');
break
default:
alert('it is anything than number');
}
</script>
The Number.isNaN() function determines whether a value is an illegal number (Not-a-Number)
There is a slight sublety in your set up as there is a + immediately in front of your prompt. Javascript takes this to be an arithmetic operation in this situation and therefore forces whatever you input to be of type 'number'. If it were not there the result of your prompt would not be forced to be of type number.
As it is, when you type something like abc123 it is forced to be a number, but it isn't one. Javascript in this situation will give it type 'number' but its value will be NaN (not a number).
Here's your switch with an alert to show you the type and under the default we check whether it's NaN or not.
var thisVarA = +prompt('a?', '');
alert('The type is ' + typeof(thisVarA));
switch (thisVarA) {
case 1:
alert('you input number 1');
break
case 2:
alert('you input number 2');
break
case 3:
alert('you input number 3');
break
default:
if (!isNaN(thisVarA)) {
alert('you input number ' + thisVarA);
}
else {
alert('its not just a number');
}
}
New to JavaScript so please forgive me if this has an obvious answer. I'm trying to get a switch statement to output a specific phrase depending on the value of an input box, however it will only output the default option. What have I done wrong? Thanks.
<input id="inputIQ" type="number"/>
<button onclick="inputIQFunction()">Submit</button>
<script>
function inputIQFunction()
{
var userinput = document.getElementById("inputIQ").value;
switch (userinput) {
case userinput <= 10:
alert("Less than 10");
break;
case userinput > 10:
alert("Greater than 10");
break;
default:
alert("Please input value");
break;
}
}
</script>
Basically, switch doesn't support conditional expressions. It just jumps to the value according to the cases.
If you put true in the switch (true) part, it'll jump to the case whose have true value.
Try like this
switch (true) {
case userinput <= 10:
alert("Less than 10");
break;
case userinput > 10:
alert("Greater than 10");
break;
default:
alert("Please input value");
break;
}
You cannot use logical conditions in your switch statement. It actually compares your userinput to a result of condition (true \ false), which never occurs.
Use conditions instead:
function inputIQFunction() {
function getIQFunctionOutput(inputValue) {
var parsedInput = parseInt(inputValue);
if (Number.isNaN(parsedInput))
return "Please, enter a correct value";
return parsedInput <= 10
? "Less or equal than 10"
: "Greater than 10";
}
var userinput = document.getElementById("inputIQ").value;
var output = getIQFunctionOutput(userinput);
alert(output);
}
<input id="inputIQ" type="number" />
<button onclick="inputIQFunction()">Submit</button>
P.S. You can actually use switch with logical statements this way:
switch (true) {
case userinput <= 10:
break;
case userinput > 10:
break;
}
but I would highly recommend not to use this approach because it makes your code harder to read and maintain.
Try like this:
<input id="inputIQ" type="number"/>
<button onclick="inputIQFunction()">Submit</button>
<script>
function inputIQFunction() {
var userinput = document.getElementById("inputIQ").value;
userinput = parseInt(userinput);
switch (true) {
case userinput <= 10:
alert("Less than 10");
break;
case userinput > 10:
alert("Greater than 10");
break;
default:
alert("Please input value");
break;
}
}
</script>
A switch works by testing the value of the expression in switch(expression) against the values of each case until it finds one that matches.
In your code, the userinput in switch(userInput) is a string, but your two case statements both have a value of either true or false. So you want to use switch(true) - that's how you get a switch to work with arbitrary conditions for each case. In context:
switch(true) {
case userinput <= 10:
alert("Less than 10");
break;
case userinput > 10:
alert("Greater than 10");
break;
default:
alert("Please input value");
break;
}
I know this is an old thread but I'm just starting out on JS (one week in) and this is the simplest thing I could create just so the logic is understood.
Switch appears to work only by true/false when using a user input value.
My script looks like:
<script>
document.getElementById("click").onclick = function () {
var day = document.getElementById("day").value;
switch (true) {
case day == 1:
document.write("Monday");
break;
case day == 2:
document.write("Tuesday");
break;
default:
document.write("Please enter valid number")
}
</script>
Like I said I'm only a week into this but I'm making a small portfolio for myself with these little things that courses may not teach, I'm open to any one wishing to offer me help learning also, hope it helps with understanding the logic.
You are not fulfilling the requirements of 'switch & case'
userinput <= 10:
It means 'true'
because '<=' is a comparison operator. It compares 'userinput' and ’10'(given value) and give you an answer in boolean(i.e. true or false).
But, here in switch case you need an integer value.
Another
You have entered this
'switch (userinput)' here 'switch' considering 'userinput' a string that should be integer,
You can fix it with this.
switch (eval(userinput))
My friend is working on a fun Javascript web application where you type something in the text box, and the computer returns you a result, but it is case-sensitive. Can he make it case-insensitive? We have tried to use:
var areEqual = string1.toUpperCase() === string2.toUpperCase();
which was on JavaScript case insensitive string comparison, but he cannot figure out how to use that.
function isValid() {
var password = document.getElementById('password').value;
if (password == "Shut Up")
{ alert('HOW ABOUT YOU!') }
else if (password == "No")
{ alert('You just did') }
}
Please, if you're going to do this, use switch! The only real difference between toLowerCase as opposed to toUpperCase here is that the values in the case lines won't be shouting at you.
function isValid() {
var password = document.getElementById('password').value;
switch (password.toLowerCase()){
case "shut up":
alert('HOW ABOUT YOU!');
break;
case "no":
alert('You just did');
break;
case "okay":
alert('Okay');
break;
// ...
case "something else":
alert('Really?');
break;
default:
alert('Type Something Else')
}
}
Just add the .toUpperCase() behind your variables:
else if (password == "No")
{alert('You just did')}
becomes:
else if (password.toUpperCase() == "No".toUpperCase())
{alert('You just did')}
or just:
else if (password.toUpperCase() == "NO") //Notice that "NO" is upper case.
{alert('You just did')}
You can use that this way, for example in the first else-if block:
else if (password.toUpperCase() == "No".toUpperCase())
{alert('You just did')}
The function toUpperCase, applied to a string, returns it's uppercased version, so for No it would be NO. If the password variable holds any lower-upper case combo of the word no, such as "No", "nO", "NO" or "no", then password.toUpperCase() will be "NO".
The previous code is equivalent to
else if (password.toUpperCase() == "NO")
{alert('You just did')}
I have problem with a variable I made (it's a string) in JavaScript. It will be prompt from the user and then with the switch I will check if it is true or not. Then when I input it upper case it will say it is identified as a another var.
Here is my code:
var grade = prompt("Please enter your class") ;
switch ( grade ){
case "firstclass" :
alert("It's 500 $")
break;
case "economic" :
alert("It's 250 $")
break;
default:
alert("Sorry we dont have it right now");
}
Just lower case it initially.
var grade = prompt("Please enter your class").toLowerCase() ;
as #nicael stated just lowercase what they input. However, if you need to preserve the way it was input and only compare using the lowercase equivalent, use this:
var grade = prompt("Please enter your class") ;
switch ( grade.toLowerCase() ){
case "firstclass" :
alert("It's $500");
break;
case "economic" :
alert("It's $250");
break;
default :
alert("Sorry we don't have it right now");
}
You could set the entire string to lower case by using the String prototype method toLowerCase() and compare the two that way.
To keep the input the same, mutate the string during your switch statement:
switch( grade.toLowerCase() ) {
// your logic here
}
You should always compare uppercase string with uppercase values in case sensitive languages.
Or lower with lower.
var grade = prompt("Please enter your class") ;
switch (grade.toUpperCase())
{
case "FIRSTCLASS" :
alert("It's 500 $")
break;
case "ECONOMIC" :
alert("It's 250 $")
break ;
default :
alert("Sorry we dont have it right now");
}
I have a switch case statement that doesn't work. I've checked the input, it's valid. If user is 1, it goes to default. If user is any number, it defaults. What's wrong here? I don't know javascript well at all.
switch (user) {
case 1:
// stuff
break;
case 2:
// more stuff
break;
default:
// this gets called
break;
}
Make sure you are not mixing strings and integers.
Try:
switch (user) {
case "1":
// stuff
break;
case "2":
// more stuff
break;
default:
// this gets called
}
Problem is data type mismatch. cast type of user to integer.
Cast type of user variable to integer
switch (+user) {
case 1: .. //
}
Javascript is type-aware. So '1' is not the same as 1. In your case the "user" has to be numeric, not the string. You can cast it by just:
user = Number(user)