2

QUICK UPDATE!

I may have fixed it. Now the code looks like this:

<script type="text/javascript">
    function rangeAnswers() {
    var answer = prompt ("How much do you like pizza?")
    if (answer > 0 )

            confirm("That's a bit low")
            window.location="http://www.continue.com";


        if (answer > 20 )

            confirm("Quite a decent score, isn't it?")
            window.location="http://www.continue.com";

    }
</script>

And it works. At this point i think that if i put other if's at the end for <0 or > 100 i can prompt the error message and without linking to the page in that two little if's i stay on the page right?


First off let me tell you i just started with js so this may be a bit dumb.

Here's what i need to do. I have a normal HTML link with a function called on click which asks the user something like "How much do you like pizza(0 100)?".

The user types in a number and i want different answers, which will still direct you to the link anyway(so no controls to send back the user here) based on the number he wrote.

I want four major breakpoints at 0-25, 26-50, 51-75, 71-100 ELSE(lower or higher value) it just alerts("The number you specified is invalid") and stays in the page so the user can click again and put a correct value(nothing fancy then).

The thing i want when he inserts a correct value is let's say 0-25 "You don't like it very much", 26-50 "Not good but not bad" etcetera + the link.

Here's my HTML a:

<h2><a href="javascript:rangeAnswers();">Click here to tell us how much you like pizza and continue!</a></h2>

And here's the foundation of my Javascript function which is NOT loading! I tried a simple Ok/cancel prompt and it worked but i still can't get the logic of if/else statement. THIS IS THE OLD CODE, I RE-PASTED THE CORRECT ONE! function rangeAnswers() { var answer = prompt ("How much do you like pizza?") if (answer) > 0

        {
            alert= "That's a bit low"
            window.location="http://www.continue.com";
        }    else

            if (answer) > 20

        {
            alert= "You don't like it nor hate it"
            window.location="http://www.continue.com";
        } else

    }
</script>

I think that's all for now, i'm still trying to work on this while i'm waiting for replies but it's been a while now and i can't get what i'm missing.

HERE'S THE UPDATED WORKING CODE

It doesn't load my alert/prompts though.

function rangeAnswers() {
    var answer = prompt ("How much do you like pizza?")
    if (answer > 0 )

            alert= "Nice!"
            window.location="http://www.continue.com";


        if (answer > 20 )

            alert= "Nice!"
            window.location="http://www.continue.com";

    }
7
  • Pleas add all your source code... or is this all you've got? Commented Jul 26, 2013 at 10:25
  • Try to open developer console in browser like firebug in firefox or developers tools in chrome... Commented Jul 26, 2013 at 10:27
  • use the console in your browser to find and fix all syntax errors, e.g. if (answer > 0) instead of if (answer) > 0 Commented Jul 26, 2013 at 10:27
  • I've already corrected the syntax errors with the debugger because as i said i'm new to js. And yes, this is all i got. It's a test page so i only have the h1 with a title and the h2 with a link, plus the little function which is not working. Commented Jul 26, 2013 at 10:29
  • Add some information about files structure... Commented Jul 26, 2013 at 10:31

4 Answers 4

1

t isn't loading because you have a trailing else, and you had parantheses around the answers, but not around the whole Booleans. Try:

<script type="text/javascript">
    function rangeAnswers(){
        var answer = prompt ("How much do you like pizza?");
        if (answer > 0) {
            alert("Nice!");
            window.location.href="http://www.continue.com";
        }else if (answer > 20){
            alert("Nice!");
            window.location.href="http://www.continue.com";
        }
    }
</script>

EDITED - full version:

<script type="text/javascript">
    function rangeAnswers(){
        var answer, 
            bad=false;
        while(true){
            answer = prompt('How much do you like pizza?');
            if(isNaN(answer)){
                bad=true;
            }else{
                bad=false;
                answer = parseInt(answer,10);
                if(answer>100 || answer<0){
                    bad=true;
                }else if(answer===100){
                    alert('Wow, you REALLY love pizza!');
                }else if (answer >= 75) {
                    alert('75-99!');
                }else if (answer >= 50){
                    alert('50-74');
                }else if (answer >= 25){
                    alert('25-49');
                }else{
                    alert('0-24');
                }
                if(!bad){
                    window.location.href="http://www.continue.com";
                    return;
                }
            }
            if(bad){
                alert('I need a number between 0 and 100!');
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that. I actually fixed the code here and in the main post. It works but doesn't display my alert/prompts!
To get the alerts, you needed alert('text'); because it is a function call, and functions use those parantheses.
Now it works, thank you! I've updated the code to the top with one last question, which is for the incorrect values (less than zero, more than 100).
0

Your html should looks like this

<head>
    <title>Title</title>
    <script src="file.js" type="text/javascript"></script>
</head>

<body>
    <h2><a href="javascript:rangeAnswers();">Click here to tell us how much you like pizza and continue!</a></h2>

</body>

</html>

Comments

0

I think the incorrect grammar made your process not going:

<script type="text/javascript">
function rangeAnswers() {

    var answer = prompt ("How much do you like pizza?");
    answer = answer - 0;
    if(isNaN(answer))
    {
        alert("The number you specified is invalid");
    }else if (answer >= 0 && answer <=25)
    {
         alert("You don't like it very much");
         window.location="http://www.continue.com";
    }else if (answer > 25 && answer <=50)
    {
        alert("Not good but not bad");
        window.location="http://www.continue.com";
    } 
    //and so on ....
}
</script>

Comments

0

Try this in your code

 function rangeAnswers() {
        var answer = prompt ("How much do you like pizza?")       
       if (answer<=25 && answer =>0){
            alert("That's a bit low")//you are not alerting your data properly.
            window.location.href="http://www.continue.com";//use window.location.href instead of window.location
       }
        else if (answer<=50 && answer =>25){
             alert("You don't like it nor hate it")
            window.location.href="http://www.continue.com";
        }
    }

UPDATE: Other way to do the same thing would be by using the switch case:

function rangeAnswers() {
            var answer = prompt ("How much do you like pizza?") 
    switch(true){
          case answer<=25 && answer =>0:
                alert("That's a bit low")//you are not alerting your data properly.
                window.location.href="http://www.continue.com";//use window.location.href instead of window.location
           break;
           case answer<=50 && answer =>25:
                 alert("You don't like it nor hate it")
                window.location.href="http://www.continue.com";
            break;
        }
    }

5 Comments

That switch statement is incorrect. answer <= 25 && answer >= 0 only equals answer if answer is true. Your "greater than or equals" operator is incorrect as well
you can't expect the question to have correct code. See the top voted answer instead
switch(true) instead of switch(answer) will produce functional code. Not sure if I like that pattern, however.

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.