Web Analytics Made Easy -
StatCounter how do you store a prompt as a variable? am I doing this right? - CodingForum

Announcement

Collapse
No announcement yet.

how do you store a prompt as a variable? am I doing this right?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how do you store a prompt as a variable? am I doing this right?

    I want to store a prompt three characters as a variable. Am I doing this right?

    Code:
    var userArray = [];
    	for (var i = 0; i < 3; i++) {
    		userArray[i] = prompt('Enter a character');
    		
    	}
    	console.log(userArray);

  • #2
    prompts are long obsolete. The JavaScript prompt is a relic from the 1990's that you seldom or never see being used in modern day websites. You should use DOM methods to obtain data from/display messages to the user.

    If you insist on using a prompt, at least get rid of the ugly "undefined" message.

    Code:
    userArray[i] = prompt('Enter a character',"");
    If you want to store all three characters in a single variable (why would you want to do this?) then use the join() method and strip the default comma seperators.
    Last edited by Philip M; Sep 22, 2016, 03:03 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.

    Comment


    • #3
      What do you expect to happen when someone uses the debugging checkbox in the prompt to turn off JavaScript - or simply has the prompt completely disabled in their browser.

      If you want a dialog box for displaying or collecting information then search for "sweetalert2" which is a JavaScript generated equivalent that actually looks the same across all browsers rather than using built in dialogs that are different for every browser.
      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.

      Comment


      • #4
        I have to because of these instructions: Generate an interesting pattern (using the x and y values to make decisions) that places the three user-provided characters in a 2-dimensional grid

        Comment


        • #5
          I am a beginner coder, so I don't know a lot of different methods to do one thing.

          Comment


          • #6
            I worded it weird sorry about that. I meant store each character entered as a variable.

            Comment


            • #7
              The normal way to collect user data is JavaScript is from an HTML form - prompt is a debugging tool for older browsers and displays extra debugging options in those browsers. Netscape 4 is the most recent browser that used it for collecting data from users.
              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.

              Comment


              • #8
                prompts are very old i wouldn't use them. If you need to get input try to use a text box. something like
                Code:
                <input type="text" id="test"></input>
                for HTML then JS is
                Code:
                var text = document.getElementById("test").innerhtml
                Your prompt does work however. Prompt syntax is as follows
                Code:
                var x = prompt(message)

                Comment


                • #9
                  Originally posted by abeltensor View Post
                  prompts are very old i wouldn't use them. If you need to get input try to use a text box. something like
                  Code:
                  <input type="text" id="test"></input>
                  for HTML then JS is
                  Code:
                  var text = document.getElementById("test").innerhtml
                  Your prompt does work however. Prompt syntax is as follows
                  Code:
                  var x = prompt(message)
                  Well actually my reply isnt complete. Youd probably want something more like this
                  Code:
                  <input type"text" id="input">
                  <input type="submit" onclick="testFunction()">
                  <script>
                  function testFunction(){
                       var text = document.getElementById("input).innerhtml;
                       return text;
                  }
                  //do whatever you want with the text.
                  </script>

                  Comment


                  • #10
                    Originally posted by abeltensor View Post
                    Well actually my reply isnt complete. Youd probably want something more like this
                    Code:
                         var text = document.getElementById("input).innerhtml;
                    It is not innerhtml, it is innerHTML and is case sensitive.

                    and var text = document.getElementById("input).innerhtml;
                    should be var text = document.getElementById("input").value;

                    Please do not handout incorrect advice to beginners when you plainly have only a weak grasp of Javascript.
                    Last edited by Philip M; Sep 23, 2016, 03:29 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.

                    Comment


                    • #11
                      So to fix the code (the ... represents the path to the server side version that will run if JavaScript is not available)

                      Code:
                      <form action="..." method="POST">
                      <input type"text" id="input">
                      <input type="submit">
                      </form>
                      <script>
                      (function() {
                      use strict';
                      function testFunction(){
                           var text = document.getElementById("input).value;
                           return text;
                      }
                      function submitted(e) {
                      var text = testFunction();
                      //do whatever you want with the text.
                      e.preventDefault();
                      }
                      document.getElementById('input').addEventListener('click', submitted, false);
                      }());
                      </script>
                      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.

                      Comment

                      Working...
                      X