Question 9(b) 2020 · February-March · Paper 32 · 4 marks
Question 9
Part (b)
Question 9
In JavaScript, conditional statements can be used to carry out specific actions based upon different
conditions. The value of the variable ‘age’ is entered into a web page and collected by the HTML code of the page. The function in Fig. 9.1 uses the ‘if else’ and ‘else if’ conditional statements to test the value of the variable ‘age’. Line numbers are shown only for your convenience when referring to the code.
1 function myFunction() { 2 var statement; 3 var age = document.getElementById("age").value; 4 if (age < 10) { 5 statement = "You are not old enough to go to this school"; 6 } else if (age < 18) { 7 statement = "You can go to this school"; 8 } else { 9 statement = "You are too old to go to this school"; 10 }
Fig. 9.1
Part (b)
The code could have been written using the ‘switch’ function, e.g. lines 4 to 7 could have
been:
4 switch(true) { 5 case age < 10: 6 statement = "You are not old enough to go to this school"; 7 break;
with additional lines for the other conditions.
Explain the drawbacks of using the ‘switch’ function.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................
Question 15
Reveal official mark-scheme answer
Official answer for Question 9
Part (b)
Question 4
Why this question appears here
98% assigned-topic match · Programming for the web
Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.
Permanent source page: Educate A Change · From 9626_m20_qp_32.pdf
