How to calculate repeating numbers in one big input integer

时间:2016-10-23 18:10:44

标签: javascript

I have looked online and tried myself ... but i have confused my self and nothing seems to be working. Can anyone help me to save and count many repeating 7s in one integer from prompt input.

var countNum = prompt("Enter an integer ", "Type here ");
     document.getElementById("integer").innerHTML = countNum;

2 个答案:

答案 0 :(得分:0)

可能会帮助你

var countNum = prompt("Enter an integer ", "Type here ");
document.getElementById("integer").innerHTML = countNum.split('7').length - 1;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
</head>
<body>
<div id="integer"></div>
</body>
</html>

答案 1 :(得分:0)

您可以使用regular expression

&#13;
&#13;
FOSRestBundle
&#13;
var countNum = prompt('Enter an integer', 'Type here'),
    divInteger = document.getElementById('integer');

divInteger.innerHTML = (countNum.match(/7/g) || []).length;
&#13;
&#13;
&#13;