满足条件时如何停止其余代码

时间:2020-06-27 17:49:54

标签: javascript

这是Mozilla进行的一项测试,测试如果打开了计算机则运行代码块,如果关闭则不运行代码块。我能够编写分数变量的代码,但是,如果'machineActive'变量为'false,我不知道如何停止其余代码的运行。

我只能找到“中断”;用于switch语句。如何在if / else if / else运算符中执行此操作?

let section = document.querySelector('body');

let response;
let score = '75';
let machineActive = false;


// Add your code here


if (machineActive === false) {
    alert('Your machine is not working');
} else if (machineActive === true) {
    alert('The machine is turned on, you can play!\nClick the ok button to continue ');
}
if (score < 0 || score > 100) {
    response = 'That is not possible!';
} else if (score > 0 && score < 19) {
    response = 'That was a terrible score -- Total Fail!';
} else if (score > 20 && score < 39) {
    response = 'You know some things, but its a pretty bad score';
} else if (score > 40 && score < 69) {
    response = 'You did a passable job, not bad!';
} else if (score > 70 && score < 89) {
    response = `That's a great score, you really knew your stuff!`;
} else if (score > 90 && score <= 100) {
    response = `What an amazing score! Did you cheat? Are you really a person?`;
} else if (score != Number) {
    response = 'Input is incorrect';
} else if (score === 0 || score === null) {
    response = 'Null / Undefined';
} else {
    'Error occured';
}


// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let para2 = document.createElement('p');

para1.textContent = `Your score is ${ score }`;
para2.textContent = response;

section.appendChild(para1);
section.appendChild(para2);

1 个答案:

答案 0 :(得分:0)

一种方法:

if (machineActive === false){
   //do something or don't
}
else{
   // we know that machineActive is true now
   // here you can nest your other if/else statements
}