Name: Man Tran
ID: 991756840
"use strict";
function task1(){
/* Variable Declaration and Data Types (2 marks)
Declare and initialize variables for the following data types:
String
Number (integer and float)
Boolean
Array */
let string1 = "Hello World !"; //String
let number1 = 13; //integer
let number2 = 8.5; //float
let boolean = true //boolean
let array = ("option 1", "option 2", "option 3"); //array
}
function calculateArea(){ //task 2
/*Functions (3 marks)
Create a function called calculateArea that takes the radius of a circle as a parameter and returns the area of the circle. Use
the formula: area = π * r^2 (where π is approximately 3.14159).
Create another function called reverseString that takes a string as a parameter and returns the reversed string.
*/
const pi = 3.14159;
const radiusInput = document.getElementById('radius');
const radius = parseFloat(radiusInput.value);
if (!isNaN(radius) && radius > 0){
const circle_area = pi * Math.pow(radius,2);
console.log("The area of the circle is: " + circle_area);
}
else{
console.log("Please enter a valid number!")
}
}
function reverseString(){
//Create another function called reverseString that takes a string as a parameter and returns the reversed string.
const stringInput = document.getElementById('string');
const string = stringInput.value;
const reversed_string = string.split("").reverse().join("");
console.log("Reversed String: " + reversed_string)
}
function gradeCalculator(){ //task 3
/* Write a function called gradeCalculator that takes a numerical score as a parameter and returns a letter grade based on the
following scale:
90-100: A
80-89: B
70-79: C
60-69: D
0-59: F
*/
const scoreInput = document.getElementById('score');
const score = parseFloat(scoreInput.value);
if (score >= 90 && score <= 100){
console.log(`${score} is A`);
}
else if (score >= 80 && score <= 89){
console.log(`${score} is B`);
}
else if (score >= 70 && score <= 79){
console.log(`${score} is C`);
}
else if (score >= 60 && score <= 69){
console.log (`${score} is D`);
}
else if (score >= 0 && score <= 59){
console.log (`${score} is F`);
}
else{
console.log (`Your grade should be a positive number`);
}
}