Quiz
Find the Pure Function
// 1
function add(a, b) {
return a + b;
}
Find the Pure Function
// 2
const sum = 0;
function addToSum(a) {
sum + a;
}
Find the Pure Function
// 3
const rounding = true;
function divide(a, b) {
const result = a / b;
if (rounding) {
result = parseInt(result);
}
return result;
}
Find the Pure Function
// 4
function isTuesday() {
const today = new Date;
return today.getDay() === 2;
}
Find the Pure Function
// 5
function luckyNumber() {
return parseInt(Math.random() * 10);
}
Find the Pure Function
// 6
function add(a, b) {
const sum = a + b;
console.log('Sum is: ', sum);
return a + b;
}
Answers
The answer is 1.
2 is not a pure function because it makes changes to the value of external variable.
3 is not a pure function because it depends on the external state.
4 is not a pure function because it uses current time inside the function which is a global state.
5 is not a pure function because its result depends on random number. It will not generate the same result every time.
6 is not a pure function because it produces some side effect printing out the result.
Date Functions
Get which day is Today?
Date Functions
Get current year?
Date Functions
Get current month?
Date Functions
Write function which return remaining days in the year.
-
// Cuurent Time
const now = new Date();
// Start of the year
const start = new Date(now.getFullYear(), 0, 0);
// Differnce
const diff = now - start;
// One Day
const oneDay = 1000 * 60 * 60 * 24;
// Today's Day
const day = Math.floor(diff / oneDay);
// Remaining Day's in the year
const daysLeft = 365 - day;
console.log('Day of year: ' + daysLeft);
Quiz
By sarabs3
Quiz
- 382