0% found this document useful (0 votes)
14 views8 pages

Data Structure and Algorithm Works

The document contains a series of programming exercises that require rewriting code snippets in pseudocode. Each exercise presents a scenario, followed by a solution in JavaScript, and includes expected output for various conditions. The exercises cover topics such as determining if a number is positive, checking even or odd numbers, comparing two numbers, calculating grades, ticket pricing based on age, identifying leap years, applying shopping discounts, greeting based on time of day, calculating BMI, and creating a number guessing game.

Uploaded by

ndolikenny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Data Structure and Algorithm Works

The document contains a series of programming exercises that require rewriting code snippets in pseudocode. Each exercise presents a scenario, followed by a solution in JavaScript, and includes expected output for various conditions. The exercises cover topics such as determining if a number is positive, checking even or odd numbers, comparing two numbers, calculating grades, ticket pricing based on age, identifying leap years, applying shopping discounts, greeting based on time of day, calculating BMI, and creating a number guessing game.

Uploaded by

ndolikenny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercise

Rewrite the following codes in Pseudocode

Exercise #1 — Determine If a Number Is Positive

Scenario: Write a program that determines whether a given


number is positive or not.

Solution:

var number = 5;

if (number > 0) {
console.log("The number is positive.");
} else {
console.log("The number is not positive.");
}

Output:

The number is positive.

Exercise #2 — Check Even or Odd

Scenario: Write a program that checks if a number is even or


odd.

1
Solution:

var number = 7;
if (number % 2 === 0) {
console.log("The number is even.");
} else {
console.log("The number is odd.");
}

Output:

The number is odd.

Exercise #3 — Determine the Greater Number

Scenario: Write a program to determine the greater of two


numbers.

Solution:

var num1 = 10;


var num2 = 15;

if (num1 > num2) {


console.log("num1 is greater.");
} else {
console.log("num2 is greater.");
}

2
Output:

num2 is greater.

Exercise #4 — Grade Calculator

Scenario: Write a program that assigns a letter grade based


on a numerical grade.

Solution:

var score = 85;


var grade;

if (score >= 90) {


grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
console.log("Grade: " + grade);

Output:

Grade: B

3
Exercise #5 — Ticket Pricing

Scenario: Write a program that calculates the ticket price


based on age.

Solution:

var age = 25;


var ticketPrice;

if (age < 12) {


ticketPrice = 5;
} else if (age >= 12 && age < 18) {
ticketPrice = 10;
} else if (age >= 18 && age < 60) {
ticketPrice = 20;
} else {
ticketPrice = 15; // Senior citizen discount
}
console.log("Ticket Price: $" + ticketPrice);

Output:

Ticket Price: $20

Exercise #6 — Determine Leap Year

Scenario: Write a program that determines if a year is a leap


year.

Solution:
4
var year = 2024;

if (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
console.log(year + " is a leap year.");
} else {
console.log(year + " is not a leap year.");
}

Output:

2024 is a leap year.

Exercise #7 — Shopping Discount

Scenario: Write a program that calculates a discount based


on the purchase amount.

Solution:

var purchaseAmount = 120;


var discount;

if (purchaseAmount >= 100) {


discount = 20;
} else if (purchaseAmount >= 50) {
discount = 10;
} else {
discount = 0;
}
console.log("Discount: " + discount + "%");

5
Output:

Discount: 20%

Exercise #8 — Time of Day Greeting

Scenario: Write a program that greets the user based on the


time of day.

Solution:

var currentTime = new Date();


var currentHour = currentTime.getHours();
var greeting;

if (currentHour < 12) {


greeting = "Good morning!";
} else if (currentHour < 18) {
greeting = "Good afternoon!";
} else {
greeting = "Good evening!";
}
console.log(greeting);

Output (depending on the time of day you execute the code):

Good afternoon!

Exercise #9 — BMI Calculator

6
Scenario: Write a program that calculates the Body Mass
Index (BMI) and categorizes it.

Solution:

var weight = 70; // in kilograms


var height = 1.75; // in meters
var bmi = weight / (height * height);
var category;

if (bmi < 18.5) {


category = "Underweight";
} else if (bmi < 24.9) {
category = "Normal weight";
} else if (bmi < 29.9) {
category = "Overweight";
} else {
category = "Obese";
}
console.log("BMI: " + bmi.toFixed(2)); // .toFixed(2) limits the
number of decimals to 2
console.log("Category: " + category);

Output:

BMI: 22.86
Category: Normal weight

Exercise #10 — Number Guessing Game

Scenario: Write a simple number guessing game.

7
Solution:

var secretNumber = 7;
var guess = 5; // The player's guess, change this to see that
different code lines are executed based on the conditions
if (guess === secretNumber) {
console.log("Congratulations! You guessed the correct number.");
} else if (guess < secretNumber) {
console.log("Try a higher number.");
} else {
console.log("Try a lower number.");
}

Output (depending on the player’s guess):

Try a higher number.

You might also like