Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sequence(array) {
- let list = array.shift().split(' ').map(Number);
- let listLength = list.length
- for (let command of array) {
- let token = command.split(' ');
- if (token[0] === 'Add') {
- list.push(Number(token[1]));
- }
- else if (token[0] === 'Remove') {
- let value = Number(token[1]);
- if (list.includes(value)) {
- list.splice(list.indexOf(value), 1);
- }
- } else if (token[0] === 'Replace') {
- let value = Number(token[1]);
- let replacement = Number(token[2]);
- if (list.includes(value)) {
- list.splice(list.indexOf(value), 1, replacement);
- }
- } else if (token[0] === 'Collapse') {
- let value = Number(token[1]);
- for (let i = 0; i < listLength; i++) {
- if (list[i] < value) {
- list.splice(list.indexOf(list[i]), 1)
- }
- }
- } else if (token[0] === 'Finish') {
- console.log(list.join(' '));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment