-1

Be gentle:

I'm new to Javascript and curious about how I would go about running a for loop [or suggest a different loop that would be more fitting] that redefines a "let" value after a condition in the loop becomes True. When the last "else if" condition becomes True, it would redefine the Value1 value by adding the const add_to to it, and then begin the first two condition checks again. Infinite loop?

I've provided an example of my current code below:

const fetch = require('node-fetch');
const api_url = 'https://an-api-i-am-using-that-returns-Value1.com'
let Value1 = 100;
const add_to = 10;

async function getTotal() {

    const response = await fetch(api_url);

    Number_Returned = await response.json();
    Number_Left = Value1 - Number_Returned
    Time_Left = Number_Left * 60;

    if (Number_Left > 0){
        console.log(Number_Left)

        var d = parseInt(Time_Left / (3600 * 24));
        console.log(d)

        nd = Time_Left % (24 * 3600);
        var h = parseInt(nd/3600);
        console.log(h)

        Time_Left %= 3600;
        var m = parseInt((Time_Left/60));
        console.log(m)

        const longEnUSFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' });

        var date = new Date();
        date.setMinutes(date.getMinutes() + (d * 24 * 60) + (h * 60) + m);
        console.log(longEnUSFormatter.format(date))
    }
    else if (Number_Left == 0 && Value1 == Number_Returned) {
        console.log(Number_Left)

        d = "no";
        console.log(d)
        h = "no";
        console.log(h)
        m = "no";
        console.log(m)
        date = "Today";
        console.log(date)
    }
    else if (Number_Left < 0) {
        console.log(Number_Left)

        d = "didn't work";
        console.log(d)
        h = "didn't work";
        console.log(h)
        m = "didn't work";
        console.log(m)
        date = "didn't work";
        console.log(date)
        Value1 =+ add_to;
    }
}

async function begin(){
    console.log('calling');
    const result = await getTotal();
    console.log(Value1);
}

begin();

Currently, the script returns the following results after running (example results):

When (Number_Left > 0) is True:

calling
3
0
0
3
Tuesday, April 27, 2021 at 7:55:59 PM GMT+1

When (Number_Left == 0 && Value1 == Number_Returned) is True:

calling
0
no
no
no
Today
98273492

When (Number_Left < 0) is True:

calling
-7
didn't work
didn't work
didn't work
didn't work
10

To reiterate, once the last condition becomes True, I would like to add the const add_to to the let value Value1, beginning the (Number_Left > 0) and (Number_Left == 0 && Value1 == Number_Returned) condition checks again.

Let me know if I can be more specific about what I'm trying to accomplish. Thanks in advance.

IamTrying
  • 39
  • 6

3 Answers3

0

The syntax is += not =+ when you want to add the value and set it

Cody E
  • 179
  • 11
  • Thank you for your comment and that does solve the issue of adding the `add_to` to `Value1`, but I would like the "if" statement to restart and begin checking against the first two conditions again. Right now it is just stuck on the last "else if" condition. – IamTrying Apr 27 '21 at 20:04
  • You could call the function again after changing the value? In a normal for / while loop you would use `continue`, but you are in a for loop – Cody E Apr 27 '21 at 20:12
  • Thanks for the suggestion. I'm going to try this now. – IamTrying Apr 27 '21 at 20:17
0

It looks like you would get your desired result if you change:

Value1 =+ add_to;

to (notice the addition assignment operator):

Value1 += add_to;
annrockio
  • 1
  • 1
  • Thank you for your comment and that does solve the issue of adding the `add_to` to `Value1`, but I would like the "if" statement to restart and begin checking against the first two conditions again. Right now it is just stuck on the last "else if" condition. – IamTrying Apr 27 '21 at 20:04
0

In order to do what I needed this code to do, I had to rewrite it. It may not be the best coding, but it works for what I'm trying to accomplish:

const api_url = 'https://an-api-i-am-using-that-returns-Value1.com';

    async function getTotal(){
        resp = await fetch(api_url);
        Number_Returned = await resp.text();
        return Number_Returned
    }

    async function checkValue1(){
        Value1 = 100;
        const add_to = 10;
        cur = await getTotal();

        if (Value1>=cur){
            return cur
        }
        else if (Value1<cur){
            Value1 = Value1 + add_to
            return cur
        }
    }

    async function calculateTotal(){
        const theValue1 = await checkValue1();
        const numDifference = 2880;
        let numLeft = (Value1 - numDifference) - theValue1;
        let timeDelta = numLeft * 60.097;
        return timeDelta
    }

    async function calcDate(){
        let timeLeft = await calculateTotal();
        let d = parseInt(timeLeft / (3600 * 24));
        document.getElementById('days').innerHTML = d;
        nd = timeLeft % (24 * 3600);
        let h = parseInt(nd/3600);
        document.getElementById('hours').innerHTML = h;
        timeLeft %= 3600;
        let m = parseInt((timeLeft/60));
        document.getElementById('mins').innerHTML = m;
        const longEnUSFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' });
        let date = new Date();
        date.setMinutes(date.getMinutes() + (d * 24 * 60) + (h * 60) + m);
        document.getElementById('date').innerHTML = longEnUSFormatter.format(date);
}

calcDate();
IamTrying
  • 39
  • 6