Skip to content

Instantly share code, notes, and snippets.

@khalid32
Last active February 15, 2020 07:56
Show Gist options
  • Save khalid32/758be6aa6b7dde060f7fd8e0e69ea700 to your computer and use it in GitHub Desktop.
Save khalid32/758be6aa6b7dde060f7fd8e0e69ea700 to your computer and use it in GitHub Desktop.
From "You Don't Know JS Yet" Appendix B
function range(start, end){
if(end != undefined){
if(start <= end){
return getRange(start, end);
}else{
return 0;
}
}else{
return function innerFunction(innerVariable){
if(start <= innerVariable){
return getRange(start,innerVariable);
}else{
return 0;
}
}
}
function getRange(start, end){
let arr = [];
for(let i = start; i <= end; i++){
arr.push(i);
};
return arr;
}
}
range(3, 3); // [3]
range(3, 8); // [3,4,5,6,7,8]
range(3, 0); // []
var start3 = range(3);
var start4 = range(4);
start3(3); // [3]
start3(8); // [3,4,5,6,7,8]
start3(0); // []
start4(6); // [4,5,6]
const dayStart = "07:30";
const dayEnd = "17:45";
const getTimer = startTime => ({
Start: {
hour: +`${[...dayStart][0]}${[...dayStart][1]}`,
minute: +`${[...dayStart][[...dayStart].length - 2]}${[...dayStart][[...dayStart].length - 1]}`
},
End: {
hour: +`${[...dayEnd][0]}${[...dayEnd][1]}`,
minute: +`${[...dayEnd][[...dayEnd].length - 2]}${[...dayEnd][[...dayEnd].length - 1]}`
},
GivenTime: (() => {
if([...startTime].length == 4){
return {
hour: +`${[...startTime][0]}`,
minute: +`${[...startTime][[...startTime].length - 2]}${[...startTime][[...startTime].length - 1]}`
}
}else{
return {
hour: +`${[...startTime][0]}${[...startTime][1]}`,
minute: +`${[...startTime][[...startTime].length - 2]}${[...startTime][[...startTime].length - 1]}`
}
}
})(),
})
function scheduleMeeting( startTime, duration ){
const time = getTimer(startTime);
if(time.GivenTime.hour == time.Start.hour){
if(time.GivenTime.minute >= time.Start.minute){
return true;
}else{
return false;
}
}else if(time.GivenTime.hour > time.Start.hour && time.GivenTime.hour < time.End.hour){
return true;
}else if(time.GivenTime.hour == time.End.hour){
// console.log("I'm here!");
if((time.GivenTime.minute + duration) <= time.End.minute){
// console.log(time.GivenTime.minute + duration);
return true;
}else{
// console.log(time.GivenTime.minute + duration);
return false;
}
}else{
return false;
}
}
scheduleMeeting("7:00", 15); // false
scheduleMeeting("07:15", 30); // false
scheduleMeeting("7:30", 30); // true
scheduleMeeting("11:30", 60); // true
scheduleMeeting("17:00", 45); // true
scheduleMeeting("17:30", 30); // false
scheduleMeeting("18:00", 15); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment