Sending Instructions from Earth to Rover on Mars using JavaScript. The ability to transmit and execute a sequence of commands to the rover is crucial for successful exploration

I have completed iteration 1 of the Mars Rover program using JavaScript, which allows my rover to move in all directions within a 2-dimensional space (x,y). Now, I am moving on to iteration #2 where I need to give a series of commands to the rover and have them executed sequentially.

Can you provide some insights on how to approach iteration 2?

Here is the current code snippet I am working with:

// --------- Mars Rover Kata: Iteration #1 ------------ \\


// ------------------- User Experience via Console --------- \\

var promptSay = "Play with the Console!\nW = UP \nS = DOWN \nD = RIGHT \nA = LEFT";

var gridEdge = "You can't go there! \n\nPlease note that you are playing within an imaginary grid, and the farthest you can go is 9 steps.";

var wrongInput = "---WRONG INPUT!--- Please use correct input i.e.: \nW = UP \nS = DOWN \nD = RIGHT \nA = LEFT";

// Object Definition: (Vars inside a Var, may include functions)  \\

...

Answer №1

Interacting with the console is not possible in a browsing environment.

To achieve this functionality, you need to employ event listeners. One way to do it is by attaching them to the document body as shown in the example below:

const UP_ARROW = 38;
const LEFT_ARROW = 37;
const DOWN_ARROW = 40;
const RIGHT_ARROW = 39;

document.body.addEventListener("keydown", function(e) {
  let direction = null;

  switch(e.keyCode) {
    case UP_ARROW:
      direction = "up";
      break;
    case LEFT_ARROW:
      direction = "left";
      break;
    case DOWN_ARROW:
      direction = "down";
      break;
    case RIGHT_ARROW:
      direction = "right";
      break;
  }

  if (direction) {
    console.log("Arrow key " + direction + " was pressed!");

    moveRover(direction);
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing difficulties with a JavaScript dice game

In my game, I'm facing an issue where one button needs to trigger two functions. The player die is working correctly, but the computer die isn't displaying a value. function Welcome() { alert("Welcome " + document.getElementById("fname ...

What is the best way to distinguish between the paths for administrators and regular users?

Currently, I am in the process of setting up routes using node js for a dashboard. Firstly, users will need to log in to obtain a JWT token. With the Token, users can access various user-related routes such as editing, deleting, and logging out. However, ...

What is the method for retrieving the information stored in a response header?

I am looking to extract information from an HTTP response header. My approach involves making an HTTP request using the following code snippet. var token = '123456'; var r = new XMLHttpRequest(); r.open('get', '/api/users/@me&apos ...

Convert HTML Rich Text to Plain Text using a TextArea component

Currently, I am facing an issue while attempting to display rich-content text from the server in a grid format without any HTML styling tags. Despite my efforts, I have been unable to successfully remove these tags by simply writing and reading the data fr ...

Assign a variable to a dynamic model within an AngularJS scope

I am facing an issue with scope closure. ... function(scope) { ... }); Within the scope, I need to achieve the following: $scope.favoriteTitle = 'some value' The challenge is that favoriteTitle cannot be static. It needs to be generated d ...

Is there a way to incorporate Java variables into a JavaScript function on a JSP page?

I've put together a JSP page: <% int num1 = 10; int num2 = 20; %> <script> function display(){ var data = new google.visualization.DataTable(); data.addColumn('string', 'Category'); data.addColumn(&a ...

Passing form data using AJAX before refreshing the page

Whenever I use a jQuery.get() request to send form data, the page reloads/redirects too quickly for my JavaScript/jQuery code to catch up and properly send the data where it needs to go. To work around this issue, I've been using an alert() to pause t ...

The catch all route in Next.js seems to be malfunctioning when paired with the getStaticPaths function

Why is the Next.js catch all route not working as expected with the getStaticPaths function? The documentation states that I should be able to navigate to both t/a.cat and t/a.cat/a.id, but it only seems to work for the latter. What could be causing this ...

I'm running into some issues with flexbox and I'm in need of some assistance to find

I positioned two divs next to one another, but instead of each taking up 100vw, they are both sharing 50% of the available space. Is there a solution for this issue? Thank you. Page Image import type { AppProps } from "next/app"; import "./global.cs ...

Can somebody please tell me the equivalent of the sleep() function in JavaScript?

Are there more efficient ways to implement a sleep function in JavaScript compared to the pausecomp function as shown below (sourced from here)? function pausecomp(millis) { var date = new Date(); var curDate = null; do { curDate = new Date(); ...

Extract the initial line in the grid

I was wondering if there is a way to automatically copy the first value of a row in a grid to all the remaining rows. For example, if I have two columns A and B, and I input a value in column B at the first row, can that value be automatically populated in ...

JS: Best way to transfer selected information to the following page?

Within our SharePoint list, users often need to add very similar items. To streamline this process, I am exploring the possibility of adding a new column that includes a button or turning one of the existing columns into a clickable link. When clicked: ...

Error: The index $_GET[...] is not defined

When transferring a javascript variable to a php file, I encounter an issue where $_GET['something'] keeps returning as an undefined index. Despite this error, the correct result is displayed and written into the xml. However, when I attempt to ...

{ [Issue: port 5000 already in use]

I'm encountering an issue with my Server.JS file while trying to connect Deployd to Heroku. Despite configuring the mongoDB port to 5000 in Nitrous, I keep getting an error. Any assistance would be greatly appreciated. Thank you! Server.JS // requir ...

The closing brackets in PHP substr() function are causing style issues

Here's the scenario: I entered a large amount of content in a text editor (WordPress). Now, I want to display this content on my homepage using PHP queries. In order to limit the content size to 100-200 characters, I used the substr() function i ...

Debugging NodeJs: The occasional success and Async.map troubles

Below is the structure of my config json file. "constraints": { "input": "input", "output": "output" } I want to read this file and generate directories for input and output along with their subdirectories. var fs = require("fs"); var async = ...

Exploring the Dynamic Resizing of Geometry Meshes in three.js

Is there a way to adjust the height of my geometry meshes dynamically? You can check out my demo here. ...

Guide to integrating Bootstrap-Vue / Vue 2 with Vite

In the process of migrating my project from webpacker to vite, I encountered some Bootstrap Vue issues such as Multiple instances of Vue detected and $attr and $listeners is readonly, which are detailed in BootstrapVue's documentation here Previously ...

ag-Grid incorporating new style elements

For my Angular application, I have a simple requirement of adding a CSS class when a row expands or collapses to highlight the row. I attempted to use gridOptions.getRowClass following the documentation at https://www.ag-grid.com/javascript-grid-row-styles ...

A collection of UIImageViews in Objective-C

I have a collection of UIImageViews and I want to create an array in Objective-C where each element corresponds to one of the existing UIImageViews. How can I achieve that? In Java, I accomplished this using the following code: JLabel l1 = new JLabel(); J ...