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

JavaScript, JSON – How Can I run a foor loop a specific ammount of time?

Hello So I have this code:
its a bit tricky

let i = 0;
const inputBuffer = [];

const randomnumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);

}
for (let k = 0; k < 10; k++) {
    console.log(convert(randomnumber(0, 1982)));
}

function convert(input) {
    inputBuffer.push(input);
    const output = {"current" : "0"};
    
    
    if (i % 3 == 0) {
        let sum = 0;
        for (let ii = 0; ii < i; ii++) {
            sum += inputBuffer[ii];
        }
        output.sum = sum;
    }
    i++;
    output.current = input;
   
    return JSON.stringify(output);
}

The Output looks like this:

{"current":605,"sum":0}
{"current":708}
{"current":456}
{"current":1838,"sum":1769}
{"current":1619}
{"current":1404}
{"current":1068,"sum":6630}
{"current":1178}
{"current":989}
{"current":1280,"sum":9865}

But I want it to look like this:

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

{"current": 605}
{"current": 708}
{"current": 456}
{"current": 1838,"sum":1769}
{"current": 1619}
{"current": 1404}
{"current": 1068,"sum":6630}
{"current": 1178}
{"current": 989}
{"current": 1280,"sum":9865}
let i = 0;
const inputBuffer = [];

const randomnumber = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);

}
for (let k = 0; k < 10; k++) {
  console.log(convert(randomnumber(0, 1982)));
}

function convert(input) {
  inputBuffer.push(input);
  const output = {
    "current": "0"
  };
  if (i % 3 == 0) {
    let sum = 0;
    for (let ii = 0; ii < i; ii++) {
      sum += inputBuffer[ii];
    }
    output.sum = sum;
  }
  i++;
  output.current = input;

  return JSON.stringify(output);
}

I dont want to show the sum the first time but later show it every 3 times
Got andy ideas? 😀
PS. I prefer staying basic and only use a for loop

Have a nice evening

>Solution :

Could you just check if the i is not zero when you assign the value of output.sum?

Something like the following:

let i = 0;
const inputBuffer = [];

const randomnumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);

}
for (let k = 0; k < 10; k++) {
    console.log(convert(randomnumber(0, 1982)));
}

function convert(input) {
    inputBuffer.push(input);
    const output = {"current" : "0"};
    
    
    if (i % 3 == 0) {
        let sum = 0;
        for (let ii = 0; ii < i; ii++) {
            sum += inputBuffer[ii];
        }
        if (i !== 0) output.sum = sum; // HERE
    }
    i++;
    output.current = input;
   
    return JSON.stringify(output);
}
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