Get Weekdays based on input number
//Get weekdays
//if you pass 0 --> Sunday
//if you pass 1 --> Monday and so on...if 8 --> undefined ...
const Weekdays = {
Sunday: 0,
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
};
Object.freeze(Weekdays);
//you cannot add new properties nor you can alter the existing properties...
console.log(`%c ${Weekdays}`, "color:red;font-size:20px");
console.log(`%c ${Weekdays["Wednesday"]}`, "color:red;font-size:20px");
//Getting key based on value .. if id is 4--> so if value is 4 --> Output should be -->Thursday
const getWeekdays = (id) => {
let day = Object.keys(Weekdays).find((key) => Weekdays[key] === id);
console.log(`%c ${day}`, "color:red;font-size:20px");
};
getWeekdays(4); //Thursday
getWeekdays(14); //undefined
WRITE IT IN ONE LINE - Object.Freeze({})
THIS CAN BE ALSO LIKE - HOW TO CREATE ENUM IN JAVASCRIPT
const Weekdays = Object.freeze({
Sunday: 0,
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
});
Get Weekdays based on input number- Key-value is swapped
const Weekdays = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
};
Object.freeze(Weekdays);
console.log(`%c ${Weekdays[2]}`, "color:red;font-size:20px");
const getWeekdays = (id) => {
console.log(`%c ${Weekdays[id]}`, "color:red;font-size:20px");
};
getWeekdays(2); //Tuesday
getWeekdays(12); //undefined
Using Switch Statement - direct return
const getWeekdays = (id) => {
switch (id) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
break;
}
};
console.log(`%c ${getWeekdays(3)}`, "color:red;font-size:20px");
Switch Statement - using break
const getWeekdays = (id) => {
let weekday = "";
switch (id) {
case 0:
weekday = "Sunday";
break;
case 1:
weekday = "Monday";
break;
case 2:
weekday = "Tuesday";
break;
case 3:
weekday = "Wednesday";
break;
case 4:
weekday = "Thursday";
break;
case 5:
weekday = "Friday";
break;
case 6:
weekday = "Saturday";
break;
default:
break;
}
return weekday;
};
console.log(`%c ${getWeekdays(3)}`, "color:red;font-size:20px");
Divisible by 3 -FIZZ, Divisible by 5 -BUZZ,
Divisible by 3 and 5 - FIZZBUZZ
FIRST SOLUTION
const fizbuzz = () => {
for (let i = 1; i <= 100; i++) {
if (i % 5 === 0 && i % 3 === 0) console.log(i + "FIZZBUZZ");
else if (i % 3 === 0) console.log(i + "FIZZ");
else if (i % 5 === 0) console.log(i + "BUZZ");
else console.log(i);
}
};
fizbuzz();
SECOND SOLUTION
const fizbuzz = () => {
for (let i = 1; i <= 100; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log(i + "FIZZBUZZ");
i++;
}
if (i % 3 === 0) {
console.log(i + "FIZZ");
i++;
}
if (i % 5 === 0) {
console.log(i + "BUZZ");
} else {
console.log(i);
}
}
};
fizbuzz();
GCD of two numbers
const gcd = (a, b) => {
let smallnum = 0;
let bignum = 0;
a > b ? ([bignum, smallnum] = [a, b]) : ([bignum, smallnum] = [b, a]);
for (let i = smallnum; i >= 1; i--) {
if (bignum % i === 0 && smallnum % i === 0) {
return i;
}
}
};
let c = gcd(15, 5);
console.log(c);
//output:- 5
Max and Min Sum
//QUESTION
//for 1 --> skip 1 -->Add other elements -->2+5+8+3=18
//for 2 --> skip 2 -->Add other elements -->1+5+8+3=17
//for 5 --> skip 5 -->Add other elements -->1+2+8+3=14
//for 8 --> skip 8 -->Add other elements -->1+2+5+3=11
//for 3 --> skip 3 -->Add other elements -->1+2+5+8=16
//Output Needed is as below
//Maximum Sum:- 18
//Mininum Sum:- 11
//SOLUTION
const number = [1, 2, 5, 8, 3];
const [minSum, maxSum] = [Math.min(...number), Math.max(...number)];
const finalSum = number.reduce((acc, curr) => acc + curr);
console.log("Maximum Sum:-", finalSum - minSum);
console.log("Mininum Sum:-", finalSum - maxSum);