Mada za sehemu hiiImplementing web scriptingMada 4
- Implementing variables
- Implementing selection, loop, and array
- Implementing basics of functions
- Implementing event handling
Selection, loops, and arrays are three fundamental building blocks that allow a program to make decisions, repeat actions, and store collections of data.
Selection statements let the program choose different actions based on conditions. The main types are if, else, and switch.
The if Statement
The if statement checks a condition. If the condition is true, the code inside the block runs.
let score = 75;
if (score >= 50) {
console.log("You passed!");
}
The if-else Statement
When you want one action if the condition is true and a different action if it is false, use else.
let temperature = 30;
if (temperature > 35) {
console.log("It is very hot today.");
} else {
console.log("The weather is okay.");
}
The switch Statement
When you have many conditions to check against one value, switch is often clearer than many if statements.
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Loops repeat a block of code multiple times. This saves you from writing the same code over and over.
The for Loop
Use a for loop when you know in advance how many times to repeat.
for (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}
The three parts are:
- Initialization:
let i = 1— sets the starting point - Condition:
i <= 5— keeps the loop running while true - Update:
i++— changes the counter each time
The while Loop
Use while when you do not know exactly how many times to repeat, but you know the condition to stop.
let price = 1000;
while (price > 0) {
console.log("Remaining: " + price);
price = price - 200;
}
The do-while Loop
This loop runs the code first, then checks the condition. It always runs at least once.
let marks = 0;
do {
console.log("Mark: " + marks);
marks++;
} while (marks < 3);
An array stores multiple values in a single variable. Each value has a position called an index, starting from 0.
let prices = [500, 1200, 300, 800];
console.log(prices[0]); // Output: 500
console.log(prices[2]); // Output: 300
You can also loop through an array using a for loop:
let items = ["Rice", "Sugar", "Salt", "Oil"];
for (let i = 0; i < items.length; i++) {
console.log(items[i]);
}
Problem: A small shop in Arusha tracks the prices of five items. Write a program that:
- Stores the prices in an array
- Displays each price
- Shows "Expensive" if the price is more than 1000 shillings, otherwise shows "Affordable"
let prices = [500, 1500, 800, 2000, 300];
for (let i = 0; i < prices.length; i++) {
console.log("Item " + (i + 1) + ": " + prices[i]);
if (prices[i] > 1000) {
console.log(" -> Expensive");
} else {
console.log(" -> Affordable");
}
}
Output:
Item 1: 500
-> Affordable
Item 2: 1500
-> Expensive
Item 3: 800
-> Affordable
Item 4: 2000
-> Expensive
Item 5: 300
-> Affordable
This example combines all three concepts: selection (if-else), loops (for), and arrays.
In Tanzania, market vendors can use these programming concepts to manage their small businesses. For example, a fruit seller at Mlimani City market can write a program that stores the prices of different fruits in an array, loops through each price to check if it is above or below a certain amount, and then prints a report showing which fruits are expensive and which are affordable — helping them decide how to set their prices for the day.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Apply conditional statements (if-else, switch).
- Use loops (for, while, do-while).
- Declare and manipulate arrays.
Tathmini ya matokeo
Selection, loop and array are correctly implemented in scripts to perform the required tasks.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to:
- implement conditions, loops, and arrays.
Principles: The student should explain the principle of:
- control flow and iteration.
Theories: The student should explain:
- The concepts of flow control and arrays
- use of arrays in storing and accessing data. Circumstantial knowledge:
Detailed knowledge about:
- array methods
Swali
What is the index of the first element in a JavaScript array?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado