Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a problem with the iteration loop javascript

I have a problem with the repetition of an error. I need to display the hours at 18, 19, 20, 21, 22 with the minutes, but there is a repetition at 18

let CloseTime = 23;
let TheTimeNow = 20;

let mintes = 20;
let LastMinutes = 60;

let resultMintes = [];
let lastResult = [];

for (TheTimeNow; TheTimeNow <= CloseTime; TheTimeNow++) {
  for (mintes; mintes <= LastMinutes; mintes += 5) {
    resultMintes.push(`${TheTimeNow}:${mintes}`);
  }

  lastResult.push(resultMintes);
}

console.log(lastResult);

https://codesandbox.io/s/staging-glade-e2msc?file=/src/App.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

thats the problem with the inner loop, in the first iteration of outer loop, the value of minutes stays at 60, so the inner loop end immediately in the following iteration, what you need is probably for (mintes = 20; mintes <= LastMinutes; mintes += 5) re-initialize your mintes (you got a u missing but this just spelling not big deal) so the inner loop can restart in every iteration of outer loop

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading