Else if in javaScript

Can anyone see the error here? I´ve written as asked but keep giving me an error…

// what is asked

On the editor to your right you find a variable named charmanderLevel, to which a value between 1 and 100 will be assigned.

Using else if statements print to the console which evolution of Charmander corresponds to that experience level. Consider an else statement if the experience level ever go above 100 that should print ‘Charizard is as good as it gets’.

Here’s a chart with the evolution which corresponds to each level:

Charmander - 1 to 15
Charmeleon - 16 to 35
Charizard - 36 to 100

// my code

var charmanderLevel = Math.ceil(Math.random() * 100);

if(charmanderLevel ===1 <=15){
console.log(‘Charmander’);
} else if(charmanderLevel ===16 <= 35){
console.log(‘Charmeleon’);
} else if(charmanderLevel ===36 <= 100){
console.log(‘Charizard’);
}else{
console.log(‘Charizard is as good as it gets’);
}

// the error

Output

Code is incorrect

You should print the string “Charmeleon” in if block that evaluates between the values 16 and 35

Charizard is as good as it gets

This is not the correct way to write the conditional expression in the if statement for what you are trying to do. What you are really saying here is:

if(charmanderLevel ===(16 <= 35))

Notice the parens around 16 <= 35. That will evaluate to true and thus your if statement really says:

if (charmanderLevel === true)

which is not what you want. All of your if statements suffer from this issue, so you’ll need to rewrite them so they do what you really want them to do.

Give it a try and if you get stuck you can show us what you have tried and we can offer advice.

P.S. If you paste new code in here please wrap it in triple backticks so it formats nicely. Or use the </> above the editor window.

2 Likes

if (charmanderLevel ===(1 <=15)){
    console.log('Charmander');
} else if (charmanderLevel ===(16 <= 35)){
    console.log('Charmeleon');
} else if (charmanderLevel ===(36 <= 100)){
    console.log('Charizard');
}else{
    console.log('Charizard is as good as it gets');
}```

still output the same error....

This is still not a valid test. I would look at the freeCodeCamp lessons on logical operators.

Those lessons start here:

1 Like

you have exactly the same issue you had before

do you understand what the issue is?

clearly not :sweat_smile: :sweat_smile:
of all the examples I saw and given what is requested, I must do 3 steps ,if else if ,else if and a last else, which will make the sentence corresponding to the correct number written and I can’t understand what am I doing wrong…

I apologize for my lack of understanding and thank you for your help!

Have you tried the freeCodeCamp lessons on this topic? You seem to have some missing knowledge about if statements.

Hello,
In hope that I understand the issue you’re facing. Are you trying to display for example ‘Charmander’ when value of charmanderLevel is 1 or 15 or any number between them? If that’s the case, then you’ll need to revise your if statement. The condition for your if statement should consider when charmanderLevel is 1 or 15 or any number between them. The same can be repeated for the other evolutions.
I hope this helps.

Tks for the help I´m gonna rewrite my code!

I´ve rewrite but still get an error…


if(charmanderLevel >= 1 && charmanderLevel < 16){
    console.log('Charmander');
} else if( charmanderLevel >= 17 && charmanderLevel < 35){
    console.log('Charmeleon');
} else if(charmanderLevel >= 36 && charmanderLevel < 100){
    console.log('Charizard');
} else {
    console.log('Charizard is as good as it gets');
}


>>>>Code is incorrect
There should be one if statement checking if the variable charmanderLevel is between 16 and 35 and in the correct order.

I try to change the else if to (x>=17 && x <35) but still doesn't work....any lights? please
Tks

Nevermind, its just the 100(101 is the cherry)!

Tks

You mentioned that chart is this:

Charmander - 1 to 15
Charmeleon - 16 to 35
Charizard - 36 to 100

So, the code would go as follows:

if (charmanderLevel >= 1 && charmanderLevel <= 15) {
    console.log('Charmander');
} else if (charmanderLevel >= 16 && charmanderLevel <= 35) {
    console.log('Charmeleon');
} else if (charmanderLevel >= 36 && charmanderLevel <= 100) {
    console.log('Charizard');
} else {
    console.log('Charizard is as good as it gets');
}

In your code, the first if will check for 1 to 15. Second if checks for 17 to 34. So, you’re missing check for level 16, 35 & 100. Try to use <= it’s better for understanding and navigating the code.

1 Like

So now that you have it working you might think about making it a little more concise. Do you really need both of the tests in every if statement? In the first if statement you are testing if a number is between 1 and 15. If it is not then you move on to the next if statement. Don’t you already know that the number is at least 16 since it wasn’t between 1 and 15?

1 Like

ahhh nice tip, tks!
cleaner is better!!!