Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(stock = [], ordered = []){
- function addingToArr(inStock = []){
- let inStockOutput = [];
- for(let i = 0; i < inStock.length; i++){
- if(i % 2 == 0){
- let name = inStock[i];
- let count = Number(inStock[i + 1]);
- let product = { name, count };
- inStockOutput.push(product);
- }
- }
- return inStockOutput;
- }
- let firstArray = addingToArr(stock);
- let secondArray = addingToArr(ordered);
- let finalResult = firstArray.slice();
- for (const first of firstArray) {
- for (const second of secondArray) {
- if(first.name === second.name){
- first.count += second.count;
- }
- else{
- finalResult.push(second)
- }
- }
- }
- console.log(finalResult)
- }
- solve([
- 'Chips', '5', 'CocaCola', '9', 'Bananas', '14', 'Pasta', '4', 'Beer', '2'],
- [
- 'Flour', '44', 'Oil', '12', 'Pasta', '7', 'Tomatoes', '70', 'Bananas', '30'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement