The problem wants me to output the string "Edabit" with the number of "a" characters equaling the initial number passed through the function.
I have tried different methods of concatenation but for some reason the string of "a"s seem to ignore their place in the concatenation.
function howManyTimes(num) {
let str = ''
if (num === 0){
return `Ed${str}bit`
}
else {
str += 'a'
return str += howManyTimes(num - 1)
}
}
console.assert(howManyTimes(0) == "Edbit", "1. Instead got "+howManyTimes(0));
console.assert(howManyTimes(1) == "Edabit", "2. Instead got "+howManyTimes(1));
console.assert(howManyTimes(10) == "Edaaaaaaaaaabit", "3. Instead got "+howManyTimes(10));
function howManyTimes(num, str) {
if (!str) str = '';
if (num > 0) {
return howManyTimes(num - 1, str + 'a');
} else {
return `Ed${str}bit`;
}
}
console.log(howManyTimes(8));
One issue is that your recursion is always appending the result of the method to an a
. Rather than doing that, pass along the aggregated string to then be used once you reach the end.