how do we add a new value to each array in the container
if the number of fruits is less than the desired target?
for example:
- we need to add new fruits to these 2d arrays:
- The maximum number of "apple" in a container should be 3 fruits,
if there are more, we add them to a new array/index in the next 2d array container.
- The maximum number of "mango" in a container should be 2 fruits,
if there are more, we add them to a new array/index in the next 2d array container.
- The maximum number of "stawberry" in a container should be 4 fruits,
if there are more, we add them to a new array/index in the next 2d array container.
const stawberry = x => {
return x.filter(el => el === "stawberry").length;
}
const apple = x => {
return x.filter(el => el === "apple").length;
}
const mango = x => {
return x.filter(el => el === "mango").length;
}
const fruits = kindOfFruits => {
if(kindOfFruits.length === 0){
return [];
} else if(stawberry(kindOfFruits[0]) === 0 ){
kindOfFruits[0].push("NEW STAWBERRY");
}
return kindOfFruits.concat((fruits(kindOfFruits.slice(1))));
}
const container = [
["apple", "apple", "banana", "mango", "stawberry", "banana", "banana"],
["banana", "mango", "stawberry", "stawberry"],
["apple", "mango", "mango"]
];
console.log(fruits(container));
The desired RESULT should look like this:
[
["apple", "apple", "banana", "mango", "stawberry", "banana", "banana", "apple", "mango", "stawberry ", "stawberry", "stawberry"],
["banana", "mango", "stawberry", "stawberry", "apple", "apple" , "stawberry"],
["apple", "mango", "mango"]
];
NOTE: The order of the fruits doesn't matter when adding them, as long as the correct number is reached.
The containers are already set up, we just need to populate them with fruits according to the rules mentioned above.
I hope my explanation makes sense, please let me know if it's unclear.