www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to store prompt inputs in an array and then print that data out

  1. #1
    Join Date
    Jul 2014
    Posts
    2

    How to store prompt inputs in an array and then print that data out

    Hi everyone,
    I'm only a beginner in javascript, and this is a very easy thing to do yet I can't figure it out myself..

    As long as the user gives a positive number, we ask the user to give another number untill they give a negative number.
    As long as the user gives a positive number, this data should be stored into an array untill the user gives a negative number. When the user gives in a negative number, the stored data should be print out.

    Everything works but I can't print out the stored data from the array..

    This is my code so far:

    <!-- Schrijf een functie die naar een getal blijft vragen zolang het opgegeven getal positief is.
    Tel de rij positieve getallen op als een negatief getal wordt opgegeven. -->
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>oefening2</title>
    </head>
    <body>
    <script type="text/javascript">

    function myFunction () {

    var userAnswer = prompt("Geef een getal in!")
    var userArray = new Array();

    if (userAnswer > 0){
    myFunction();
    userAnswer = userArray++;
    }

    else {
    if (userAnswer > 0) {
    myFunction();
    userAnswer = userArray++;
    }

    else {
    document.write(userArray);
    }
    }
    }

    myFunction();
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    This smells of homework...
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    May 2014
    Posts
    936
    What exactly are you expecting this:

    Code:
    userAnswer = userArray++;
    To do? ++ is an integer operation to increment a number. It has NOTHING to do with arrays whatsoever! You could use it to increment the index into an array, but that would be a separate variable.

    I THINK what you are trying to do should be:

    Code:
    userArray.push(userAnswer);
    which puts the value onto the end of the array as a new record.

    Also, this:
    Code:
    if (userAnswer > 0){
     myFunction();
     userAnswer = userArray++;
     }
    
     else {
     if (userAnswer > 0) {
    That second if makes no sense, you already tested for zero... it would never fire as an ELSE. Take out the code in the middle and you can see how silly it is...

    Code:
    if (userAnswer > 0){
     } else {
     if (userAnswer > 0) {
    See?

    Also, if you're just going to run it, instead of making "myfunction" use an anonymous function instead. Also, you don't have anything in there to actually make things loop to keep asking. Also, document.write of an array 'blind' like that is probably going to look pretty ugly, you'd want to at least throw some line-breaks in there by iterating through the array.

    Code:
    (function() {
    
    	var results = [], answer, i;
    		
    	while ((answer = prompt('Geef een getal in!')) > 0) results.push(answer);
    	
    	for (i = 0; i < results.length; i++) document.write(results[i] + '<br />');
     
    })();
    Is probably more along the lines of what you are trying to do.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Jul 2014
    Posts
    2
    Hi!
    This helps a lot! As I said, I'm a beginner and have only been programming for two weeks now so I have obviously a lot of learning to do!
    Anyway, thank you very much for your reply!

  5. #5
    Join Date
    May 2014
    Posts
    936
    Two weeks? Good work actually, you're doing better than some folks who've done this for years!
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles