1

They want if you enter random number like 600 the program to print "It's made of three digits",they want to be with a cycle and the input to be taken from the user.We cannot use document.write(""); Sorry if already similar post exist i cannot find what i was looking for.Down there is part of my idea basically input,button and getting the input with document.getelementbyid and somehow to show it with inner html.Thanks in advance!

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>


    <p id="result">Hello, please enter a number and the program will show the number of digits
    the number is made of.</p>

    <input id="input1" name="x" placeholder="Enter a number " />

    <button onclick="result()">submit</button>

    <script>
function firstFunction() {
   var input = document.getElementById("input1");
   var result = result();

   document.getElementById("result").innerHTML = ;
   
}

function result(){

//function for the cycle


}
    


    </script>


</body>
</html>
Kirito
  • 13
  • 3

2 Answers2

0

I hope I understand what you want, a function to tell someone how many digits they have entered into a text input. I think what I have made might be too fancy for what you need, but maybe not. I'm using RegEx to count the number of digits in the input and then convert that number into the string that represents it. If there are no digits, but there are letters or special characters, it returns as zero. It only counts up to nine and stops, but you can easily go as high as you need.

function res() {
  var input = document.getElementById("input1");
 var result = document.getElementById("result");
 var pattern = /[0-9]/g;
 var num = (input.value).match(pattern);
 var str;
 if (!num) {
   str = "zero digits";
 } else {
   switch (num.length) {
     case 0:
      str = "zero digits";
     break;
     case 1:
   str = "one digit";
     break;
     case 2:
      str = "two digits";
   break;
       case 3:
     str = "three digits";
     break;
       case 4:
        str = "four digits";
     break;
       case 5:
     str = "five digits";
     break;
     case 6:
      str = "six digits";
     break;
     case 7:
     str = "seven digits";
      break;
       case 8:
        str = "eight digits";
     break;
     case 9:
   str = "nine digits";
     break;
     default:
        str = "more than nine digits";
     break;
   }
 }
 result.innerHTML = "It's made of " + str + "."
}
<p id="result">Hello, please enter a number and the program will show the number of digits the number is made of.</p>
<input id="input1" name="x" placeholder="Enter a number" />
<button onclick="res()">submit</button>
ecg8
  • 1,362
  • 1
  • 12
  • 16
  • Thanks so much for replying i was thinking no one will,well it's working perfectly but they want the solution to be with loop otherwise there are probably more solutions found one, but they want a loop`$('#input').on('keypress', function(e) { var count = $(this).val().length; $('#span').text(count);` – Kirito Apr 14 '18 at 16:09
  • I don't understand the way you are using loop. Also, what "they" want doesn't matter. What is it that "you" are trying to achieve? – ecg8 Apr 14 '18 at 17:53
  • From the university and i' m use to seeing loops only in this structure for (int i = 1; i <= 5; i++) sorry for the low knowledge.. { Console.WriteLine(i); } – Kirito Apr 14 '18 at 19:53
0

Get number of digits with JavaScript

It's already been done i don't see it before.

function count_digits(n) {
numDigits = 0;
integers = Math.abs(n);

while (integers > 0) {
    integers = (integers - integers % 10) / 10;
    numDigits++;
}
return numDigits;

}

Kirito
  • 13
  • 3