-1

What am I missing in this code? When I try to run it it only gives the first "else if" alert. I cant find anything I have done wrong on Google(c) so I was wondering if the experts can have a look at my code. Is there a text box I need to add?

<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>HTML5 Compliant Template</title>

  <link rel="stylesheet" href="css/style.css">

  <script>
    var scoops = 5;
    if (scoops >= 5) {
      alert("Eat faster, the ice cream is going to melt!");
    } else if (scoops == 5) {
      alert("See a Doctor Now you may have diabeties");
    } else if (scoops == 4) {
      alert("Bit greedy dont you think");
    } else if (scoops == 3) {
      alert("Leave some for the other kids");
    } else if (scoops == 2) {
      alert("Going once!");
    } else if (scoops == 1) {
      alert("Going twice!");
    } else if (scoops == 0) {
      alert("Gone!");
    } else {
      alert("Still lots of ice cream left, come and get it.")
    }
  </script>
</head>

<body>

</body>

</html>

If I am doing something wrong please alert me.

Thank you Ben Guha

Jai
  • 74,255
  • 12
  • 74
  • 103
  • 3
    What do you **expect** from this code? If you set `scoops = 5`, there is only branch to be triggered – Nico Haase Aug 06 '18 at 08:22
  • so what do I need to add –  Aug 06 '18 at 08:23
  • 3
    Only the first `if` will be triggered because `5 >= 5` is true. `When I try to run it it only gives the first "else if" alert` no your are wrong, it does not. What do you want to do ? – Hearner Aug 06 '18 at 08:24
  • The question is still open: what should your code do? Why do you think that you need a text box? Can you explain from a user's point of view (not from your developer's POV) what should happen? – Nico Haase Aug 06 '18 at 08:52

4 Answers4

1

You are putting the wrong sequence. Though sequence should be as per input/output model you are keeping in mind.

Below should be the code--

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>HTML5 Compliant Template</title>
        <link rel="stylesheet" href="css/style.css">
        <script>
            var scoops = 5;

            if (scoops == 0) {
                alert("Gone!");
            }else if (scoops == 1) {
                alert("Going twice!");
            }  else if (scoops == 2) {
                alert("Going once!");
            } else if (scoops == 3) {
                alert("Leave some for the other kids");
            }else if (scoops == 4) {
                alert("Bit greedy dont you think");
            } else if (scoops == 5) {
                alert("See a Doctor Now you may have diabeties");
            } else  if (scoops >= 5) {
                alert("Eat faster, the ice cream is going to melt!");
            } else {
                alert("Still lots of ice cream left, come and get it.")
            }
        </script>
    </head>
    <body>
    </body>
</html>

Hope it helps!

Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56
  • 1
    How does your code run different from the OP's code? – Nico Haase Aug 06 '18 at 08:50
  • 1
    In my answer I have mentioned that the mental model , "Though sequence should be as per input/output model you are keeping in mind. " ,this line. That's my thought of comparing smaller to bigger ones , it does differently cause , he is using if (scoops >= 5) and below that its if (scoops == 5), thats wrong anyways! – Krishna Satwaji Khandagale Aug 06 '18 at 08:54
0

It seems like you want a form and from the question it seems like you really need to know some basic knowledge about html you should try some tutorials like this.

Here is the code that should do what you want. Form tag creates a form and takes as attribute an function name that should be executed on submit. Input tag creates a field with type set to number indicating that the input is a number. And last a button with type set to submit indicating it will submit the form.

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>HTML5 Compliant Template</title>

  <link rel="stylesheet" href="css/style.css">

  <script>
    function validate(){
      var scoops = document.getElementById("scoops").value;
      if (scoops >= 5) {
        alert("Eat faster, the ice cream is going to melt!");
      } else if (scoops == 5) {
        alert("See a Doctor Now you may have diabeties");
      } else if (scoops == 4) {
        alert("Bit greedy dont you think");
      } else if (scoops == 3) {
        alert("Leave some for the other kids");
      } else if (scoops == 2) {
        alert("Going once!");
      } else if (scoops == 1) {
        alert("Going twice!");
      } else if (scoops == 0) {
        alert("Gone!");
      } else {
        alert("Still lots of ice cream left, come and get it.")
      }
    }
  </script>
</head>

<body>
<form onsubmit="validate()">
<span>Enter no. of scoops here</span>
<input id="scoops" type="number">
<button type="submit">
Submit
</button>
</form>


</body>

</html>
-2

html

<input id='inputElement' type='number' onchange="updateInput()">

Javascript

var scoops=document.getElementById("inputElement").value // here you can use your if else logic

Sujay
  • 613
  • 1
  • 5
  • 16
-2

try this

 <script>
    var scoops = 5;
    if (scoops > 5) { //this must be greater than, **not greater than or equal**
      alert("Eat faster, the ice cream is going to melt!");
    } else if (scoops == 5) {
      alert("See a Doctor Now you may have diabeties");
    } else if (scoops == 4) {
      alert("Bit greedy dont you think");
    } else if (scoops == 3) {
      alert("Leave some for the other kids");
    } else if (scoops == 2) {
      alert("Going once!");
    } else if (scoops == 1) {
      alert("Going twice!");
    } else if (scoops == 0) {
      alert("Gone!");
    } else {
      alert("Still lots of ice cream left, come and get it.")
    }
  </script>
mehmetx
  • 850
  • 6
  • 6
  • Did you test what happens if `scopes` is smaller then `5`? – t.niese Aug 06 '18 at 08:42
  • it goes on related else/else if block. if scopes is 4 (a smaller number than 5), then it shows "Bit greedy dont you think" alert message. what do you expect? – mehmetx Aug 06 '18 at 11:18