我知道这可能是一个简单的循环,但无法想到它.
我有10个分数,我需要通过确保它们在0和1之间(大量小数)来验证它们.
输入非常宽松,因此空白,空,字母数字值可以在那里.
现在我只是
if (!(score1>=0 && score1<=1)){var result="error"} else
if (!(score2>=0 && score2<=1)){var result="error"} else
if (!(score3>=0 && score3<=1)){var result="error"} ...
Run Code Online (Sandbox Code Playgroud)
也许不是最优雅的格式,但是 - 必须有一种方法来循环这个,对吗?
只需使用每个MDN,并将您的数字放在一个数组中.
var score1 = 0.89;
var score2 = 0.75;
var score3 = 0.64;
var booleanResult = [score1,score2,score3].every(s => s >= 0 && s<= 1);
console.log(booleanResult);Run Code Online (Sandbox Code Playgroud)
这个答案使用箭头功能:
或者,这是使用每个经典函数回调的示例
var score1 = 0.89;
var score2 = 0.75;
var score3 = 0.64;
var booleanResult = [score1,score2,score3].every(function(s){ return s >= 0 && s<= 1 });
console.log(booleanResult);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |