From 6473425eb0b0e298a50a9fffc51e5733696b2de1 Mon Sep 17 00:00:00 2001 From: Jaspreet Date: Sun, 1 Jan 2023 02:34:47 -0600 Subject: [PATCH] Cont'd Day1, completed conditional exercises, will start reading on loops next --- solutions/day-01/conditionals/index.html | 15 ++++++ solutions/day-01/conditionals/level1.js | 35 ++++++++++++++ solutions/day-01/conditionals/level2.js | 60 ++++++++++++++++++++++++ solutions/day-01/conditionals/level3.js | 33 +++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 solutions/day-01/conditionals/index.html create mode 100644 solutions/day-01/conditionals/level1.js create mode 100644 solutions/day-01/conditionals/level2.js create mode 100644 solutions/day-01/conditionals/level3.js diff --git a/solutions/day-01/conditionals/index.html b/solutions/day-01/conditionals/index.html new file mode 100644 index 0000000..2513e0a --- /dev/null +++ b/solutions/day-01/conditionals/index.html @@ -0,0 +1,15 @@ + + + + + + + Conditionals + + + + + + + + \ No newline at end of file diff --git a/solutions/day-01/conditionals/level1.js b/solutions/day-01/conditionals/level1.js new file mode 100644 index 0000000..0704cf8 --- /dev/null +++ b/solutions/day-01/conditionals/level1.js @@ -0,0 +1,35 @@ +/*Get user input using prompt('Enter your age:'). If user is 18 or older , +give feedback:'You are old enough to drive' but if not 18 give another feedback +stating to wait for the number of years he needs to turn 18. */ +let userInput = prompt('Enter your age:'); +if(userInput >= 18){ + console.log('You are old enough to drive.'); +}else{ + let years = 18 - userInput; + console.log(`You must wait ${years} ${(years == 1) ? 'year' : 'years'} until you can drive.`); +} + +/*Compare the values of myAge and yourAge using if … else. Based on the comparison +and log the result to console stating who is older (me or you). +Use prompt('Enter your age:') to get the age as input. */ +let myAge = parseInt(prompt('User1 enter your age:')); +let yourAge = parseInt(prompt('User2 enter your age:')); +console.log((myAge > yourAge) ? `User1(Age:${myAge}) is older than User2(Age:${yourAge})` + : `User2(Age:${yourAge}) is older than User1(Age:${myAge})`); + +// If a is greater than b return 'a is greater than b' else 'a is less than b'. Try to implement it in two ways +let a = 4, b = 3; +// using if else +if(a > b){ + console.log(`${a} is greater than ${b}`); +}else{ + console.log(`${a} is less than ${b}`); +} +// ternary operator. +console.log((a > b) ? `${a} is greater than ${b}` : `${a} is less than ${b}`); + +/* Even numbers are divisible by 2 and the remainder is zero. +How do you check, if a number is even or not using JavaScript?*/ +let num = prompt('Enter a number:'); +console.log((num % 2 == 0) ? `${num} is an even number` : `${num} is an odd number`); + diff --git a/solutions/day-01/conditionals/level2.js b/solutions/day-01/conditionals/level2.js new file mode 100644 index 0000000..2325b32 --- /dev/null +++ b/solutions/day-01/conditionals/level2.js @@ -0,0 +1,60 @@ +/*Write a code which can give grades to students according to theirs scores: +90-100 = A, 70-89 = B, 60-69 = C, 50-59 = D, 0-49 = F */ +let score = parseInt(prompt('Enter your score:')); +// When using switch(true), an expression in a case will be evaluated before matching. +// If the expression in your case evaluates to true - it will be matched. +switch(true) { + case score >= 90: console.log(`You score is an 'A'!`); + break; + case score >= 70 && score < 90: console.log(`You score is a 'B'!`); + break; + case score >= 60 && score < 70: console.log(`You score is a 'C'!`); + break; + case score >= 50 && score < 60: console.log(`You score is a 'D'!`); + break; + case score <= 49: console.log(`You score is a 'F'!`); + break; + default: + console.log('Error, please enter a valid score!'); + break; +} + +/*Check if the season is Autumn, Winter, Spring or Summer. If the user input is : +September, October or November, the season is Autumn. +December, January or February, the season is Winter. +March, April or May, the season is Spring +June, July or August, the season is Summer*/ +let month = prompt('Enter a month:'); +month = month.charAt(0).toUpperCase() + month.slice(1).toLowerCase(); // capitalize the given month and lower case the rest of the letters +switch (true) { + case month == 'September' || month == 'October' || month == 'November': + console.log(`The season during the month of ${month} is Autumn.`); + break; + case month == 'December' || month == 'January' || month == 'February': + console.log(`The season during the month of ${month} is Winter.`); + break; + case month == 'March' || month == 'April' || month == 'May': + console.log(`The season during the month of ${month} is Spring.`); + break; + case month == 'June' || month == 'July' || month == 'August': + console.log(`The season during the month of ${month} is Summer.`); + break; + default: + console.log('Error, please enter a valid month!'); + break; +} + +// Check if a day is weekend day or a working day. Your script will take day as an input. +let day = prompt('What is the day today?'); +day = day.charAt(0).toUpperCase() + day.slice(1).toLowerCase(); // uppercase first letter of day and lowercase rest of letters +switch (true) { + case day == 'Saturday' || day == 'Sunday': + console.log(`${day} is a weekend day.`); + break; + case day == 'Monday' || day == 'Tuesday' || day == 'Wednesday' || day == 'Thursday' || day == 'Friday': + console.log(`${day} is a working day.`); + break; + default: + console.log(`${day} is a not a valid day, please enter a day!`); + break; +} \ No newline at end of file diff --git a/solutions/day-01/conditionals/level3.js b/solutions/day-01/conditionals/level3.js new file mode 100644 index 0000000..e394cd0 --- /dev/null +++ b/solutions/day-01/conditionals/level3.js @@ -0,0 +1,33 @@ +// Write a program which tells the number of days in a month. and considers leap year +let monthInput = prompt('Enter a month:'); +monthInput = monthInput.charAt(0).toUpperCase() + monthInput.slice(1).toLowerCase(); +let yr = new Date().getFullYear(); // get year so its dynamic and never hardcoded in +switch (true) { + case monthInput == 'January': console.log(new Date(yr,1,0).getDate()); + break; + case monthInput == 'February': console.log(new Date(yr,2,0).getDate()); + break; + case monthInput == 'March': console.log(new Date(yr,3,0).getDate()); + break; + case monthInput == 'April': console.log(new Date(yr,4,0).getDate()); + break; + case monthInput == 'May': console.log(new Date(yr,5,0).getDate()); + break; + case monthInput == 'June': console.log(new Date(yr,6,0).getDate()); + break; + case monthInput == 'July': console.log(new Date(yr,7,0).getDate()); + break; + case monthInput == 'August': console.log(new Date(yr,8,0).getDate()); + break; + case monthInput == 'September': console.log(new Date(yr,9,0).getDate()); + break; + case monthInput == 'October': console.log(new Date(yr,10,0).getDate()); + break; + case monthInput == 'November': console.log(new Date(yr,11,0).getDate()); + break; + case monthInput == 'December': console.log(new Date(yr,12,0).getDate()); + break; + default: + console.log('Error, please enter a valid month!') + break; +} \ No newline at end of file