Advertisement
megi_al

Untitled

Apr 6th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. function problemTwo(input) {
  2. let sequence = input.shift().split(' ');
  3. let finalCommand = '';
  4. for (let line of input) {
  5. let [command, ...args] = line.split(' ');
  6.  
  7. if (command === 'add') {
  8. let to = args.shift();
  9. let afterTo = args.shift();
  10. if (afterTo === 'start') {
  11. sequence.unshift(...args);
  12. } else {
  13. sequence.push(...args);
  14. }
  15.  
  16. } else if (command === 'remove') {
  17. let greaterLower = args[0];
  18. let than = args[1];
  19. let value = Number(args[2]);
  20.  
  21. if (greaterLower === 'greater') {
  22. sequence = sequence.filter(x => x < value);
  23.  
  24. } else if(greaterLower === 'lower'){
  25. sequence = sequence.filter(x => x > value);
  26.  
  27. } else if(greaterLower === 'at') {
  28. if(value >= 0 && value < sequence.length){
  29. sequence.splice(value, 1);
  30. }
  31. } else if(greaterLower === 'count'){
  32. than = Number(than);
  33. sequence.splice(sequence.length - than);
  34.  
  35.  
  36. }
  37. } else if (command === 'replace') {
  38. let value = args[0];
  39. let replacement = args[1];
  40. if (sequence.includes(value)) {
  41. sequence = sequence.join(' ').replace(value, replacement).split(' ');
  42.  
  43.  
  44. }
  45. } else if(command === 'find'){
  46. let evenOdd = args[0];
  47. if(evenOdd === 'even'){
  48. let even = sequence.filter(el => el % 2 === 0);
  49. console.log(even.join(' '));
  50.  
  51. } else {
  52. let odd = sequence.filter(el => el % 2 !== 0);
  53. console.log(odd.join(' '));
  54.  
  55. }
  56. } else if(command === 'END'){
  57. break;
  58. }
  59. }
  60. console.log(sequence.join(', '));
  61.  
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement