Професионална програма
Loading...
+ Нов въпрос
AlexanderF avatar AlexanderF 2 Точки

Array Manipulator 81/100

Може би правя грешка в някоя от последните 2 манипулации, но не откривам нищо.

https://pastebin.com/tJBNtzpi

Условие:

Write a function that receives an array of integers and array of string commands and executes them over the array. The commands are as follows:

  • add <index> <element> – adds element at the specified index (elements right from this position inclusively are shifted to the right).
  • addMany <index><element 1> <element 2> … <element n> – adds a set of elements at the specified index.
  • contains <element> – prints the index of the first occurrence of the specified element (if exists) in the array or -1 if the element is not found.
  • remove <index> – removes the element at the specified index.
  • shift <positions>shifts every element of the array the number of positions to the left (with rotation).
    • For example, [1, 2, 3, 4, 5] -> shift 2 -> [3, 4, 5, 1, 2]
  • sumPairs – sums the elements in the array by pairs (first + second, third + fourth, …).
    • For example, [1, 2, 4, 5, 6, 7, 8] -> [3, 9, 13, 8].
  • print – stop receiving more commands and print the last state of the array.

Examples

Input

Output

[1, 2, 4, 5, 6, 7],

['add 1 8', 'contains 1', 'contains 3', 'print']

0

-1

[ 1, 8, 2, 4, 5, 6, 7 ]

[1, 2, 3, 4, 5],

['addMany 5 9 8 7 6 5', 'contains 15', 'remove 3', 'shift 1', 'print']

-1

[ 2, 3, 5, 9, 8, 7, 6, 5, 1 ]

 

Тагове:
0
JavaScript Fundamentals 26/06/2020 11:31:30
krasizorbov avatar krasizorbov 376 Точки
Best Answer

Мисля че проблема беше при шифт командата но може да е имало и на друго място защото тръгнах да преправям и други команди от началото. По принцип на ме лично ми е много трудно да работя по начина по кйто си го направил а иманно:

manipulations го сплитваш и после позваш шифт върху него. Но това решение дава 100/100

 

function arrayManipulator(array, manipulations) {
  for (let i = 0; i < manipulations.length; i++) {
    let manipulation = manipulations[i].split(" ");
    let command = manipulation[0];
    let firstNum = Number(manipulation[1]);
    let secNum = Number(manipulation[2]);

    switch (command) {
      case "add":
        array.splice(firstNum, 0, secNum);
        break;

      case "addMany":
        //let index = firstNum;
        let newArray = manipulation.slice(2).map(Number);
        array.splice(firstNum, 0, ...newArray);
        break;

      case "contains":
        let index = array.indexOf(firstNum);
        console.log(index);
        break;

      case "remove":
        array.splice(firstNum, 1);
        break;

      case "shift":
        let rotations = firstNum;
        for (let j = 0; j < rotations; j++) {
          let element = array.shift();
          array.push(element);
        }
        break;
      case "sumPairs":
        let sumArr = [];
        if (array.length % 2 === 0) {
          for (let k = 0; k < array.length; k += 2) {
            sumArr.push(array[k] + array[k + 1]);
          }
        } else {
          for (let k = 0; k < array.length - 1; k += 2) {
            sumArr.push(array[k] + array[k + 1]);
          }
          sumArr.push(array.pop());
        }
        array = sumArr;

        break;
      case "print":
        console.log(`[ ${array.join(", ")} ]`);
        break;
    }
  }
}
arrayManipulator([1, 2, 4, 5, 6, 7], ["contains 1", "print"]);

0
AlexanderF avatar AlexanderF 2 Точки

Благодаря, от шифт манипулацията идва проблемът.

0