Advertisement
CoineTre

Robots Direction

Oct 9th, 2024 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.50 KB | Source Code | 0 0
  1. function getDirection(direction) {
  2.   let hor = 0;
  3.   let ver = 0;
  4.  
  5.   switch (direction) {
  6.     case ('forward'):
  7.       hor = 0; ver = 1;
  8.       break;
  9.     case ('back'):
  10.       hor = 0; ver = -1;
  11.       break;
  12.     case ('right'):
  13.       hor = 1; ver = 0;
  14.       break;
  15.     case ('left'):
  16.       hor = -1; ver = 0;
  17.       break;
  18.     case ('stop'):
  19.       hor = 0; ver = 0;
  20.       break;
  21.   }
  22.  
  23.   return (`hor=${hor} ver=${ver}`);
  24. }
  25. console.log(getDirection('forward'));
  26.  
  27.  
  28. /*We train robots but teaching robots is even harder than teaching people!
  29.  
  30. Our robot receives a command to its terminal in the form of the string back, forward, left, right, stop. Then his brain converts the value into x and y coordinates and sends an impulse with coordinates to its robotic legs as a string hor=x ver=y.
  31.  
  32. Write a function getDirection, that takes a string direction with direction and returns a string in the format hor=x ver=y.
  33.  
  34. The commands are the following:
  35.  
  36. forward is the signal 0 for x and 1 for y
  37. back is the signal 0 for x and -1 for y
  38. right is the signal 1 for x and 0 for y
  39. left is the signal -1 for x and 0 for y
  40. stop is the signal 0 for x and 0 for y
  41. If the command is not recognized, x and y will be set to 0.
  42.  
  43. And one more thing. Our robot has not yet learned conditional operators, so you have to use a switch case.
  44.  
  45. For example:
  46.  
  47. For the 'forward' command the robot needs coordinates in the format hor=0 ver=1
  48. And he doesn't know 'turn around' command, so the coordinates will be hor=0 ver=0
  49.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement