// 1
function add(a, b) {
return a + b;
}
// 2
const sum = 0;
function addToSum(a) {
sum + a;
}
// 3
const rounding = true;
function divide(a, b) {
const result = a / b;
if (rounding) {
result = parseInt(result);
}
return result;
}
// 4
function isTuesday() {
const today = new Date;
return today.getDay() === 2;
}
// 5
function luckyNumber() {
return parseInt(Math.random() * 10);
}
// 6
function add(a, b) {
const sum = a + b;
console.log('Sum is: ', sum);
return a + b;
}
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.
Get which day is Today?
Get current year?
Get current month?
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);