Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shoppingList(arr) {
- let initialList = arr.shift().split('!');
- let lastCommand = arr.pop();
- for (let i = 0; i < arr.length; i++) {
- let commands = arr[i].split(' ');
- let command = commands[0];
- let product = commands[1];
- let productNewName = commands[2];
- if (command === 'Urgent') {
- if (!initialList.includes(product)) {
- initialList.unshift(product);
- }
- }
- if (command === 'Unnecessary') {
- if (initialList.includes(product)) {
- let index = initialList.indexOf(product);
- initialList.splice(index, 1);
- }
- }
- if (command === 'Correct') {
- if (initialList.includes(product)) {
- let index = initialList.indexOf(product);
- initialList[index] = productNewName;
- }
- }
- if (command === 'Rearrange') {
- if (initialList.includes(product)) {
- let index = initialList.indexOf(product);
- initialList.splice(index, 1);
- initialList.push(product);
- }
- }
- }
- console.log(initialList.join(', '));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement