How to create number like this in javascript loop
I'am sorry' i'am bad english
CodePudding user response:
let result = ""
for(let i = 1 ; i <= 10 ; i ){
for(let j = 1 ; j <= 3 ; j ){
if(i === 10)
{
break;
}
if(i % 4 == 0)
{
result = ' result ' i ' back to start result ' j '\n';
}else{
result = ' result ' i ' also result ' j '\n';
}
if((i % 3) != 0)
{
i ;
}
}
if(i === 10)
{
break;
}
}
console.log(result)
CodePudding user response:
I think you are looking for something like this
const loop = (from, to, limit) => {
for (let i = from; i <= to; i ) {
let number = i % limit === 0 ? limit : i % limit;
//do something with the number
console.log(number);
}
};
Then if you call it like this:
loop(1, 10, 3);
The logged result is 1, 2, 3, 1, 2, 3, 1, 2, 3, 1