I ran into a problem with a for-loop in js. Let me try to explain my issue.
For example, we have an array
let arr = [15, 29, 44];
also we have a for
loop
for (let h = 1; h < 100; h +=10) {
}
I need to iterate over an array inside the loop, so when h
goes bigger than array it continues from the value number.
In the end I should get values like: 10, 15, 25, 35, 44, 54...
I'm trying to make a double loop, but it confuses me.
My code:
for (let h = 1; h < 100; h +=10) {
for (var i = 0; i < arr .length; i++) {
if(i > h) {
h=i;
}
}
}
Does there better way to do it?
Thanks
You could take a single loop with a check if h
is greater than a value from the array, then take this value instead.
const array = [15, 29, 44], result = []; for (let h = 10, i = 0; h < 100; h +=10) { if (h > array[i]) h = array[i++]; result.push(h); } console.log(...result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.