Web Dev 101 - Fundamentals Part 2 Help - Snarky Robot


#1

In this lesson, there’s an external link to lesson with exercises at the end. I’m working on the final exercise and I can’t quite figure out why my code is not working. Here is the prompt:

A Snarky Robot

Write code that asks the user what their favorite number is and then responds according to the following guidelines:

  1. If the number is 42, the response should be “Yay! That’s my favorite too!”
  2. If the number is less than 42 the response should be: “Eh, that’s OK but X would be even better” for this response X should be the number that they guessed + 1
  3. If the number is higher than 42 the response should be: “LAME. That number is too large!”

This is my code:

let number = prompt('what is your favorite number');
let x = number++;

if (number = 42) {
  response = "Yay! That's my favorite too!";
} else if (number < 42) {
  response = "Eh, that's OK but " + x + " would be even better";
} else {
  "LAME. That number is too large!";
}

console.log(response);

I’ve tried a few different things but the second test (prompt response “15”) keeps failing. It responds with the first option rather than the second. What am I doing wrong?


#2

Hi @nikhilpatel87 :wave:

I reckon its this, your assigning the number variable to 42 here instead of checking if it is 42

Try this instead

if (number == 42) {

#3

I tried that but this is the error I get"

ReferenceError: response is not defined
at eval:12:13
at eval
at new Promise


#4

hmm thats odd, Can I see your updated code please?


#5
let number = prompt('what is your favorite number');
let x = number++;

if (number == 42) {
  response = "Yay! That's my favorite too!";
} else if (number < 42) {
  response = "Eh, that's OK but " + x + " would be even better";
} else {
  response = "LAME. That number is too large!";
}

console.log(response);

#6

I have also tried putting the 42 inside quotes thinking that perhaps the user response is considered a string rather than a number, but that didn’t work either


#7

I think I figured it out. When you are incrementing number by one and assigning it to x it is mutating the original number variable.

changing it to this made it work for me let x = number + 1;


#8

I appreciate the help, Kevin. I tried that before and now again, but it seems to be concatenating the second test output. In the second output test, it expects the response to be

Eh, that’s OK but 16 would be even better

Instead, the output is showing 151

This assignment is on repl.it


#9

ahh I see, ok it seems to be a combination of things. The number being a string is indeed the last remaining problem.

Converting to a number with the Number function made all the tests pass for me.

Edit: you’ll have to open it on repl.it to get the prompt to show. It doesn’t seem to allow the prompt to display on an embedded repl.it


#10

Thanks Kevin! That worked perfectly. I’m not exactly sure why the number variable had to be that way for it to work, but I’ll research it some more to gain better understanding. I appreciate your help!


#11

I know this thread is about a month old, but others may find this useful in the near future.

It needs to be let x = ++number instead of let x = number++ . The former returns the number incremented by one while the latter returns the value of number before beginning to increment. You can move let x to your else if statement as well.


#12

This seems to be a good answer.

let number = Number(prompt('What is your favorite number?'));
let x = number;

if (number == 42) {
  response = "Yay! That's my favorite too!"
} else if (number < 42) {
  response = "Eh, that's OK but " + (++x) + " would be even better"
} else {
  response = "LAME. That number is too large!"
}

console.log(response)

#13

Hi all!

I am piggybacking on this and was wondering if someone could take a look at the code I have written. I have compared it to others in this thread but for some reason mine does not want to work. The following is what I have and it only partially works:

let number = prompt(‘what is your favorite number’);
let x = ++number;

if (number == 42) {
response = ‘Yay! That’s my favorite too!’;
} else if (number < 42) {
response = ‘Eh, that’s OK but ’ + x + ’ would be even better’;
} else {
response = ‘LAME. That number is too large!’;
}

console.log(response);

That only works with 2nd and 3rd prompt. When I enter in the number 42 I get "LAME. That number is too large!"

Any ideas or tips on what I am doing wrong?

Thank you in advance!


#14

let x = ++number;

Using ++ modifies the original number even though you assign it to another variable.


#15

Thank you for responding! removing number++ has correct the original issues and then has created a new issue and that is number + 1 concatenates the numbers instead of adding them. I saw that @KevinMulhern figured out how to fix it by changing the first line to:

let number = Number(prompt(‘what is your favorite number’));

Thank you two for your help!

Best!


#16

Well, guys, if I’m not wrong, when it is prompted to enter a number for the first time, the input comes as a String and not a number. So, you have to convert it’s format to Number. If it is not converted, doing number+1 will concatenate 1 to the String, because there’s no addition in Strings, there’s concatenation.
Hence,
let number = prompt('what is your favorite number'); takes String as input and doing number+1 appends 1
while,
let number = Number(prompt('what is your favorite number')); converts the input from a String to a number and you can do normal arithmetic on it now. Hope this helps in understanding!
Cheers :smiley: :smiley: