Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Location
    New York
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.
    Last edited by Philip M; 10-11-2013 at 07:52 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    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);
    alert(planet);
    </script>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by sunfighter View Post
    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);
    alert(planet);
    </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.
    Last edited by Philip M; 10-11-2013 at 05:22 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    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.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by felgall View Post
    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.
    Last edited by rnd me; 10-11-2013 at 06:10 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    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.
    Last edited by felgall; 10-11-2013 at 09:29 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by felgall View Post
    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?


  •  

    Tags for this Thread

    Posting Permissions

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