Skip to content

Instantly share code, notes, and snippets.

@jonybekov
Last active February 6, 2022 15:33
Show Gist options
  • Save jonybekov/a798493a9d5102f77600af5b5c75bd93 to your computer and use it in GitHub Desktop.
Save jonybekov/a798493a9d5102f77600af5b5c75bd93 to your computer and use it in GitHub Desktop.
YDKJS 2: Getting started: Appendix B: Practicing Closure Problem Solution
function range(start,end) {
if(start == end) {
return [start]
}
const printRange = (from, to) => {
if(to === 0) {
return [];
}
return Array((to - from) + 1).fill(null).map((_, i) => start + i)
}
if(start < end) {
return printRange(start, end)
}
if(typeof start === 'number' && end == undefined) {
return function (end) {
return printRange(start, end)
}
}
}
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment