www.webdeveloper.com
Results 1 to 11 of 11

Thread: [RESOLVED] Writing to a table using prompt()

  1. #1
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    resolved [RESOLVED] Writing to a table using prompt()

    Hey! I'm taking an introduction to JavaScript course and I'm struggling on an exercise. I was hoping someone could help me out! Or at least give me a couple of hints... Any help would be greatly appreciated, thanks!

    Q: For this exercise you will need to use your tables and dialogue boxes skills.

    To start, you will need to define a table with 3 elements. Each element will
    be associated with a different first name.

    Next, you need to create a first function that will allow the addition of
    a first name into the table. The function will have the previously created
    table as a parameter with one additional element that's value will be the
    first name that we wish to add. You will need to use a prompt-style dialogue
    box to ask the user to specidy the first name they'd like to add to the
    table.

    You will also need to create a second function who's goal will be to
    display the first names that constitute the different elements of the
    table. This function will also use the previously created table as a
    parameter and will display all of the first names with the help of a
    alert style dialogue box that will hold all of the first name seperated

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    How about a "good faith" effort at a code solution of your own design?
    I would be happy to comment on and make some corrections to your design,
    but I don't have the time to design and debug it for you.

  3. #3
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    My code

    Of course! Here's what I have so far... My problem is that the exercise asks for two functions where as I've only been able to figure out getting the correct result with one. I'm unsure how to add a user-defined array element from a prompt. Thoughts?

    <html>
    <head>
    <title>Exercice 04</title>
    </head>
    <script language="javascript">

    <!--

    var myArray = new Array(4);

    myArray[0] = "Paul";
    myArray[1] = "David";
    myArray[2] = "Mireille";

    function nom() {
    var reponse = prompt("Quel est votre nom?", "")
    alert (reponse +";" +myArray.join(";"))
    }

    // -->
    </script>

    <body onload="nom();">

    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb Something to consider ...

    Consider these changes (see comments within code):
    Code:
    <html>
    <head>
    <title>Exercice 04</title>
    
    <script type="text/javascript"> <!-- archaic: language="javascript" -->
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1112942#post1112942
    
    <!-- Un-necessary to surround the "script" code -->
    
    var myArray = [];		// why define [4] when you only assign [3]
    myArray[0] = "Paul";
    myArray[1] = "David";
    myArray[2] = "Mireille";
    
    function nom() {
      alert ("Before: " +myArray.join(";"))
      var reponse = prompt("Quel est votre nom?", "")
      myArray.push(reponse);
      alert ("After: " +myArray.join(";"))
    }
    
    </script>
    </head>
    
    <body> <!--  onload="nom();"    OK, but for additional testing try button below -->
    <button onclick="nom()">Add to Array</button>
    </body>
    </html>
    BTW: You should surround your script with [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to see your attempts.

  5. #5
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    Thank you

    JMRKER: Thank you for taking the time to help me with this! I really appreciate it. JavaScript has been causing me many sleepless nights as of late and my teacher isn't much help.

    I just joined the forum yesterday so I wasn't aware of the [code] tags but will make sure to use them in the future.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Exclamation

    You're most welcome.
    Happy to help!

    Hopefully you understand the changes suggested.
    If you are just starting, the teacher many not accept the .push instruction as it is more advanced.

    You could make the assignment as this instead within the function of post #4 ...
    Code:
    //  myArray.push(reponse);
      myArray[myArray.length] = reponse;
    to get the same results.

    But be sure you can explain the difference should the teacher quiz you about your solution.

  7. #7
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    I missed something

    Me again! Sorry to bother you... if you're sick of me no worries! I'll figure it out eventually. The exercise was rejected by my teacher and he's making me do it again.

    Here's what he said I'm missing (I'm translating from French so if it sounds a little wonky that's why):

    "The instructions that you used are the correct ones, it just doesn't follow the directions. You need to have 2 functions.
    - One you've already begun (the function has to add the first name acquired from the prompt() to an array and then return the new array but without displaying it's contents).
    - The 2nd function needs to use the previously created array as a parameter and display the contents in an alert.

    Here is an example of what your functions could look like:
    array1 = variables
    array2 = addUserFirstNametoArray (array1);
    DisplayArrayValues(array2);
    "

    I'm going to play around with it on my own; but I wouldn't turn down a hint or two.

  8. #8
    Join Date
    Sep 2009
    Location
    London
    Posts
    51
    Hi Carmel, you could try this:

    Code:
    <html>
        <head>
            <title>Exercice 04</title>
            <script type="text/javascript">
                
                // Global array to store names entered by the user.
                var arrNames = [];
                
                function addUser()
                {
                    //Display the prompt asking the user to enter their name
                    var response = prompt("Quel est votre nom?", "");
                    
                    //Add the text that user has entered through the prompt at the end of the array arrNames.
                    arrNames[arrNames.length] = response;
                    
                    //Or can use array push function
                    //arrNames.push(response);
                    
                    //Call the displayName() function to display the name/s entered by the user.
                    displayName();
                }
                
                function displayName()
                {
                    //Display the names entered by the user. "\n" used for new line when displaying text within the JavaScript alert.
                    alert('Names:\n' + arrNames.join('\n'));
                }
                
            </script>
        </head>
        <body>
            <button onclick="addUser()">
                Add to Array
            </button>
        </body>
    </html>

  9. #9
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    Question What about this?

    Thanks tpircsavaj! I actually played around with it today and came up with something else. Could someone please let me know if this makes sense? I think I'm on to something...

    Code:
    <html>
    <head>
    <title>Exercice 04</title>
    
    <script language="javascript">
    
    var monArray = ["Paul","David","Mireille"];
    montreprenoms(monArray);
    var ajourArray = prenom(monArray);
    montreprenoms(ajourArray);
    
    function prenom(leArray){
    var reponse = prompt("Quel est votre prénom?","");
    
    if(reponse!=null && reponse!="")
    leArray[leArray.length] = reponse;
    return leArray;
    }
    
    function montreprenoms(leArray){
    var montreString = "";
    for(var i = 0; i < leArray.length; i++)
    {
    if(i > 0)
    montreString += ";";
    montreString += leArray[i];
    }
    alert(montreString);
    }
    </script>
    </head>
    <body></body>
    </html>

  10. #10
    Join Date
    Sep 2009
    Location
    London
    Posts
    51
    Yes that is fine, just few tips:

    Try to avoid:
    Code:
    if (i > 0) 
        montreString += ";";
    instead write:
    Code:
    if (i > 0) {
        montreString += ";";
    }
    There is no need to loop through the array, you can use array.join to achieve the same.

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb Alternate solution ...

    Alternatively, keeping with your original design, but using 2 functions with array as a parameter ...
    Code:
    <html>
    <head>
    <title>Exercice 04</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1112942#post1112942
    
    var myArray = [];
    myArray[0] = "Paul";
    myArray[1] = "David";
    myArray[2] = "Mireille";
    
    function nom(ArrayOld) {
      var reponse = prompt("Quel est votre nom?", "");
      ArrayOld[ArrayOld.length] = reponse;
    }
    function show(ArrayNow) {
      alert ('Contents currently:\n'+ArrayNow.join(";"))
    }
    </script>
    </head>
    <body>
    <button onclick="show(myArray)">Show Array</button>
    <button onclick="nom(myArray)">Add to Array</button>
    </body>
    </html>

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