2 min read ~ Hello readers! It’s really important for me to continue my practice and learning. Lately, I have been completing a coding challenge a week with codewars. This helps me keep my skills sharp and see how other devs all over the planet would solve these problems. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. Today, the problem set focuses on JavaScript.
Getting the integers between two numbers
Build a function that can get all the integers between two given numbers.
Example:
(2,9)
Should give you this output back:
[ 3, 4, 5, 6, 7, 8 ]
If startNum is the same as endNum, return an empty array.
function range(startNum, endNum) {
let finalArr = [];
for (var i = startNum + 1; i < endNum; i++) {
finalArr.push(i)
}
return finalArr;
};
Disclaimer: *Now, as much as I am tempted to use ES8 & ES9 functions to reduce the number of lines in JavaScript, I am always a fan of readable code that others can understand. Therefore, with the JavaScript examples, I will try to write it without fancy functions as much as possible so it can actually be read and absorbed by my readers. All of my solutions are commented to explain my thinking. Of course, they could always be better. Please feel free to share how you would solve it!*
All solutions are also now on my GitHub profile if you want to check out all of the coding challenges I’ve completed so far!
All credit to problem sets goes to codewars