User enters any number but only store number that are divisible by 3 and display back












1















Prepare script to store data into an array. The user can enter any number, but ONLY store value that is divisible by number three. Then, display back the data (stored in Array) on HTML page.



I need to do this using JavaScript code. How do store value the value and display? Thank you.










share|improve this question























  • we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

    – P.hunter
    Nov 25 '18 at 13:54











  • Array.prototype.filter and modulus operator are your friends here

    – Rob_M
    Nov 25 '18 at 13:57











  • For how much time you want to store the entered values? Only valid for current session?

    – Prashant Pimpale
    Nov 25 '18 at 13:58













  • The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

    – Prashant Pimpale
    Nov 25 '18 at 14:00











  • @P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

    – Sugma
    Nov 25 '18 at 14:12


















1















Prepare script to store data into an array. The user can enter any number, but ONLY store value that is divisible by number three. Then, display back the data (stored in Array) on HTML page.



I need to do this using JavaScript code. How do store value the value and display? Thank you.










share|improve this question























  • we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

    – P.hunter
    Nov 25 '18 at 13:54











  • Array.prototype.filter and modulus operator are your friends here

    – Rob_M
    Nov 25 '18 at 13:57











  • For how much time you want to store the entered values? Only valid for current session?

    – Prashant Pimpale
    Nov 25 '18 at 13:58













  • The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

    – Prashant Pimpale
    Nov 25 '18 at 14:00











  • @P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

    – Sugma
    Nov 25 '18 at 14:12
















1












1








1








Prepare script to store data into an array. The user can enter any number, but ONLY store value that is divisible by number three. Then, display back the data (stored in Array) on HTML page.



I need to do this using JavaScript code. How do store value the value and display? Thank you.










share|improve this question














Prepare script to store data into an array. The user can enter any number, but ONLY store value that is divisible by number three. Then, display back the data (stored in Array) on HTML page.



I need to do this using JavaScript code. How do store value the value and display? Thank you.







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 13:53









SugmaSugma

213




213













  • we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

    – P.hunter
    Nov 25 '18 at 13:54











  • Array.prototype.filter and modulus operator are your friends here

    – Rob_M
    Nov 25 '18 at 13:57











  • For how much time you want to store the entered values? Only valid for current session?

    – Prashant Pimpale
    Nov 25 '18 at 13:58













  • The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

    – Prashant Pimpale
    Nov 25 '18 at 14:00











  • @P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

    – Sugma
    Nov 25 '18 at 14:12





















  • we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

    – P.hunter
    Nov 25 '18 at 13:54











  • Array.prototype.filter and modulus operator are your friends here

    – Rob_M
    Nov 25 '18 at 13:57











  • For how much time you want to store the entered values? Only valid for current session?

    – Prashant Pimpale
    Nov 25 '18 at 13:58













  • The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

    – Prashant Pimpale
    Nov 25 '18 at 14:00











  • @P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

    – Sugma
    Nov 25 '18 at 14:12



















we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

– P.hunter
Nov 25 '18 at 13:54





we need a snippet of code that you have tried in order to give you hints and direct answers (in some cases).

– P.hunter
Nov 25 '18 at 13:54













Array.prototype.filter and modulus operator are your friends here

– Rob_M
Nov 25 '18 at 13:57





Array.prototype.filter and modulus operator are your friends here

– Rob_M
Nov 25 '18 at 13:57













For how much time you want to store the entered values? Only valid for current session?

– Prashant Pimpale
Nov 25 '18 at 13:58







For how much time you want to store the entered values? Only valid for current session?

– Prashant Pimpale
Nov 25 '18 at 13:58















The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

– Prashant Pimpale
Nov 25 '18 at 14:00





The user can enter any number, but ONLY store value that is divisible by number three --> Using prompt? or how?

– Prashant Pimpale
Nov 25 '18 at 14:00













@P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

– Sugma
Nov 25 '18 at 14:12







@P.hunter var arr = ; for (var i = 0; i < 1; i++) { arr.push(prompt('Enter a number ')); } alert(' ' + arr.join('')); yes only for current session

– Sugma
Nov 25 '18 at 14:12














2 Answers
2






active

oldest

votes


















1














Is this what you required?






var numbers = ; // Array to store the valid numbers

function openPromt() {
var num = prompt("Please enter a number", "0");
storeIfNumValid(num);
}

openPromt();

// funtion that will call after each value from `prompt`
function storeIfNumValid(num) {
if (num != null && num % 3 === 0) {
numbers.push(num);
openPromt();
} else {
console.log(numbers);
document.getElementById("demo").innerHTML = numbers;
}
}

<div id="demo"></div>








share|improve this answer

































    0














    var array = ;
    $(document).ready(function() {
    $("#yourInputId").keydown(function (e) {
    // Allow: backspace, delete, tab, escape, enter and .
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
    // Allow: Ctrl/cmd+A
    (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
    // Allow: Ctrl/cmd+C
    (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
    // Allow: Ctrl/cmd+X
    (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
    // Allow: home, end, left, right
    (e.keyCode >= 35 && e.keyCode <= 39)) {
    // let it happen, don't do anything
    return;
    }
    // Ensure that it is a number and stop the keypress
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
    e.preventDefault();
    }
    });
    });
    var SOMENAME = document.getElementById('yourInputId').value
    if(SOMENAME % 3 == 0){
    array.push(SOMENAME);
    for (numb = 0; numb < array.length; numb++)
    document.write(array[i]);
    }
    else{
    console.log('error')} //u can do anything if 'IF' condition is false





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468183%2fuser-enters-any-number-but-only-store-number-that-are-divisible-by-3-and-display%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Is this what you required?






      var numbers = ; // Array to store the valid numbers

      function openPromt() {
      var num = prompt("Please enter a number", "0");
      storeIfNumValid(num);
      }

      openPromt();

      // funtion that will call after each value from `prompt`
      function storeIfNumValid(num) {
      if (num != null && num % 3 === 0) {
      numbers.push(num);
      openPromt();
      } else {
      console.log(numbers);
      document.getElementById("demo").innerHTML = numbers;
      }
      }

      <div id="demo"></div>








      share|improve this answer






























        1














        Is this what you required?






        var numbers = ; // Array to store the valid numbers

        function openPromt() {
        var num = prompt("Please enter a number", "0");
        storeIfNumValid(num);
        }

        openPromt();

        // funtion that will call after each value from `prompt`
        function storeIfNumValid(num) {
        if (num != null && num % 3 === 0) {
        numbers.push(num);
        openPromt();
        } else {
        console.log(numbers);
        document.getElementById("demo").innerHTML = numbers;
        }
        }

        <div id="demo"></div>








        share|improve this answer




























          1












          1








          1







          Is this what you required?






          var numbers = ; // Array to store the valid numbers

          function openPromt() {
          var num = prompt("Please enter a number", "0");
          storeIfNumValid(num);
          }

          openPromt();

          // funtion that will call after each value from `prompt`
          function storeIfNumValid(num) {
          if (num != null && num % 3 === 0) {
          numbers.push(num);
          openPromt();
          } else {
          console.log(numbers);
          document.getElementById("demo").innerHTML = numbers;
          }
          }

          <div id="demo"></div>








          share|improve this answer















          Is this what you required?






          var numbers = ; // Array to store the valid numbers

          function openPromt() {
          var num = prompt("Please enter a number", "0");
          storeIfNumValid(num);
          }

          openPromt();

          // funtion that will call after each value from `prompt`
          function storeIfNumValid(num) {
          if (num != null && num % 3 === 0) {
          numbers.push(num);
          openPromt();
          } else {
          console.log(numbers);
          document.getElementById("demo").innerHTML = numbers;
          }
          }

          <div id="demo"></div>








          var numbers = ; // Array to store the valid numbers

          function openPromt() {
          var num = prompt("Please enter a number", "0");
          storeIfNumValid(num);
          }

          openPromt();

          // funtion that will call after each value from `prompt`
          function storeIfNumValid(num) {
          if (num != null && num % 3 === 0) {
          numbers.push(num);
          openPromt();
          } else {
          console.log(numbers);
          document.getElementById("demo").innerHTML = numbers;
          }
          }

          <div id="demo"></div>





          var numbers = ; // Array to store the valid numbers

          function openPromt() {
          var num = prompt("Please enter a number", "0");
          storeIfNumValid(num);
          }

          openPromt();

          // funtion that will call after each value from `prompt`
          function storeIfNumValid(num) {
          if (num != null && num % 3 === 0) {
          numbers.push(num);
          openPromt();
          } else {
          console.log(numbers);
          document.getElementById("demo").innerHTML = numbers;
          }
          }

          <div id="demo"></div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 15:24

























          answered Nov 25 '18 at 14:10









          Prashant PimpalePrashant Pimpale

          3,52331034




          3,52331034

























              0














              var array = ;
              $(document).ready(function() {
              $("#yourInputId").keydown(function (e) {
              // Allow: backspace, delete, tab, escape, enter and .
              if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
              // Allow: Ctrl/cmd+A
              (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
              // Allow: Ctrl/cmd+C
              (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
              // Allow: Ctrl/cmd+X
              (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
              // Allow: home, end, left, right
              (e.keyCode >= 35 && e.keyCode <= 39)) {
              // let it happen, don't do anything
              return;
              }
              // Ensure that it is a number and stop the keypress
              if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
              e.preventDefault();
              }
              });
              });
              var SOMENAME = document.getElementById('yourInputId').value
              if(SOMENAME % 3 == 0){
              array.push(SOMENAME);
              for (numb = 0; numb < array.length; numb++)
              document.write(array[i]);
              }
              else{
              console.log('error')} //u can do anything if 'IF' condition is false





              share|improve this answer




























                0














                var array = ;
                $(document).ready(function() {
                $("#yourInputId").keydown(function (e) {
                // Allow: backspace, delete, tab, escape, enter and .
                if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                // Allow: Ctrl/cmd+A
                (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
                // Allow: Ctrl/cmd+C
                (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
                // Allow: Ctrl/cmd+X
                (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
                // Allow: home, end, left, right
                (e.keyCode >= 35 && e.keyCode <= 39)) {
                // let it happen, don't do anything
                return;
                }
                // Ensure that it is a number and stop the keypress
                if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
                }
                });
                });
                var SOMENAME = document.getElementById('yourInputId').value
                if(SOMENAME % 3 == 0){
                array.push(SOMENAME);
                for (numb = 0; numb < array.length; numb++)
                document.write(array[i]);
                }
                else{
                console.log('error')} //u can do anything if 'IF' condition is false





                share|improve this answer


























                  0












                  0








                  0







                  var array = ;
                  $(document).ready(function() {
                  $("#yourInputId").keydown(function (e) {
                  // Allow: backspace, delete, tab, escape, enter and .
                  if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                  // Allow: Ctrl/cmd+A
                  (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: Ctrl/cmd+C
                  (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: Ctrl/cmd+X
                  (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: home, end, left, right
                  (e.keyCode >= 35 && e.keyCode <= 39)) {
                  // let it happen, don't do anything
                  return;
                  }
                  // Ensure that it is a number and stop the keypress
                  if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                  e.preventDefault();
                  }
                  });
                  });
                  var SOMENAME = document.getElementById('yourInputId').value
                  if(SOMENAME % 3 == 0){
                  array.push(SOMENAME);
                  for (numb = 0; numb < array.length; numb++)
                  document.write(array[i]);
                  }
                  else{
                  console.log('error')} //u can do anything if 'IF' condition is false





                  share|improve this answer













                  var array = ;
                  $(document).ready(function() {
                  $("#yourInputId").keydown(function (e) {
                  // Allow: backspace, delete, tab, escape, enter and .
                  if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                  // Allow: Ctrl/cmd+A
                  (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: Ctrl/cmd+C
                  (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: Ctrl/cmd+X
                  (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
                  // Allow: home, end, left, right
                  (e.keyCode >= 35 && e.keyCode <= 39)) {
                  // let it happen, don't do anything
                  return;
                  }
                  // Ensure that it is a number and stop the keypress
                  if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                  e.preventDefault();
                  }
                  });
                  });
                  var SOMENAME = document.getElementById('yourInputId').value
                  if(SOMENAME % 3 == 0){
                  array.push(SOMENAME);
                  for (numb = 0; numb < array.length; numb++)
                  document.write(array[i]);
                  }
                  else{
                  console.log('error')} //u can do anything if 'IF' condition is false






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 14:09









                  codRcodR

                  518




                  518






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468183%2fuser-enters-any-number-but-only-store-number-that-are-divisible-by-3-and-display%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga