Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function santasGifts(input) {
- let commandNumbers = Number(input[0]);
- let houses = input[1].split(' ');
- let index = 0;
- for (let i = 2; i < commandNumbers + 2; i++) {
- let currentIndex = index;
- let command = input[i].split(' ');
- if (command[0] == "Forward" || command[0] == "Back") {
- let steps = Number(command[1]);
- currentIndex += (command[0] == "Forward" ? steps : -steps);
- if (currentIndex >= 0 && currentIndex < houses.length) {
- index = currentIndex;
- houses.splice(index, 1);
- }
- } else if (command[0] == "Gift") {
- currentIndex = Number(command[1]);
- if (currentIndex >= 0 && currentIndex < houses.length) {
- index = currentIndex;
- houses.splice(index, 0, command[2]);
- }
- } else if (command[0] == "Swap") {
- let index1 = houses.indexOf(command[1]),
- index2 = houses.indexOf(command[2]);
- if (index1 != -1 && index2 != -1) {
- let value = houses[index1];
- houses[index1] = houses[index2];
- houses[index2] = value;
- }
- }
- }
- console.log(`Position: ${index}`);
- if (houses.length > 0) {
- console.log(houses.join(", "));
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement