0

For my JavaScript Course i am stuck on a question.

The question is:

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

The code i've build

var charmanderLevel = Math.ceil(Math.random() * 100);
if (charmanderLevel =>1) {
    console.log('Charmander');
} else if (charmanderLevel <=35) {
    console.log('Charmeleon');
} else if (charmanderLevel <=100) {
    console.log('Charizard');
} else {
    console.log('Charizard is as good as it gets');
}

With this code i get the warning:

Code is incorrect There should be one if statement checking if the variable charmanderLevel is greater than 1 and lesser than 15

I've tried to change my code to a value of <=15 but that gives the same warning. Also tried to make something to use >=1 and <=15 but that doesn't solve it either.

What goes wrong here and how to solve this?

Touheed Khan
  • 2,149
  • 16
  • 26
Richard
  • 11
  • 1
  • 2
    It should be `charmanderLevel >= 1`, `=>` is for creating an arrow function – Hao Wu Nov 19 '19 at 09:10
  • ```charmanderLevel >= && charmanderLevel <= ``` in all if block should work. – Shail_bee Nov 19 '19 at 09:13
  • Did you try just <15 instead of <=15 – pavan kumar Nov 19 '19 at 09:13
  • And also as @Hao Wu noticed an equals sign(=) must always have a comparator sign(< or >) before it – pavan kumar Nov 19 '19 at 09:16
  • A point to note for you would also be to make sure to not create where the if statement is always true and therefor the else statements will never be checked for example if you get the number 99 with your corrected function you'd get the output "Charmander" because it's greater than 1 and the other if's would therefor never be checked – tung Nov 19 '19 at 09:45

4 Answers4

0

I guess thats correct

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

You could check only the upper bound, because the lower bound is one, as the description shows.

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

if (charmanderLevel <= 15) {
    console.log('Charmander');
} else if (charmanderLevel <= 35) {
    console.log('Charmeleon');
} else if (charmanderLevel <= 100) {
    console.log('Charizard');
} else {
    console.log('Charizard is as good as it gets');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

As the error says lesser than 15 not less than or equal to, I think it should be if(charLvl >= 1 && charLvl <15) Try this and let me know

pavan kumar
  • 823
  • 6
  • 15
0

Try this, This will work.

var charmanderLevel = Math.ceil(Math.random() * 100);
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');
}
Idexigner
  • 45
  • 8