21 Programming for the web (3)
Resources |
Revision Questions |
Information Technology IT
Login to see all questions
Click on a question to view the answer
1.
Write JavaScript code using a do-while
loop to generate a sequence of numbers from 1 to 5. The loop should execute at least once, regardless of the initial condition. Also, calculate the sum of these numbers within the loop and display the final sum after the loop completes.
let sum = 0;
let i = 1;
do {
sum += i;
i++;
} while (i
Explanation:
- The
do-while
loop first executes the code block and then checks the condition. - The code block adds the current value of
i
to the sum
variable. - The
i
variable is then incremented. - The loop continues as long as
i
is less than or equal to 5. - Finally, the calculated
sum
is displayed.
The do-while
loop is suitable when you need to ensure that the loop body is executed at least once.
2.
Explain the difference between the setTimeout() and setInterval() functions in JavaScript. Provide a practical example demonstrating the use of each function and clearly state the intended outcome of each.
setTimeout() executes a provided function or piece of code once after a specified delay (in milliseconds). setInterval() executes a provided function or piece of code repeatedly at a fixed time interval (in milliseconds). The interval continues until clearInterval() is called to stop it.
setTimeout() Example:
let message = "Hello after 2 seconds!";
setTimeout(function() {
alert(message);
}, 2000);
// Outcome: After 2 seconds, a pop-up alert will display "Hello after 2 seconds!".
setInterval() Example:
let counter = 0;
let intervalId = setInterval(function() {
counter++;
console.log("Counter value: " + counter);
}, 1000);
// Outcome: Every 1 second, the counter value will be logged to the console. The interval will continue indefinitely until clearInterval() is called.
// To stop the interval: clearInterval(intervalId);
3.
Question 2: Describe the difference between inline, internal, and external JavaScript. Discuss the advantages and disadvantages of each approach, considering factors such as code organisation, maintainability, and browser performance.
JavaScript can be included in an HTML document in three different ways:
- Inline JavaScript: JavaScript code is embedded directly within an HTML element using the
onclick
, onmouseover
, etc., attributes. Advantages: Simple for small snippets of code. Disadvantages: Poor code organization, difficult to maintain, increases HTML file size, and can negatively impact browser performance due to parsing overhead.
- Internal JavaScript: JavaScript code is placed within a
tag inside the
or
section of the HTML document.Advantages: Better code organization than inline, all JavaScript code is in one file. Disadvantages: Can still increase HTML file size, and the code is not easily reusable across multiple pages.
- External JavaScript: JavaScript code is stored in a separate .js file and linked to the HTML document using the
tag.Advantages: Best code organization, promotes reusability, reduces HTML file size, and improves browser performance as the browser can cache the .js file. Disadvantages: Requires an extra HTTP request to load the .js file.
Generally, external JavaScript is the preferred approach due to its advantages in code organization, maintainability, and performance.