[英]How to put conditions in switch statement
I am trying to write out a switch
statement with conditions stored in an array
.我正在尝试写出一个带有存储在array
中的条件的switch
语句。 The conditions are whether a score is between two numbers ('test')
.条件是score is between two numbers ('test')
。 If it is between those two numbers it issues a 'response'
.如果它在这两个数字之间,它会发出'response'
。
My expectation was that using the variable of 'score'
and grabbing the argument from the array
would use my 'response'
variable to output a message about the score
if the condition is met.我的期望是,如果满足条件,使用'score'
变量并从array
中获取参数将使用我'response'
变量来 output 一条关于score
的消息。 I can get the outputs for paragraph1
, however, I cannot get the output for 'response'
on paragraph two
in my switch statement
.我可以得到第 1 paragraph1
的输出,但是,我无法得到 output 用于我的switch statement
中paragraph two
的'response'
。
let section = document.querySelector('body');
let response;
let score = 85;
let test = [(score < 0 || score > 100), (score != Number || score == 0), (score > 0 || score < 19), (score > 19 || score < 39), (score > 39 || score < 69), (score > 69 || score < 89), (score > 90 || score <= 100)];
let machineActive = true;
// Add your code here
if (machineActive === false) {
alert('Your machine is not working');
}
for (let i of test) {
switch (score) {
case score[0]:
response = 'broken';
break;
case score[1]:
response = 'not a number';
break;
case score[3]:
response = '0-19';
break;
case score[4]:
response = '19-39';
break;
case score[5]:
response = '39-69';
break;
case score[6]:
response = '69-89';
break;
case score[7]:
response = '89-100';
break;
default:
response = 'If this is showing it is the default and not working';
}
}
// 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);
To write switch cases with range, you'll need to write something like below (showing case 1 and 3 as example below):要编写带范围的切换案例,您需要编写如下内容(以下示例显示案例 1 和 3):
switch (true) {
case ((score < 0 || score > 100)):
-- Execute case 1 ---
break;
case ((score > 0 || score < 19)):
-- Execute case 3 ---
break;
default:
-- Execute default case ---
break;
}
Note : I wrote this answer before seeing in the comments that using a
switch
statement was a requirement for a JS challenge.注意:我在评论中看到使用switch
语句是 JS 挑战的要求之前写了这个答案。 But I'll leave it here anyway但无论如何我都会把它留在这里
switch
is not really appropriate for what you are trying to do. switch
并不适合您尝试做的事情。 Here is how I would do it:这是我的做法:
const section = document.querySelector('body'); const score = 85; const conditions = [ { test: s => isNaN(s), response: 'not a number' }, { test: s => (s < 0 || s > 100), response: 'broken' }, { test: s => s < 19, response: '0-18' }, { test: s => s < 39, response: '19-38' }, { test: s => s < 69, response: '39-68' }, { test: s => s < 89, response: '69-88' }, { test: s => true, response: '89-100' } ]; const response = conditions.find(cond => cond.test(score)).response; // 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);
there's no point in turning these conditions into an array.将这些条件转换为数组是没有意义的。 you are over-complicating things.你把事情复杂化了。 just do this:这样做:
if(score < 0 || score > 100) {
return 'broken';
} else if (score != Number || score == 0) {
return 'not a number'
} else if (score > 19 || score < 39){
return '20-38'
}
etc... ETC...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.