【问题标题】:Is there a way to redefine a "let" value when a certain condition becomes True?当某个条件变为真时,有没有办法重新定义“让”值?
【发布时间】:2021-07-21 04:27:54
【问题描述】:

要温柔

我是 Javascript 新手,我很好奇我将如何运行一个 for 循环 [或建议一个更合适的不同循环] 在循环中的条件变为 True 后重新定义“let”值。当最后一个“else if”条件变为 True 时,它​​将通过添加 const add_to 来重新定义 Value1 值,然后再次开始前两个条件检查。无限循环?

我在下面提供了我当前代码的示例:

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

目前脚本运行后返回如下结果(示例结果):

当 (Number_Left > 0) 为真时:

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

当 (Number_Left == 0 && Value1 == Number_Returned) 为真时:

calling
0
no
no
no
Today
98273492

当 (Number_Left

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

重申一下,一旦最后一个条件变为 True,我想将 const add_to 添加到 let 值 Value1,再次开始 (Number_Left &gt; 0)(Number_Left == 0 &amp;&amp; Value1 == Number_Returned) 条件检查。

如果我能更具体地说明我想要完成的工作,请告诉我。提前致谢。

【问题讨论】:

    标签: javascript loops for-loop conditional-statements timedelta


    【解决方案1】:

    当您要添加值并设置它时,语法是+= 而不是=+

    【讨论】:

    • 感谢您的评论,这确实解决了将 add_to 添加到 Value1 的问题,但我希望重新启动“if”语句并再次开始检查前两个条件.现在它只是停留在最后一个“else if”条件。
    • 更改值后可以再次调用该函数吗?在正常的 for / while 循环中,您将使用 continue,但您处于 for 循环中
    • 感谢您的建议。我现在要试试这个。
    【解决方案2】:

    如果你改变,看起来你会得到你想要的结果:

    Value1 =+ add_to;
    

    到(注意addition assignment operator):

    Value1 += add_to;
    

    【讨论】:

    • 感谢您的评论,这确实解决了将 add_to 添加到 Value1 的问题,但我希望重新启动“if”语句并再次开始检查前两个条件.现在它只是停留在最后一个“else if”条件。
    【解决方案3】:

    为了完成我需要这段代码做的事情,我不得不重写它。它可能不是最好的编码,但它适用于我想要完成的工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 2022-01-10
      • 2023-03-29
      • 2020-10-05
      • 2020-02-23
      • 2018-10-20
      • 2021-10-21
      • 2022-07-13
      相关资源
      最近更新 更多