Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140817

How to count digits of given number?

$
0
0

I want the user to enter a number and print back the amount of digits of that number.

I know that I can use length, but my homework asking for while loop.

This is what I have so far:

var num;
var count = 0;

num = prompt('Enter number: ');

function counter(x, y) {
  while (x > 0) {
    y++;
    x /= 10;
  }
  return y;
}

var result = counter(num, count);

console.log(result);

When I give the number 3456 (example), I get back the number 328. I want it to print back the number 4.


Viewing all articles
Browse latest Browse all 140817

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>