What will be printed out when you run the following code (choose 1)?
var n = 1;
switch (n) {
case 3: console.log(‘C’);
case 1: console.log(‘A’);
case 2: console.log(‘B’);
}
A. A
B. A
B
C. C
A
B
D. The code will not run because there are no break; statements
E. The code will not run because the cases are not in order
Actually cause n=1 it goes straight to case 1 and since there is no break it will continue to execute what ever lies beneath it which case 2. So I think the answer is A B ?
Thanks guys for your response. I have another question :
The user inputs the number and type of coffees that they require in text fields with the
ids ‘input1’ and ‘input2’ respectively and the page calculates the cost and
writes it to a text area with the id ‘result’. The functions getInput(),
setOutput() and formatMoney() are similar to many of the examples in the
course and can be expected to behave correctly.
function react() {
'use strict';
var number = getInput('input1');
if (number <= 0) {
alert('Please enter a positive number');
return false;
}
var cost = 0;
var type = document.getElementById('input2').value;
switch (type) {
case 'Latte':
cost = 2.0;
break;
case 'Cappuccino':
cost = 1.8;
break;
case 'FlatWhite':
cost = 1.5;
break;
default:
setOutput('Coffee ' + type + ' not recognised');
return false;
}
if (number > 5) { // discount
cost -= 0.2;
}
cost *= number;
setOutput('That will be ' + formatMoney(cost));
return false;
}
What is the output if the user enters “5” and “latte” in the number and type text
fields (choose 1)?
A. There will be a run-time error
B. That will be £10.00
C. That will be £9.00
D. That will be £5.00
E. Coffee latte not recognised
What is the output if the user enters “10” and “Latte” in the number and type
text fields (choose 1)?
A. There will be a run-time error
B. That will be £20.00
C. That will be £18.00
D. That will be £10.00
E. Coffee Latte not recognised
Would the answer be be D That will be £5.00 and the answer C £18.00. I’m not entirely sure at how to explain this but could someone help me explain it thanks!
well firstly, cause Latte is in the form of a case and it says cost =2.0, so if we enter 5 it will be £5.00. As for £18 i took a wild guess. I have no idea otherwise
basically you have all the types and then i the output they are not recognized. Then the user creates a new field called latte and types in 10 since 10 is greater than 5 it does 10 minus 0.2= 9.8 and then after that I dont really know where to go. There may be a few guesses i have to make as i am a beginner and they are testing us on this stuff already which is early.