Advertisement
xoxama

Military Tanks

Feb 22nd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. >> Military Tanks
  2.  
  3. const input = ['UDLRRR'];
  4. const print = this.print || console.log;
  5. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  6.  
  7. /* Задача 1: Military Tanks
  8. Military scientists are training battle tanks using artificial intelligence. The first lesson is to teach them to move across the (x,y) - plane. They give them a sequence of moves and observe whether the tanks get to the correct (x, y) position on the field.
  9. This sequence is represented by string, and the character at position i represents the tank’s i-th move.
  10. There are several commands – R – moves right, L – moves left, U – moves up and D – moves down.
  11. To help the scientists, you have to write a program that collects the learning results of the tanks.
  12. + танкът започва движение с координати 0, 0 - това се виждаше и от картинката */
  13.  
  14. const iMove = gets().split(''); // [ 'U', 'D', 'L', 'R', 'R', 'R' ]
  15. let x = 0;
  16. let y = 0;
  17.  
  18. for (let i = 0; i < iMove.length; i++) {
  19. if (iMove[i] === 'U') {
  20. y += 1;
  21. } else if (iMove[i] === 'D') {
  22. y -= 1;
  23. } else if (iMove[i] === 'L') {
  24. x -= 1;
  25. } else if (iMove[i] === 'R') {
  26. x += 1;
  27. }
  28. }
  29.  
  30. print(`(${x}, ${y})`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement