javascript中如何在某个位置输出某个数字的数字

认为它会像

对于范围 x,y 中的数字 如果数字为个数或数字为数百则打印 else

??? 我不知道用什么命令来执行 if 语句。


幕布斯7119047
浏览 111回答 4
4回答

HUH函数

您可以尝试在此处使用模运算符:for (i=100; i <= 200; ++i) {&nbsp; &nbsp; if (i % 10 == 2 || Math.floor(i / 10) % 10 == 3) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; // turned this off for demo purposes&nbsp; &nbsp; &nbsp; &nbsp; // console.log("???");&nbsp; &nbsp; }}

天涯尽头无女友

你的问题太模糊了,请说得更具体一些。话虽这么说,这个函数应该满足你的要求。function printIfDigitIsInPlace(INPUT, DIGIT, PLACE) {&nbsp; &nbsp; const arr = Array.from(INPUT.toString());&nbsp; &nbsp; if (arr[PLACE] === DIGIT.toString()) console.log(INPUT);}

精慕HU

for (let i = 100; i <= 200; i++) {&nbsp; &nbsp; if (i % 10 === 2 || (i / 10) % 10 === 3) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log('???')&nbsp; &nbsp; }}

aluckdog

tensDigit您可以将和存储onesDigit在变量中,然后在 if 语句中检查它们以获得更好的可读性:for (let num = 100; num <= 200; num++) {&nbsp; &nbsp; const tensDigit = Math.floor((num % 100) / 10);&nbsp; &nbsp; const onesDigit = num % 10;&nbsp; &nbsp; if (tensDigit === 3 || onesDigit === 2) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(num);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript