Hey,
I like your alternative solution to the recursion exercise:
I think using…
if (number < 0) number = -number;
… as the first if
statement in the function body provides a good simple solution, which only requires one recursive function call in the final else
statement, rather than the two used in the suggested solution, and my less concise solution (below).
function isEven(num) {
if (num === 0) return true;
else if (num > 0) {
if (num === 1) return false;
else return isEven(num -2);
}
else if (num === -1) return false;
else return isEven(num + 2);
}
console.log(isEven(-1));
Also, not sure if you know, but if you wrap the code you include in these posts in back ticks (while editing), it formats it nicely for you, making it easier to read. For an inline piece of code: surround it in single back ticks e.g. `console.log()` will appear as console.log()
For a block of code put three back ticks on the line before and on the line after e.g.
```
function min(x, y) {
return x < y ? x : y;
}
console.log(min(5, -3.5));
```
will appear as:
function min(x, y) {
return x < y ? x : y;
}
console.log(min(5, -3.5));
As an easy alternative to typing the back ticks manually, you can just click on the </>
in the menu bar at the top of the text input box and it will automatically give you two backticks, or two sets of three backticks, depending on whether you are inline or on a new line
It’s using a simple markup language called Markdown. Here’s a link if you, or anyone else reading this, would like to know more: https://commonmark.org/help/
By the way, that final piece of code I used in my example is my alternative solution to the first exercise, using a ternary operator. I like how short and sweet it is