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";

    }
daghene
  • 237
  • 1
  • 5
  • 13
  • Pleas add all your source code... or is this all you've got? – Damian0o Jul 26 '13 at 10:25
  • Try to open developer console in browser like firebug in firefox or developers tools in chrome... – Damian0o Jul 26 '13 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` – Roland Jansen Jul 26 '13 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. – daghene Jul 26 '13 at 10:29
  • Add some information about files structure... – Damian0o Jul 26 '13 at 10:31
  • Damiano the file structure is irrelevant i think, i just have a single empty page with an h1, an h2 and the function call on the a inside the h2. Nothing more nothing less, no other js or anything else. – daghene Jul 26 '13 at 10:33
  • @daghene it's good to show it anyways. The shorter the better, as long as it still demonstrates the issue. see http://sscce.org – John Dvorak Jul 26 '13 at 10:43

4 Answers4

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>
AngelWarrior
  • 840
  • 7
  • 12
  • Thanks for that. I actually fixed the code here and in the main post. It works but doesn't display my alert/prompts! – daghene Jul 26 '13 at 10:31
  • To get the alerts, you needed `alert('text');` because it is a function call, and functions use those parantheses. – AngelWarrior Jul 26 '13 at 10:36
  • 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). – daghene Jul 26 '13 at 10:37
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>
Damian0o
  • 653
  • 5
  • 15
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;
        }
    }
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • 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 – John Dvorak Jul 26 '13 at 10:36
  • @JanDvorak:http://stackoverflow.com/questions/5464362/javascript-using-a-condition-in-switch-case – HIRA THAKUR Jul 26 '13 at 10:37
  • you can't expect the question to have correct code. [See the top voted answer instead](http://stackoverflow.com/a/9055603/499214) – John Dvorak Jul 26 '13 at 10:38
  • `switch(true)` instead of `switch(answer)` will produce functional code. Not sure if I like that pattern, however. – John Dvorak Jul 26 '13 at 10:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34221/discussion-between-messiah-and-jan-dvorak) – HIRA THAKUR Jul 26 '13 at 10:43
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>
FuWei
  • 1