skip to Main Content

I have some task to resolve, when we need to renew the coordinates array from commands, like this.
when forward – y is adding 1, back – y minus 1, right – x plus 1 and left – x minus 1. But something is not working properly here.

function getLocation(coordinates, commands) {
  // write code here
  let x = coordinates[0]
  let y = coordinates[1]
  //let newCoordinates = [];

    for (let i = 0; i < commands.length; i++) {
     if (i === 'forward' ) {
          y += 1 ;
          }
       if (i === 'back') {
         y -= 1 ;
      }
      if (i === 'right') {
      x +=  1;
      }
      if (i === 'left') {
      x -= 1;
    }

 }
 //newCoordinates.push(x, y)
return coordinates;

}
 console.log(getLocation([0, 0], ['forward', 'forward', 'left']))

2

Answers


  1. You are using index for checking the command, you should use values of array in conditional statement like this

    function getLocation(coordinates, commands) {
      let x = coordinates[0]
      let y = coordinates[1]
      let newCoordinates = [];
      
      commands.forEach((command) => {
          if (command === 'forward' ) {
              y += 1 ;
          }
          if (command === 'back') {
             y -= 1 ;
          }
          if (command === 'right') {
            x +=  1;
          }
          if (command === 'left') {
            x -= 1;
          }
      })
    
      newCoordinates.push(x, y)
      return newCoordinates;
    
    }
     console.log(getLocation([0, 0], ['forward', 'forward', 'left']))
    Login or Signup to reply.
  2. You’re not accessing the values in the commands array when looping. At the moment you’re comparing if the i is either 'forward', 'back', or any other command.

    What you should be doing is comparing each value in the commands array by accessing the value. commands[i] === 'forward', and so forth.

    Returning the coordinates array will not give you the updated values, as modifying x and y don’t update the values in the coordinates array. The easiest way to solve this by returning a new array with x and y in it, like so: return [x, y];

    function getLocation(coordinates, commands) {
      let x = coordinates[0]
      let y = coordinates[1]
    
      for (let i = 0; i < commands.length; i++) {
        if (commands[i] === 'forward') {
          y += 1;
        }
        
        if (commands[i] === 'back') {
          y -= 1;
        }
        
        if (commands[i] === 'right') {
          x += 1;
        }
        
        if (commands[i] === 'left') {
          x -= 1;
        }
      }
      
      return [x, y];
    }
    
    console.log(getLocation([0, 0], ['forward', 'forward', 'left']))

    I also took the liberty to modify your function to use a destructuring assignment and a rest parameter as an example on how you could implement the function in a different way. Don’t know if it will help you out, but I assume you’re still learning and this might expand your knowledge base.

    function getLocation([x, y], ...commands) {
      for (command of commands) {
        if (command === 'forward') {
          y += 1;
        }
        
        else if (command === 'back') {
          y -= 1;
        }
        
        else if (command === 'right') {
          x += 1;
        } 
        
        else if (command=== 'left') {
          x -= 1;
        }
      }
      
      return [x, y];
    }
    
    console.log(getLocation([0, 0], 'forward', 'forward', 'left'))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search