30 Days Of JavaScript.3E
Day 3. Booleans, Operators, Date_Exercise
On
day 3
, I learned booleans, undefined, null from primitive data types, along with various operators, operator precendence, window methods, and date objects. It was fun learning these. Let’s start!
👟Level 1.
6) Figure out the result of the following expressions first without using console.log(). After you decide the result confirm it by using console.log().
xi. There is no ‘on’ in both dragon and python
console.log(!('dragon'.includes('on')) && !('python'.includes('on'))); //false
I don’t have to use two lines for comparing two words, I can just use ampersand operator(&&
). Notice the negation operator(!
) to express ‘no’ in ‘both’.
👟Level 2.
2) Prompt a user to enter side a, b, c of the triangle & calculate the perimeter(a+b+c) of triangle.
let sideA = prompt('Enter side A');
let sideB = prompt('Enter side B');
let sideC = prompt('Enter side C');
let perimeter = sideA + sideB + sideC;
console.log('The perimeter of the triangle is ${perimeter}.);
I typed 5, 4, 3 for each sides. The code seemed okay, but the result was 543, not 12. I searched google and found out that prompt always returns a string(or null). I learned how to change data types(casting) on Day 2.
let perimeter = Number(sideA) + Number(sideB) + Number(sideC);
⚠️5) Calculate the slope, x-intercept and y-intercept of y = 2x - 2.
To calculate the x-intercept I should plug in 0 for y and solve for x. To calculate the y-intercept I should plug in 0 for x and solve for y.
//Calculating the y-intercept
let x = 0;
let y = 2*x - 2;
console.log(y); //-2
//Calculating the x-intercept
let y = 0;
let x = y/2 + 2;
console.log(x); //2
Calculating the y-intercept was easy, because I had the function of y described by x. However, calulating the x-intercept wasn’t easy. I could just change the function like x = y/2 + 2, but I just wanted the computer to do the job. I got the value but I believe there’s a better way to solve this. I’ll come back with better solution later.
6) Slope is m = (y2-y1)/(x2-x1). Find the slope between point (2, 2) and point(6,10)
console.log((2-6)/(2-10)); //2
I wanted to make out a fancy expression, but I just did the plain calculation. You’re free to leave any comment about the code.
⚠️8) Calculate the value of y (y = x2 + 6x + 9). Try to use different x values and figure out at what x value y is 0.
I’ll think more about it.
12) Declare two variables myAge and yourAge and assign them initial values and myAge and yourAge.
let myAge = 52;
let yourAge = 23;
let diff = myAge - yourAge
diff > 0
? console.log(`You are ${diff} years younger than me.`)
: console.log(`You are ${Math.abs(diff)}) years older than me.`);
let diff = Math.abs(myAge - yourAge)
myAge > yourAge
? console.log(`You are ${diff} years younger than me.`)
: console.log(`You are ${diff}) years older than me.`);
I think the question wanted me to set just diff and use back-ticks to print the expression. I thought maybe I could use ternary operator, so I added some lines. The thing I learned is that I can put Math object
inside the ${}
. Though I want to learn how to set multiple conditions like if
, else if
, or else
.
13) Using prompt get the year the user was born and if the user is 18 or above allow the user to drive if not tell the user to wait a certain amount of years.
let birthYear = prompt('Enter the year of your birth.');
let diff = 2022 - Number(birthYear);
diff > 18
? console.log('You are ${diff} years old. You are old enough to drive.')
: console.log(`You are ${diff} years old. You will be allowed to drive after ${18-diff} years.`);
👟Level 3.
1) Create a human readable time format using the Date time object. The hour and the minute should be all the time two digits(7 hours should be 07 and 5 minutes should be 05)
YYYY-MM-DD HH:mm eg. 2020-01-02 07:05
- First approach:
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const date = now.getDate();
const hours = now.getHours();
const minutes = now.getMinutes();
month < 9
? console.log(`${year}-0${month}-${date} ${hours}:${minutes}`)
: console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
//2022-04-18 21:24
In this case, I figured out that the only number that needed ‘0’ in front of it was the month
, so I only made a condition with month
. If I want to create all-time working code, I think I should add more ternary operators.
- Second approach (X):
month < 9
? console.log(`${year}-0${month}-${date} ${hours}:${minutes}`)
: console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
date < 10
? console.log(`${year}-${month}-0${date} ${hours}:${minutes}`)
: console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
hours < 10
? console.log(`${year}-${month}-${date} 0${hours}:${minutes}`)
: console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
minutes < 10
? console.log(`${year}-${month}-${date} ${hours}:0${minutes}`)
: console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
At first I added all four conditions, but found out that when executed, there would be four different outputs that have different conditions by each.
- Final answer:
const now = new Date();
const year = now.getFullYear();
let month = now.getMonth() + 1;
let date = now.getDate();
let hours = now.getHours();
let minutes = now.getMinutes();
month < 9
// '0' + month also works too
? month = `0${month}`
: month = month;
date < 10
? date = `0${date}`
: date = date;
hours < 10
? hours = `0${hours}`
: hours = hours;
minutes < 10
? minutes = `0${minutes}`
: minutes = minutes;
console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
//2022-04-18 22:03
I first wrote 0
+${date}
but I realized it could be shorted to 0${date}
and it finally worked out.I wonder if there’s shorter or better way to write the code. If you have any idea, please comment below!
I came back from Day 10. I realized that I didn’t have to use ternary operator. I could just use if
conditional, to only add ‘0’ when the variable has one digit.
const now = new Date();
const year = now.getFullYear();
let month = now.getMonth() + 1;
let date = now.getDate();
let hours = now.getHours();
let minutes = now.getMinutes();
if (month < 9) {
month = `0${month}`;
}
if (date < 10) {
date = `0${date}`;
}
if (hours < 10) {
hours = `0${hours}`;
}
if (minutes < 10) {
minutes = `0${minutes}`;
}
console.log(`${year}-${month}-${date} ${hours}:${minutes}`);
// 2022-05-13 22:33
Leave a comment