Exercise two - official solution does not work yet passes!
swinston100 opened this issue · 3 comments
swinston100 commented
Everything seems to pass unless an error is thrown! For example the official solution
function repeat(operation, num) {
if (num <= 0) return
operation()
return repeat(operation, --num)
}
module.exports = repeatonly performs the operation once yet passes the verification
a working alternative would be:
function repeat(operation, num) {
if (num > 0){
operation(); //do the operation
}
else { return; }
repeat(operation, --num); //reduce number first then use recursion
};
module.exports = repeat;teone commented
This code would also pass the verication:
function repeat(operation, num) {
if(num >= 0){
return;
}
//operation();
return repeat(operation, num -1);
}
module.exports = repeat;
Also if operation is actually never called..
swinston100 commented
Good point @teone !!!
timoxley commented
I don't see the problem with the solution. e.g. this works (try paste in browser console):
function repeat(operation, num) {
if (num <= 0) return
operation()
return repeat(operation, --num)
}
// should repeat 5 times
var count = 0;
repeat(function() {
count++
}, 5);
if (count !== 5) {
console.error('it does not work')
} else {
console.log('it works')
}Though yeah, the verification is definitely broken.