Задачата Shopping List с функции
Здравейте, опитвам се да реша задачата Shopping List с функции, Judge дава 60/100, а по обикновения начин с switch-case дава 100/100
Някой може ли да ми помогне ?
задача: https://judge.softuni.org/Contests/Practice/Index/2031#1
код:
function solve(arr){
let groceries = arr.shift().split("!");
let command = arr.shift()
let actions = {Urgent: urgent, Unnecessary: unnecessary, Correct: correct, Rearrange: rearrange}
function urgent(item){
if(!groceries.includes(item)){
groceries.unshift(item);
}
}
function unnecessary(item){
if(groceries.includes(item)){
let index = groceries.indexOf(item);
groceries.splice(index, 1)
}
}
function correct(item){
let[oldItem, newItem] = item.split(" ")
if(groceries.includes(oldItem)){
let index = groceries.indexOf(oldItem)
groceries.splice(index, 1, newItem);
}
}
function rearrange(item){
if(groceries.includes(item)){
let index = groceries.indexOf(item)
groceries.splice(index, 1);
groceries.push(item)
}
}
while(command !== "Go Shopping!"){
let [action, item] = command.split(" ");
command = arr.shift()
actions[action](item)
}
console.log(groceries.join(", "))
}
thanks a lot :)