Past-paper question · 2020 February-March · Paper 32 · Q9(b)

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.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

.............................................................................................................................................

[4]

Question 15

Reveal official mark-scheme answer Official mark-scheme answer for 9(B)

Official answer for Question 9

Part (b)

✓
Four from:

Question 4

✓
The order of Case/conditions checks in code must be logically
✓
correct/perfect
✓
…for correct /expected comparisons to be made
✓
(Switch) syntax does not follow the usual rules/colons not semi-colons so
✓
code is difficult/confusing to write/read
✓
Code can be lengthy as each condition has to be individually stated
✓
…this is repetitive and prone to error
✓
‘break’ has to be manually inserted after every ‘case’
✓
….debugging problems/difficulties with ‘nested’ conditions
✓
’default’ condition should be included to catch/trap unexpected conditions.
✓
Question
✓
Answer
✓
Marks

Mark scheme 9626_m20_ms_32.pdf →

Topic relevance

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

Keep practising this topic