Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: I need help with this code i am learning

1. ## I need help with this code i am learning

// Get Your Astrological Zodiac Find Your Ruler Planet
// I need my code to answer depending on the user input

var zodiacNames= ["Aries","Leo","Sagittarius","Taurus","Virgo","Capricorn","Gemini","Libra","Aquarius","Cancer","Scorp io","Pisces"];
var rullerPlanet= ["Mars","Venus","Mercury","Moon","Sun","Mercury","Venus","Pluto","Jupiter","Saturn","Uranus","Neptune "];
var name = prompt("Enter your Zodiac Name");

if (!name) name = zodiacNames[0] = rullerPlanet[0];

zodiacNames[0] = rullerPlanet[0];
zodiacNames[1] = rullerPlanet[1];
zodiacNames[2] = rullerPlanet[2];
zodiacNames[3] = rullerPlanet[3];
zodiacNames[4] = rullerPlanet[4];
zodiacNames[5] = rullerPlanet[5];
zodiacNames[6] = rullerPlanet[6];
zodiacNames[7] = rullerPlanet[7];
zodiacNames[8] = rullerPlanet[8];
zodiacNames[9] = rullerPlanet[9];
zodiacNames[10] = rullerPlanet[10];
zodiacNames[11] = rullerPlanet[11];
zodiacNames[12] = rullerPlanet[12];

2. Hello,

The purpose of this question is to give you experience on working with for loops.

After accepting the users input you need to create a for loop that runs through all elements in the "zodiacNames" array. Once you find a match for what the user enters you need to simply print out the value contained at the same index in the "rullerplant" array.

Here's some information on for loops: http://www.w3schools.com/js/js_loop_for.asp

Hope this helps!

3. That code makes no sense whatsoever. The debugging prompt call will let you know that the script has run to that point but everything else it does will just be discarded - assuming you don't disable JavaScript when the prompt displays.

4. Although you have not used the word "please" which is always a good idea when seeking assistance from strangers the following ought to help you.

Code:
```<script type = "text/javascript">

var zodiacNames= ["aries","leo","sagittarius","taurus","virgo","capricorn","gemini","libra","aquarius","cancer","scorpio","pisces"];  /// Note in lower case
var rulerPlanet= ["Mars","Venus","Mercury","Moon","Sun","Mercury","Venus","Pluto","Jupiter","Saturn","Uranus","Neptune "];
var zn = prompt("Enter your Zodiac Name","");

var x = "No such Zodiac Name!";
for (var i = 0; i<zodiacNames.length; i++) {
if (zn.toLowerCase() == zodiacNames[i]) {
var x = rulerPlanet[i];
}
}
alert ("Your ruler planet is: " + x);

</script>```
promts and alerts are obsolete and as felgall implies should be used only for debugging and testing purposes.

Please do not violate the forum rules by double posting. Please read the forum rules and guidelines (incling the one about use of code tags) before posting again.

Quizmaster: What are the first two books of the Old Testament?
Contestant: Genesis and, er, Exorcist.

5. Without a loop:
Code:
```<script type="text/javascript">
var zodiacNames= ["aries","leo","sagittarius","taurus","virgo","capricorn","gemini","libra","aquarius","cancer","scorpio","pisces"];
var rullerPlanet= ["Mars","Venus","Mercury","Moon","Sun","Mercury","Venus","Pluto","Jupiter","Saturn","Uranus","Neptune "];
var name = prompt("Enter your Zodiac Name");

var planet = zodiacNames.indexOf(name);
</script>```

6. Originally Posted by sunfighter
Without a loop:
Code:
```<script type="text/javascript">
var zodiacNames= ["aries","leo","sagittarius","taurus","virgo","capricorn","gemini","libra","aquarius","cancer","scorpio","pisces"];
var rullerPlanet= ["Mars","Venus","Mercury","Moon","Sun","Mercury","Venus","Pluto","Jupiter","Saturn","Uranus","Neptune "];
var name = prompt("Enter your Zodiac Name");

var planet = zodiacNames.indexOf(name);
</script>```

Hardly what is wanted. That returns the index of the array, not the value. And returns -1 if I type Leo or whatever.

Should be
Code:
```var p = zodiacNames.indexOf(name.toLowerCase());
alert(rullerPlanet[p] || "No such zodiac sign");```
But I imagined the purpose of the exercise was to practice using loops.

7. That's what happens when you play around with something after you get it working Thanks for correction.
But I never saw the requirement for loops.

8. Originally Posted by felgall
assuming you don't disable JavaScript when the prompt displays.
why do you seem to assume that everyone is using an old copy of opera?

nobody uses an old copy of opera, and it was the only browser dumb enough to let users break the web voluntarily...
according to my analytic accounts, IE6 is more popular than the last version of opera to offer the "feature" you mention every time someone uses prompt()...

the trend seem to be going in the opposite direction; firefox has already removed the option to disable JavaScript, and chrome has made it harder than ever to get to.

9. Originally Posted by rnd me
why do you seem to assume that everyone is using an old copy of opera?

Why do you seem to assume that everyone is still using Netscape 4. Those calls were obsolete even in Internet Explorer 5.

Opera 12 it isn't an old copy of Opera. The newer versions are still incomplete and are missing most of the functionaliity that is the reason for using Opera in the first place - which is why Opera 12 doesn't upgrade to them. Opera 16 is still basically Chrome without most of the features that Chrome has - and Chrome only has some of the features of Opera 12.

Anyway you can turn off all alert, confirm and prompt debugging calls for ALL web sites in ANY modern browser. I don't have any debugging dialogs display at all when I use Internet Explorer, Chrome or Firefox or Opera 16. Opera 12 just happens to be the only browser on my computer where I allow any alert, confirm or prompt debugging dialogs to display at all.

STOP WRITING ALL YOUR JAVASCRIPT AS IF ALL YOUR VISITORS ARE USING NETSCAPE 4 (or earlier browsers). Netscape 4, alert(), confirm() and prompt() are all long dead.

10. Originally Posted by felgall
Why do you seem to assume that everyone is still using Netscape 4. Those calls were obsolete even in Internet Explorer 5.
Why do you insist on peddling this incessant mantra to rookie students, when the last thing they need at the outset is the unnecessary complication of learning more advanced display techniques?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•