JavaScript Tasks (Multiplication, Half of the sum, Find the perimeter and area, BMI (Body Mass Index), older than 16 years)

Task 1: Multiplication

Objective: Display the result of multiplying two numbers.

Code Solution:

const a = 3;
const b = 6;
const multiplicationResult = a * b;
console.log("Multiplication Result:", multiplicationResult);

Explanation: This code multiplies a and b, then prints the result.

Task 2: Half of the Sum

Objective: Display half of the sum of two numbers.

Code Solution:

const a = 3;
const b = 6;
const halfSum = (a + b) / 2;
console.log("Half of the sum:", halfSum);

Explanation: The code adds a and b, divides the sum by 2, and displays the result.

Task 3: Perimeter and Area of a Rectangle

Objective: Calculate the perimeter and area of a rectangle.

Code Solution:

const length = 6;
const width = 3;
const perimeter = 2 * (length + width);
const area = length * width;
console.log("Perimeter:", perimeter);
console.log("Area:", area);

Explanation: This code calculates both perimeter and area for a rectangle with given sides.

Task 4: Calculate Body Mass Index (BMI)

Objective: Calculate BMI using the formula: BMI = weight (kg) / height (m)².

Code Solution:

function calculateBMI(weight, height) {
return weight / (height ** 2);
}
const weight = 70; // in kg
const height = 1.75; // in meters
const bmi = calculateBMI(weight, height);
console.log("BMI:", bmi);

Explanation: The function calculates BMI using weight and height and displays the result.

Task 5: Buy Ticket for Adult Movie Session

Objective: Allow buying a ticket only if the person is older than 16 years.

Code Solution:

function buyTicket(age) {
if (age > 16) {
console.log("You can buy a ticket for the adult session.");
} else {
console.log("You are too young for the adult session.");
}
}
// Example usage:
const userAge = 18;
buyTicket(userAge);

Explanation: This function checks the age and determines if the person can buy a ticket.

Comparison Table

Task Code Example Summary Expected Output
Multiplication Multiplies two numbers a and b. 18
Half of the Sum Calculates half of (a + b). 4.5
Perimeter and Area Calculates perimeter and area of a rectangle. Perimeter: 18, Area: 18
Calculate BMI Computes BMI based on weight and height. 22.86
Buy Ticket for Adult Session Checks if age is greater than 16 for ticket sale. "You can buy a ticket for the adult session."
Scroll to Top