didkoslawow

Untitled

Dec 23rd, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. function ladybugsFlying(inputInfo) {
  2. // building the logic for the starting field
  3.  
  4. let field = new Array(inputInfo[0]);
  5. field.fill(0);
  6. let startingIndexes = inputInfo[1].split(" ");
  7.  
  8. for (let i = 0; i < startingIndexes.length; i++) {
  9. for (let j = 0; j < startingIndexes.length; j++) {
  10. field[startingIndexes[j]] = 1;
  11. }
  12. }
  13.  
  14. // building the logic for the ladybugs to fly around
  15.  
  16. for (let i = 2; i < inputInfo.length; i++) {
  17. let command = inputInfo[i].split(" ");
  18. if (field[command[0]] === 1) {
  19. if (command[1] === "left") {
  20. field[command[0]] = 0;
  21. field[Number(command[0]) - Number(command[2])] = 1;
  22. } else if (command[1] === "right") {
  23. field[command[0]] = 0;
  24. field[Number(command[0]) + Number(command[2])] = 1;
  25. }
  26. }
  27. }
  28. console.log(field.join(" "));
  29. }
  30.  
  31. ladybugsFlying([3, "0 1 2", "0 right 1", "1 right 1", "2 right 1"]);
Advertisement
Add Comment
Please, Sign In to add comment