Home > OS >  JavaScript, JSON : how can I run a foor loop a specific amount of time?
JavaScript, JSON : how can I run a foor loop a specific amount of time?

Time:12-17

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:

{"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 don't want to show the sum the first time but later show it every 3 times

Got any ideas? :D

PS. I prefer staying basic and only use a for loop

Have a nice evening

CodePudding user response:

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);
}

CodePudding user response:

I found this out, yours is obviously shorter but here was my Idea :D

let i = 0;
const inputBuffer = [];
let db = false;
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 (db && i % 3 == 0) {
        let sum = 0;
        for (let ii = 0; ii < i; ii  ) {
            sum  = inputBuffer[ii];
        }
        output.sum = sum;
    }
    i  
    db = true;
    
    output.current = input;
   
    return JSON.stringify(output);
}
  • Related