2

I'm trying to write this exercise from a book:

Write a program to ask yourself, using prompt, what the value of 2 + 2 is. If the answer is "4", use alert to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean.

I made this attempt:

var input = "" || 'number'
prompt ("How many is 2+2 ?", input)
if (input = 4)
  print ("Awesome !");
else if (input = 3 || input = 5)
  print ("Close !");
else if (input = 'number'
  print ("wrong number");
else if (input = 'random text')
  print ("use numbers only!")

I know it is wrong. This is I intended to do:

  • I need to determine the type of var, not just the value. I need to make var either number or string (according to typeof). Why ? For prompt imput, because below else if condition, will be based on which type was inputted.

  • I know that exercise didn't asked it, but I want make it superior.

8
  • 2
    To get you started off, you use == to test if something is equal to something else, not =. Commented Aug 22, 2013 at 20:24
  • 1
    actually, use === ;) Commented Aug 22, 2013 at 20:25
  • The prompt input is always a string. Do you mean typeof something else? Commented Aug 22, 2013 at 20:26
  • 3
    @IvanKuckir It looks a lot like me like Rapier is learning Javascript. Commented Aug 22, 2013 at 20:29
  • 2
    Go for a Switch...it's simpler Commented Aug 22, 2013 at 20:30

4 Answers 4

3

= is assignment. == is comparison.

To convert the string that prompt gives you to a number, use parseInt(input,10) - that said, JavaScript will typecast for you, so there's no real need here. You can even tell if the user entered something that isn't a number by testing isNaN(input) for your "use numbers only" result.

So something like this:

var input = parseInt(prompt("How much is 2 + 2?",""),10);
if( input == 4) alert("Awesome!");
else if( input == 3 || input == 5) alert("Almost!");
else if( input == 10) alert("No GLaDOS, we're working in Base 10 here.");
else if( input == 42) alert("That may be the answer to Life, the Universe and Everything, but it's still wrong.");
else if( isNaN(input)) alert("Use numbers only please!");
else alert("You are wrong!");
Sign up to request clarification or add additional context in comments.

Comments

3

I'd personally suggest:

var guess = parseInt(prompt('What is 2 + 2?'), 10);

switch (guess) {
    case 4:
        console.log('Well done!');
        break;
    case 3:
    case 5:
        console.log('Almost!');
        break;
    default:
        console.log('Seriously? No.');
        break;
}

JS Fiddle demo.

Or, to be more functional about it:

function answerMath (sum) {
    var actual = eval(sum),
        guess = parseInt(prompt('What is ' + sum + '?'),10);
    if (guess === actual) {
        console.log('Well done!');
    }
    else if (guess + 1 === actual || guess - 1 === actual) {
        console.log('Almost!');
    }
    else {
        console.log('Seriously? No.');
    }
}

answerMath ('2*3');

JS Fiddle demo.

Note that while eval() is the only means I could think of in this situation to evaluate the sum passed to the function as a string, I'm not entirely sure it's a good recommendation (albeit eval() has more bad press than it perhaps deserves, though it does present risks).

2 Comments

You could pull the operator out of the sum with Regex and use a switch to do the calculation.
True, but that gets messy (and quickly!) if there's more than two numbers to be operated upon.
1

In most programming languages, = is assignment, and == tests for equality. So

a = 4 assigns the number 4 to the variable a. But a == 4 checks to see if a is equal to 4.

So for your code, you'd need:

var input = "" || 'number'
prompt ("How many is 2+2 ?", input)
if (input == 4)
  print ("Awesome !");
else if (input == 3 || input == 5)
  print ("Close !");
else if (input == 'number')
  print ("wrong number");
else if (input == 'random text')
  print ("use numbers only!")

Comments

0

I'm going to build on David Thomas's answer a little, because if you wanted to make it better, you could easily turn it into a little game.

var numa = Math.round(Math.random() * (100 - 1) + 1);
var numb = Math.round(Math.random() * (100 - 1) + 1);
var answer = numa + numb;
var guess = parseInt(prompt('What is ' + numa + ' + ' + numb + '?'), 10);

switch (guess) {
    case answer:
        alert('Well done!');
        break;
    case (answer - 1):
    case (answer + 1):
        alert('Almost!');
        break;
    default:
        alert('Seriously? No.');
        break;
}

Further things you could do would be to include a timer to see how long the user took to answer the question, and ask them if they way to play again when they get it right.

Here is a Fiddle: http://jsfiddle.net/6U6eN/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.