🟡 JavaScript Practice Session — Test
Preparation
Purpose: Prepare for Monday’s live coding test.
Students should practice all 10 problems.
During the test:
● One student will come to the laptop
● Solve one random problem
● Write the solution live
So they must understand all questions, not just memorize code.
📁 Folder Setup
Create a simple project.
js-test-practice/
│
├── [Link]
└── [Link]
🧱 [Link]
<!DOCTYPE html>
<html>
<head>
<title>JS Test Practice</title>
</head>
<body>
<h1>JavaScript Test Practice</h1>
<button id="btn">Click Me</button>
<p id="output"></p>
<script src="[Link]"></script>
</body>
</html>
All code will go inside [Link].
🟢 Question 1 — Find Even Numbers
Task
Given this array:
const numbers = [12, 5, 8, 130, 44, 9, 20];
Create a new array that only contains even numbers.
Print the new array.
Solution
const numbers = [12, 5, 8, 130, 44, 9, 20];
const evenNumbers = [Link](function(num) {
return num % 2 === 0;
});
[Link]("Even numbers:", evenNumbers);
Concepts:
● filter()
● modulus operator
🟢 Question 2 — Count Vowels in a String
Task
Write a function that counts vowels in a word.
Example:
countVowels("javascript")
Output:
Solution
function countVowels(word) {
const vowels = "aeiou";
let count = 0;
for (let i = 0; i < [Link]; i++) {
if ([Link](word[i])) {
count++;
}
}
return count;
}
[Link](countVowels("javascript"));
Concepts:
● loops
● string methods
● conditions
🟢 Question 3 — Remove Duplicates from
Array
Task
Given:
[1,2,3,2,4,5,3,6]
Return an array without duplicates.
Solution
const numbers = [1,2,3,2,4,5,3,6];
const uniqueNumbers = [];
for (let i = 0; i < [Link]; i++) {
if () {
[Link](numbers[i]);
}
}
[Link](uniqueNumbers);
Concepts:
● includes()
● loops
● array manipulation
🟢 Question 4 — Total Price of Products
Task
Given:
const products = [
{name:"Phone", price:500},
{name:"Laptop", price:1500},
{name:"Mouse", price:50}
];
Calculate the total price of all products.
Solution
const products = [
{name:"Phone", price:500},
{name:"Laptop", price:1500},
{name:"Mouse", price:50}
];
let total = 0;
for (let i = 0; i < [Link]; i++) {
total += products[i].price;
}
[Link]("Total price:", total);
Concepts:
● objects inside arrays
● accessing properties
🟢 Question 5 — Reverse a String
Task
Write a function that reverses a string.
Example:
reverseString("hello")
Output:
olleh
Solution
function reverseString(str) {
return [Link]("").reverse().join("");
}
[Link](reverseString("hello"));
Concepts:
● split()
● reverse()
● join()
🟢 Question 6 — Find Average Marks
Task
Given:
const marks = [80, 90, 70, 85, 95];
Calculate and print the average marks.
Solution
const marks = [80, 90, 70, 85, 95];
let total = 0;
for (let i = 0; i < [Link]; i++) {
total += marks[i];
}
const average = total / [Link];
[Link]("Average marks:", average);
Concepts:
● loops
● math logic
🟢 Question 7 — Capitalize First Letter
Task
Create a function that capitalizes the first letter.
Example:
capitalize("javascript")
Output:
Javascript
Solution
function capitalize(word) {
return [Link](0).toUpperCase() + [Link](1);
}
[Link](capitalize("javascript"));
Concepts:
● charAt
● toUpperCase
● slice
🟢 Question 8 — Button Click Counter
Task
Each time the button is clicked, increase a counter and display it.
Example:
Button clicked 1 times
Button clicked 2 times
Solution
let count = 0;
const button = [Link]("btn");
const output = [Link]("output");
[Link]("click", function() {
count++;
[Link] = "Button clicked " + count + " times";
});
Concepts:
● DOM
● events
● state variable
🟢 Question 9 — Find Largest Number
Task
Write a function that finds the largest number in an array.
Example:
[3,8,2,10,5]
Output:
10
Solution
function findLargest(arr) {
let largest = arr[0];
for (let i = 1; i < [Link]; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
[Link](findLargest([3,8,2,10,5]));
Concepts:
● comparisons
● loops
🟢 Question 10 — Shopping Cart Total
with reduce()
Task
Given:
const cart = [200,150,300,100];
Calculate the total using reduce().
Solution
const cart = [200,150,300,100];
const total = [Link](function(acc, price) {
return acc + price;
}, 0);
[Link]("Cart total:", total);
Concepts:
● reduce
● accumulator pattern
🟡 Practice Question 11 — Student Report
System
This question prepares them directly for the Student Analyzer, Word Counter thinking, and
Cart logic.
📝 Question
You are given an array of students with their marks.
const students = [
{ name: "Rahul", marks: [70, 80, 90] },
{ name: "Sneha", marks: [60, 75, 85] },
{ name: "Arjun", marks: [90, 88, 92] }
];
Tasks
Write code that:
1. Calculate the total marks for each student
2. Print the result like this:
Rahul total marks: 240
Sneha total marks: 220
Arjun total marks: 270
3. Find the student with the highest total marks
Print:
Top Student: Arjun
✅ Solution
const students = [
{ name: "Rahul", marks: [70, 80, 90] },
{ name: "Sneha", marks: [60, 75, 85] },
{ name: "Arjun", marks: [90, 88, 92] }
];
let highestMarks = 0;
let topStudent = "";
for (let i = 0; i < [Link]; i++) {
let total = 0;
for (let j = 0; j < students[i].[Link]; j++) {
total += students[i].marks[j];
}
[Link](students[i].name + " total marks: " + total);
if (total > highestMarks) {
highestMarks = total;
topStudent = students[i].name;
}
}
[Link]("Top Student:", topStudent);
🟡 Practice Question 12 — Word
Frequency Finder
This question prepares students for the Word Frequency Counter in the test.
📝 Question
You are given a sentence:
const sentence = "apple banana apple orange banana apple";
Tasks
1. Convert the sentence into an array of words using split().
2. Count how many times each word appears.
3. Store the result in an object.
4. Print the final object.
Expected output:
{
apple: 3,
banana: 2,
orange: 1
}
✅ Solution
const sentence = "apple banana apple orange banana apple";
const words = [Link](" ");
const wordCount = {};
for (let i = 0; i < [Link]; i++) {
const word = words[i];
if (wordCount[word]) {
wordCount[word]++;
} else {
wordCount[word] = 1;
}
}
[Link](wordCount);