I am confused as to why my function continues to loop. wedgesNeeded = number, limes = array
function limesToCut(wedgesNeeded, limes) {
let total = 0
while (wedgesNeeded > 0) {
let lime = limes.shift()
switch (lime) {
case 'small':
wedgesNeeded -= 6;
total++;
break;
case 'medium':
wedgesNeeded -= 8;
total++;
break;
default:
}
}
return total
}
console.log(limesToCut(12, ['small','small']));
limehas any of the values in the switch? If you put an alert in thedefaultcase, does that appear?limescontains none of those strings, it will loop indefinitely. Also, iflimesdoes not contain enough of those string to getwedgesNeededdown to 0, it will continue to loop.lime?let lime = limes.shift(); it only gets evaluated once, right? I have no idea why this would loop infinitely; I'd think you'd eventually run out of limes.${wedgesNeeded} ${total}) after the switch it will return all the correct values. The total increases correctly and the wedgesNeeded will decrease correctly.