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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Defining Array Values with Prompt

    Hello, in case you haven't guessed, I have a problem.

    I am trying to create a script that will ask the user (using window.prompt()) how many values they want put in the array, and then ask them (same method) what those values are and assigning them to the appropriate cell.

    I also don't want the values to go 2, 1, 0 as this would confuse the average user- so the prompt should say something like enter number 3 of 3, 2 of 3 etc. (This part I can do, just wanted to get you the whole picture.)

    This is what I have so far...

    Code:
    	function readNumberOfEntries() {
                // Read the number of prompts to display.
                var numberOfPhrases = new Array(Number(prompt("How many values in the array?")));
                // Display the requested prompts.
                var numberTillStop = numberOfPhrases.length;
                var arrayAddress = numberOfPhrases.length - 1;
                do {
                    numberOfPhrases[arrayAddress] = window.prompt("Please enter array number " + numberTillStop + ".")
                    var arrayAddress = arrayAddress - 1;
                    var numberTillStop = arrayAddress - 1;
                    
                } while (numberTillStop >= 0);
                return numberOfPhrases;
    	}
    However, when I run this it skips the first 3 array blocks... what's wrong?

    EDIT- Please note that I am (obviously) a beginner at programming in general, so please be patient with me. Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You can simply the code quite a lot:-

    Code:
    <script type = "text/javascript">
    
    function readNumberOfEntries() {
                // Read the number of prompts to display.
                var numberOfPhrases = new Array(Number(prompt("How many values in the array?","")));
               
    for (var i=0; i<numberOfPhrases.length; i++) {
    numberOfPhrases[i] = window.prompt("Please enter value for array number " + (i+1) + ".","")
    } 
    alert (numberOfPhrases);             
    }
    
    </script>

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    You can simply the code quite a lot:-

    Code:
    <script type = "text/javascript">
    
    function readNumberOfEntries() {
                // Read the number of prompts to display.
                var numberOfPhrases = new Array(Number(prompt("How many values in the array?","")));
               
    for (var i=0; i<numberOfPhrases.length; i++) {
    numberOfPhrases[i] = window.prompt("Please enter value for array number " + (i+1) + ".","")
    } 
    alert (numberOfPhrases);             
    }
    
    </script>
    I understand this, but this is for a intro class on JS, and the requirements are that I use a while loop in this function. (Don't ask.)
    Last edited by EchoLynx; 04-05-2008 at 01:04 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I hope I have understood what you are trying to do. That is, enter array elements in descending order.

    Code:
    <script type = "text/javascript">
    
    function readNumberOfEntries() {
    // Read the number of prompts to display.
    var numberOfPhrases = new Array(Number(prompt("How many values in the array?","")));
    // Display the requested prompts.
    var arrayAddress = numberOfPhrases.length;
    do {
    numberOfPhrases[arrayAddress-1] = window.prompt("Please enter the value of array number " + (arrayAddress-1) + ".","")
    arrayAddress = arrayAddress - 1;
    }
    while (arrayAddress > 0);
    alert (numberOfPhrases); // delete after testing
    return numberOfPhrases;
    }
    </script>
    Unlike the simple "while" loop, the do-while loop will always be executed at least once because the test expression is not encountered until the end of the loop.

    "There is no reason anyone would want a computer in their home." — Ken Olson, President/founder of Digital Equipment Corp., 1977.
    Last edited by Philip M; 04-05-2008 at 02:10 PM. Reason: Add quote

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    numberOfPhrases[arrayAddress] = window.prompt("Please enter array number " + arrayAddress + ".","")
    Yes this would work, but I am trying to make it so that the Array cells = 123 not 012 to the end user. That's why I have the extra variable.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I added to and expanded on my first response at 5.10am so we have "crossed in the ether".

    You can change this line if you want to:-

    numberOfPhrases[arrayAddress-1] = window.prompt("Please enter the value of array number " + (arrayAddress) + ".","");

    but of course the first array element is [0].
    Last edited by Philip M; 04-05-2008 at 02:21 PM.


  •  

    Posting Permissions

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