Reputation: 291
I am trying to write out a switch
statement with conditions stored in an array
. The conditions are whether a score is between two numbers ('test')
. If it is between those two numbers it issues a '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. I can get the outputs for paragraph1
, however, I cannot get the output for 'response'
on paragraph two
in my switch statement
.
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);
Upvotes: 0
Views: 251
Reputation: 1880
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...
Upvotes: 0
Reputation: 25659
Note: I wrote this answer before seeing in the comments that using a
switch
statement was a requirement for a JS challenge. But I'll leave it here anyway
switch
is not really appropriate for what you are trying to do. 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);
Upvotes: 1
Reputation: 1453
To write switch cases with range, you'll need to write something like below (showing case 1 and 3 as example below):
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;
}
Upvotes: 3