当某个条件变为 True 时,有没有办法重新定义 "let" 值?
Is there a way to redefine a "let" value when a certain condition becomes True?
温柔点:
我是 Javascript 的新手,很好奇我将如何处理 运行 重新定义“let”值的 for 循环 [或建议更合适的不同循环]在循环中的条件变为 True 之后。当最后一个“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();
目前,脚本 returns 在 运行 之后产生以下结果(示例结果):
当 (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 < 0) 为真时:
calling
-7
didn't work
didn't work
didn't work
didn't work
10
重申一下,一旦最后一个条件变为真,我想将 const add_to
添加到 let 值 Value1
,开始 (Number_Left > 0)
和 (Number_Left == 0 && Value1 == Number_Returned)
再次检查条件。
让我知道是否可以更具体地说明我要完成的任务。提前致谢。
语法是 +=
而不是 =+
当你想添加值并设置它时
如果您更改:
,您似乎会得到想要的结果
Value1 =+ add_to;
至(注意 addition assignment operator):
Value1 += add_to;
为了执行我需要此代码执行的操作,我不得不重写它。它可能不是最好的编码,但它适用于我想要完成的事情:
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();
温柔点:
我是 Javascript 的新手,很好奇我将如何处理 运行 重新定义“let”值的 for 循环 [或建议更合适的不同循环]在循环中的条件变为 True 之后。当最后一个“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();
目前,脚本 returns 在 运行 之后产生以下结果(示例结果):
当 (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 < 0) 为真时:
calling
-7
didn't work
didn't work
didn't work
didn't work
10
重申一下,一旦最后一个条件变为真,我想将 const add_to
添加到 let 值 Value1
,开始 (Number_Left > 0)
和 (Number_Left == 0 && Value1 == Number_Returned)
再次检查条件。
让我知道是否可以更具体地说明我要完成的任务。提前致谢。
语法是 +=
而不是 =+
当你想添加值并设置它时
如果您更改:
,您似乎会得到想要的结果Value1 =+ add_to;
至(注意 addition assignment operator):
Value1 += add_to;
为了执行我需要此代码执行的操作,我不得不重写它。它可能不是最好的编码,但它适用于我想要完成的事情:
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();