Lab 2 – JavaScript Basics II
Instructions
• Implement each solution in its own file.
• The use of AI software engineering tools is not allowed.
Part A – Variables, Operators & Control Structures
1. Write a JS program that declares two variables, num1 and num2. Assigns num1 the
value 15 and num2 the value 4. Then, performs the following:
• calculates and prints their sum.
• calculates and prints their difference.
• calculates and prints their product.
• calculates and prints their quotient (division).
• calculates and prints the remainder when num1 is divided by num2.
2. Write a JS program that declares two boolean variables: isRaining and hasUmbrella.
Set isRaining to true and hasUmbrella to false. In your program, do the following:
• check if it is raining AND you have an umbrella, then print the result.
• check if it is raining OR you have an umbrella, then print the result.
• check if it is NOT raining, then print the result.
3. Write a program which determines whether a number is even or odd. If the number is
even, print the message “The number is even”, else print “The number is odd”.
4. Write a program that determines the grade category given a student’s score. The
categories are as follows:
• A: 90–100
• B: 80–89
• C: 70–79
• D: 60–69
• F: below 60
1
5. Write a program that takes the original price of a product, a discount percentage,
calculates the discount amount, and displays the final price after discount.
6. Write a program that awards a bonus based on years of service on salary. Calculate
the bonus and print it to the console. The bonus is applied in the following ways:
• 5+ years: 10% bonus
• 3–4 years: 5% bonus
• Less than 3: no bonus
7. Write a program that prints numbers from 20 down to 5.
8. Write a program that prints all numbers that are divisible by 3 and 5 between 0 and
100.
9. Given three numbers, write a program that determines the greatest number of the
three.
10. Write a program that converts a given number of minutes into the format “10 days,
4 hours, 32 minutes”. For example, 121 minutes should be formatted as “0 days, 2
hours, 1 minute(s)”.
11. Write a program that calculates the following:
• The sum of numbers between 1 and 1000
• The product of all numbers divisible by 5 between 0 and 100
12. Write a program which multiplies a variable by 10 only if it is a number; if it is not,
print an error message.
13. Write a program that counts how many even and odd numbers exist between two given
numbers. Given start = 1 and end = 10, the program should count all even and odd
numbers between them and print the results. The program should work when you
adjust the start and end values.
Part B – Functions
1. Implement a function that prints the message “hello, how are you?” to the console.
2. Implement a function that takes a parameter user and prints a message “hello, good
morning [user]”.
3. Implement functions that do the following:
• Take a radius parameter, calculate the area of a circle, and return it.
• Take length and width parameters of a rectangle, calculate the area, and return
it.
2
• Take the base and height of a right-angled triangle, calculate the hypotenuse,
and return it.
• Take three numbers, find the minimum, and return the result.
4. Create your own function called isNumber that takes one parameter and returns true
if that parameter is a number.
5. Write a function that validates whether two strings are identical; the function takes
the strings as parameters and returns a Boolean result. Make it case insensitive.
6. Create a function that takes an array of numbers, finds the max, min, mean, and
returns an array or objects containing the results.
7. Create a function that receives an array of numbers and finds the sum of all even
numbers it has.
8. Create a function that receives a sentence and returns the longest word in the sentence.
9. Write a function that receives a sentence and returns an object representing the fre-
quency of each character excluding a space.