skip to Main Content

I’m trying to create an equation for a time calculation but can’t figure out what I need to do. I have a time in minutes that is getting multiplied by .005 which that number is then subtracted by the time to create a new time number. I then need this to repeat x amount of times. Here is the excel version of it but I don’t know how to write it in code to make it work once you input the time number.

1

What I currently have is this:

var time = document.getElementById("buildTime").value;
var x = time * .005;

const timeReduction = () => {
  time * .005 = -x + time;
}

2

Answers


  1. This simply doesn’t make sense:

    time * .005 = -x + time;
    

    Even if that line of code worked, what would the expected result be? Should time be modified by this or not?

    Perhaps you meant this?:

    time = time - time * 0.005;
    

    For example:

    let time = 10000;
    
    const timeReduction = () => {
      time = time - time * 0.005;
    }
    
    for (let i = 0; i < 10; i++) {
      console.log(time);
      timeReduction();
    }

    Which can even be simplified a bit:

    let time = 10000;
    
    const timeReduction = () => {
      time *= 0.995;
    }
    
    for (let i = 0; i < 10; i++) {
      console.log(time);
      timeReduction();
    }

    Because subtracting x * .005 from x is the same result as simply x * .995.

    Login or Signup to reply.
  2. I’m going to add an additional option here that doesn’t require a loop.

    If want to do this ‘n’ times, you can do:

    const timeReduction = (time, n) => time * Math.pow(0.955, n)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search