patternjavascriptMinor
Get rid of recursion in fruit picker
Viewed 0 times
ridpickerfruitrecursionget
Problem
I have this simple code and it is working fine it is self explanatory: the user will choose a random number 10 times and based on the random number the array will be filled by fruits.
However in the code above, requirement is not restrictive; so below the number of available fruit is limited, and even if the random number choose a fruit, i have first to check if it is available in store or not. if the fruit is not available I should fall through to the next available fruit. The code below is working fine; but I had to do it via recursion in case fruit is not available.
This code is working and my question is:
Since this selection of food is happening many times in the page, and it involves recursion, I am worried that this will degrade the performance.
Is there any advice in this regard? or any change suggested to the code or algorithm?
var fruit =[];
for( x=0; x<10,x++){
var r = Math.floor(Math.random() * 100 + 1);
choose(r);
}
function choose(num){
if(num <30){
fruit.push("apple");
}else if(num <70){
fruit.push("kiwi");
}else if(num< 90){
fruit.push("banana");
}else{
fruit.push("grapes");
};
}However in the code above, requirement is not restrictive; so below the number of available fruit is limited, and even if the random number choose a fruit, i have first to check if it is available in store or not. if the fruit is not available I should fall through to the next available fruit. The code below is working fine; but I had to do it via recursion in case fruit is not available.
var apple = 2;
var kiwi = 1;
var banana = 5;
var grapes = 2;
var fruit =[];
for( x=0; x0){
fruit.push("apple");
apple--;
}else{
num = 31;
choose(num);
};
}else if(num 0){
fruit.push("kiwi");
kiwi--;
}else{
num = 71;
choose(num);
};
}else if(num0){
fruit.push("banana");
banana--;
}else{
num = 91;
choose(num);
};
}else{
if(grapes>0){
fruit.push("grapes");
grapes--;
}else{
num = 1;
choose(num);
};
};
}This code is working and my question is:
Since this selection of food is happening many times in the page, and it involves recursion, I am worried that this will degrade the performance.
Is there any advice in this regard? or any change suggested to the code or algorithm?
Solution
Micnic was almost there, but I think that you can lose the recursion completely by separating your if statement into several if statements
This will remove the recursion (sounds like that is what you want to do) and will still cascade through the fruit. notice that if the number is
If the number is 40 it will start at checking for kiwi's if there are none then then it will check for bananas.
I think this is exactly what you were looking for.
I don't like a function returning undefined, so I added the
function choose(num){
var looped = false;
while (!looped) {
if (num 0) {
fruit.push("apple");
apple--;
return "added apple";
}
if (num 0) {
fruit.push("kiwi");
kiwi--;
return "added kiwi";
}
if (num 0) {
fruit.push("banana");
banana--;
return "added banana";
}
if (grapes > 0) {
fruit.push("grapes");
grapes--;
return "added grapes";
} else {
if (looped) {
return "no fruit left";
}
num = 1;
looped = true;
}
}
};This will remove the recursion (sounds like that is what you want to do) and will still cascade through the fruit. notice that if the number is
95 and there are no grapes it will still recursively call the choose function with a number of 1 to cover the rest of the fruit, The only difference here is that you don't have the recursive call in every if statement, and you have less than half of the if statement calls. I also made sure that the this wouldn't loop more than once by implementing a while loop that will only ever loop once.If the number is 40 it will start at checking for kiwi's if there are none then then it will check for bananas.
I think this is exactly what you were looking for.
I don't like a function returning undefined, so I added the
return "added {fruit}"; but you could just have return; if that is your preference.Code Snippets
function choose(num){
var looped = false;
while (!looped) {
if (num < 30 && apple > 0) {
fruit.push("apple");
apple--;
return "added apple";
}
if (num < 70 && kiwi > 0) {
fruit.push("kiwi");
kiwi--;
return "added kiwi";
}
if (num < 90 && banana > 0) {
fruit.push("banana");
banana--;
return "added banana";
}
if (grapes > 0) {
fruit.push("grapes");
grapes--;
return "added grapes";
} else {
if (looped) {
return "no fruit left";
}
num = 1;
looped = true;
}
}
};Context
StackExchange Code Review Q#53873, answer score: 7
Revisions (0)
No revisions yet.