[英]how to get an output of next `ten` odd and even numbers after entering any random number in javascript
I want to have a logic where if I enter an even number I want next 10 even numbers to be printed and If I enter an odd number, I want next 10 odd numbers to be printed.我想要一个逻辑,如果我输入一个偶数,我想要打印接下来的 10 个偶数,如果我输入一个奇数,我想要打印接下来的 10 个奇数。 How should I rectify this logic inside a function. If someone can please help me rectifying the logic which was answered.我应该如何在function中纠正这个逻辑。如果有人可以帮助我纠正已回答的逻辑。
JS
function oddEven() { var input = prompt(""); for (let x = 1; x <= 10; x++) { console.log(input + x * 2); } } oddEven()
This should work for any number这应该适用于任何数字
const input = 2
for (let x = 1; x <= 10; x += 1) {
console.log(input + x * 2)
}
let inputval = 2; for (let x = 1; x <= 10; x++) { console.log(inputval + x * 2) }
i hope this help我希望这有帮助
function printTen(input){
let list = []
let number = input
while(list.length <= 10){
number++
if(input % 2 === 0 && number % 2 === 0){
console.log(number + " is even");
list.push(number)
}else if(input % 2 !== 0 && number % 2 !== 0) {
console.log(number + " is odd");
list.push(number)
}
}
}
printTen(9)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.