如何统计javascript中用cycle(loop)输入的输入?
How to count the input entered with cycle(loop) in javascript?
他们想要如果你输入像 600 这样的随机数程序打印 "It's made of three digits",他们想要一个循环并且从 user.We 中获取的输入不能使用 document.write("");抱歉,如果已经存在类似的 post,我找不到我要找的东西 for.Down 我的想法有一部分基本上是输入,按钮和使用 document.getelementbyid 获取输入,并以某种方式用内部 html.Thanks提前!
<!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>
我希望我明白你想要什么,一个告诉别人他们在文本输入中输入了多少数字的功能。我想我做的可能对你需要的东西来说太花哨了,但也许不是。我正在使用 RegEx 来计算输入中的位数,然后将该数字转换为表示它的字符串。如果没有数字,但有字母或特殊字符,则returns为零。它只数到九就停止,但您可以轻松达到所需的高度。
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>
Get number of digits with JavaScript
已经完成了,之前没看到
function count_digits(n) {
numDigits = 0;
integers = Math.abs(n);
while (integers > 0) {
integers = (integers - integers % 10) / 10;
numDigits++;
}
return numDigits;
}
他们想要如果你输入像 600 这样的随机数程序打印 "It's made of three digits",他们想要一个循环并且从 user.We 中获取的输入不能使用 document.write("");抱歉,如果已经存在类似的 post,我找不到我要找的东西 for.Down 我的想法有一部分基本上是输入,按钮和使用 document.getelementbyid 获取输入,并以某种方式用内部 html.Thanks提前!
<!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>
我希望我明白你想要什么,一个告诉别人他们在文本输入中输入了多少数字的功能。我想我做的可能对你需要的东西来说太花哨了,但也许不是。我正在使用 RegEx 来计算输入中的位数,然后将该数字转换为表示它的字符串。如果没有数字,但有字母或特殊字符,则returns为零。它只数到九就停止,但您可以轻松达到所需的高度。
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>
Get number of digits with JavaScript
已经完成了,之前没看到
function count_digits(n) {
numDigits = 0;
integers = Math.abs(n);
while (integers > 0) {
integers = (integers - integers % 10) / 10;
numDigits++;
}
return numDigits;
}