0

I am trying to create a what is your horoscope program in javascript and will only output the last if statement, how do I fix it so it shows the words of what it is equal to? Code linked to JSFIDDLE

<body>
<center>
    <p>To find your Horoscope click the button below!</p>
<input type='button' value='Click Me!' onclick='myHoroscope();' />

    <p id="show"></p>
</center>
 <script type="text/javascript">
    function myHoroscope () {
 confirm("In order to find your Horoscope you need to type in the month you were born correctly!")
    var horoscope = prompt("What month were you born?")
    if (age = "january")
    {
        document.getElementById('show').innerHTML = "January";
    }
    if (age = "february")
    {
        document.getElementById('show').innerHTML = "february";
    }
    if (age = "march")
    {
        document.getElementById('show').innerHTML = "march";
    }
    if (age = "april")
    {
        document.getElementById('show').innerHTML = "april";
    }
    if (age = "may")
    {
        document.getElementById('show').innerHTML = "may";
    }
    if (age = "june")
    {
        document.getElementById('show').innerHTML = "june";
    }
    if (age = "july")
    {
        document.getElementById('show').innerHTML = "july";
    }
    if (age = "august")
    {
        document.getElementById('show').innerHTML = "august";
    }
    if (age = "september")
    {
        document.getElementById('show').innerHTML = "september";
    }
    if (age = "october")
    {
        document.getElementById('show').innerHTML = "You are either the Libra 10/1 - 10/22 or Scorpio";
    }
    if (age = "november")
    { 
        document.getElementById('show').innerHTML = "The Scorpio from 11/1 -11/23 or Sagittarius";
    }
    if (age = "december")
    {
        document.getElementById('show').innerHTML = "december";
    }
        }

 </script>

</body>
baao
  • 71,625
  • 17
  • 143
  • 203
james17
  • 23
  • 2

2 Answers2

0

Here's your working fiddle

I've changed = to == as == is used for checking

I also changed var horoscope to

var age = prompt("What month were you born?")

as var horoscope isn't the one you were "checking" for.

and here the code:

<body>
<center>
    <p>To find your Horoscope click the button below!</p>
<input type='button' value='Click Me!' onclick='myHoroscope();' />

    <p id="show"></p>
</center>
</body>
<script>
        function myHoroscope () {
 confirm("In order to find your Horoscope you need to type in the month you were born correctly!")
    var age = prompt("What month were you born?")
    if (age =="january")
    {
        document.getElementById('show').innerHTML = "January";
    }
    if (age =="february")
    {
        document.getElementById('show').innerHTML = "february";
    }
    if (age =="march")
    {
        document.getElementById('show').innerHTML = "march";
    }
    if (age =="april")
    {
        document.getElementById('show').innerHTML = "april";
    }
    if (age =="may")
    {
        document.getElementById('show').innerHTML = "may";
    }
    if (age =="june")
    {
        document.getElementById('show').innerHTML = "june";
    }
    if (age =="july")
    {
        document.getElementById('show').innerHTML = "july";
    }
    if (age =="august")
    {
        document.getElementById('show').innerHTML = "august";
    }
    if (age =="september")
    {
        document.getElementById('show').innerHTML = "september";
    }
    if (age =="october")
    {
        document.getElementById('show').innerHTML = "You are either the Libra 10/1 - 10/22 or Scorpio";
    }
    if (age =="november")
    { 
        document.getElementById('show').innerHTML = "The Scorpio from 11/1 -11/23 or Sagittarius";
    }
    if (age =="december")
    {
        document.getElementById('show').innerHTML = "december";
    }
        }

</script>

SIDENOTE:

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same. From this answer:

Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
baao
  • 71,625
  • 17
  • 143
  • 203
  • Thanks Michael, how does changing the variable to age effect the outcome, changing the = to == made it so nothing showed, but when I added the var = age it affected my code. Why is that? – james17 Nov 13 '14 at 03:00
  • Because you were assigning the value from your prompt to horoscope, but there was no check for horoscope within your if . The only reason you had an output (december) earlier, was that you assigned age within the if s. You can see this here: http://jsfiddle.net/8q7tch74/5/ -- I've just altered march to only = you will now allways get march as result shown – baao Nov 13 '14 at 03:02
0

See http://jsfiddle.net/8q7tch74/4/

if(age === "january")

Changes:

1.) = assigns a value == checks for equality of values after type casting the operands === does a strict equality check

2.) you were using horoscope variable in one place, and age in another, fixed it.

Aravind
  • 3,169
  • 3
  • 23
  • 37