Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions week3/final/robot-text/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
'use strict';

const board = [
['T', 'T', '.', 'F'],
['T', '.', '.', '.'],
['.', '.', '.', '.'],
['R', '.', '.', 'W']
['.', '.', '.', '.','.'],
['.', '.', '.', '.','.'],
['R', '.', '.', '.','F'],
['.', 'A', 'A', 'A','.'],
['.', '.', '.', '.','.']
];

const robot = {
x: 0,
y: 0,
dir: 'up',
y: 2,
dir: 'right',
};

let flagReached = false;
let appleEaten = false;
let applesEaten = 0;

let moves = 0;

board.reverse();
Expand All @@ -36,8 +40,9 @@
}
console.log(line);
}
if (appleEaten) { console.log('YUM!');}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to always format an if statement using multiple line, like this:

if (appleEaten) { 
   console.log('YUM!');
}

This makes it easier to add more lines to the if block should that be necessary in the future.

if (flagReached) {
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
console.log('\nHurray! Flag reached in ' + moves + ' steps!' + "I ate " + applesEaten +" apples");
}
}

Expand All @@ -62,11 +67,14 @@

const cellContents = board[y][x];

if (cellContents === '.' || cellContents === 'F') {
if (cellContents === '.' || cellContents === 'A' || cellContents === 'F') {
board[robot.y][robot.x] = trailIndicators[robot.dir];
robot.x = x;
robot.y = y;
board[y][x] = 'R';
if (cellContents === 'A') {
appleEaten = true;
applesEaten ++;}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closing brace should be on a new line:

  applesEaten ++;
}

if (cellContents === 'F') {
flagReached = true;
}
Expand Down Expand Up @@ -98,13 +106,15 @@

render();

move();

turn('right');
move();
turn("left");
move();
move();
turn('left');
move();
move();
turn('left');
move();

})();