猿问

迭代的最近整除数

我有这个代码

https://codepen.io/clickDcode/pen/jOOEQRQ


function myFunction() {

  var ans;

  var num = document.getElementById("num").value;

  var div = document.getElementById("div").value;

  var mod = (Number(num) + Number(div)) % Number(div);


  if (mod == 0)

    var ans = num;

  else

    var ans = (Number(num) + Number(div)) - Number(mod);

  document.getElementById("demo").innerHTML = ans;

}

<p>

  Divisible by:

  <input type="number" id="div" name="text2" placeholder="0">

  <p/>

  <p>

    Your Number:

    <input type="number" id="num" name="text1" placeholder="0" onchange="myFunction()">

  </p>


  <p id="demo"></p>

已经完成了获取最近的可整除数
现在,我需要像下面这样迭代 (10) 答案示例:

可被:22 整除
您的号码:55

(回答:最近的更大的整除数是 66)

输出必须是:

可被:22 整除
您的号码:55

66
88
110
132
154
176
198
220
242
264

(通过可整除数使用迭代数)


慕容3067478
浏览 126回答 2
2回答

芜湖不芜

递归呢?function ndn(d, n, i, ac) {&nbsp; &nbsp; if (i === 0) return ac;&nbsp; &nbsp; var m = n % d;&nbsp; &nbsp; if (m !== 0) n += d - m;&nbsp; &nbsp; ac.push(n);&nbsp; &nbsp; return ndn(d, n + d, --i, ac);}ndn(22, 55, 10, []);

繁华开满天机

function myFunction() {&nbsp; var ans = 0;&nbsp; var text = "";&nbsp; var num = document.getElementById("num").value;&nbsp; var div = document.getElementById("div").value;&nbsp; var mod = (Number(num) + Number(div)) % Number(div);&nbsp; if (mod == 0)&nbsp; &nbsp; var ans = num;&nbsp; else&nbsp; &nbsp; var ans = (Number(num) + Number(div)) - Number(mod);&nbsp; var init = 0;&nbsp; if (Number(ans) % Number(div) === 0) {&nbsp; &nbsp; text = addToText(text, ans)&nbsp; &nbsp; init = 1;&nbsp; }&nbsp; for (i = init; i < 10; i++) {&nbsp; &nbsp; ans = Number(ans) + Number(div);&nbsp; &nbsp; text = addToText(text, ans)&nbsp; }&nbsp; function addToText(text, ans) {&nbsp; &nbsp; return text + "Next TS : " + ans + "<br>";&nbsp; }&nbsp; document.getElementById("demo").innerHTML = text;}<p>&nbsp; Divisible by:&nbsp; <input type="number" id="div" name="text2" placeholder="0">&nbsp; <p/>&nbsp; <p>&nbsp; &nbsp; Your Number:&nbsp; &nbsp; <input type="number" id="num" name="text1" placeholder="0" onchange="myFunction()">&nbsp; </p>&nbsp; <p id="demo"></p>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答