Created
May 17, 2015 09:18
-
-
Save king-panda/94df17605c9f9f922276 to your computer and use it in GitHub Desktop.
入力した数値の桁数を表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*{margin:0;padding:0;}body{font:30px sans-serif;text-align:center;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>数値を入力してください</p> | |
<form> | |
<input type="number" id="num" value="" pattern="^[0-9]+$"> | |
</form> | |
<p>桁数 : <span id="result"></span></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//入力した数値の桁数を表示 | |
/* 桁数を計算 */ | |
var count = function(x){ | |
digit = 0; | |
while(number!==0){ | |
number = Math.floor(number/10); | |
digit++; | |
} | |
x = digit; | |
return x; | |
}; | |
/* 変数の宣言 */ | |
var number; | |
var digit; | |
/* 桁数の表示 */ | |
document.getElementById("num").addEventListener("change",function(){ | |
number = this.valueAsNumber; | |
document.getElementById("result").innerHTML = count(number); | |
},true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment