Welcome! This lesson involves an engaging task that combines basic operations with array manipulation. We will implement a "Move Until Obstacle" game using an integer array in TypeScript. Visualize yourself as a game developer and get ready to immerse yourself in the fascinating world of problem-solving with enhanced type safety!
In the "Move Until Obstacle" game, the player begins at the start of a linear array of integers. The number at each position indicates how many steps a player can move to the right. An obstacle number is one on which the player cannot land. The goal is to move as far to the right as possible until either an obstacle stops the player or the player reaches the end of the array.
Your function, function moveUntilObstacle(numbers: number[], obstacle: number): number
, should tally and return the number of moves needed to reach the end of the array without encountering an obstacle. If the player encounters an obstacle, the function should return the index at which this obstacle lies.
For example, if the function receives the input numbers = [2, 3, 3, 4, 2, 4]
and obstacle = 4
, it should return 5
. This is because the player starts at the 0th index, takes 2 steps as indicated by the number at the 0th index (landing on the 2nd index), and then takes 3 more steps, as indicated by the number at the 2nd index, to land on the 5th index, which is the obstacle 4
.
If the function receives the following input numbers = [4, 1, 2, 2, 4, 2, 2]
and obstacle = 2
, the output should be 2
. The player starts at the 0th index, takes 4 steps, lands on the 4th index, then takes 4 more steps, which brings the player outside the array. So the total number of steps the player takes is 2
.
Our first step is to ensure we have variables to track the player, i.e., their current position and the moves they've made so far. We'll name them position
and moves
, initializing both to 0
with explicit type annotations for clarity:
TypeScript1function moveUntilObstacle(numbers: number[], obstacle: number): number { 2 let position: number = 0; 3 let moves: number = 0;
Next, we'll use a while loop to iterate over the array. It continues as long as position
is less than the size of the array numbers
, which we get using numbers.length
:
TypeScript1function moveUntilObstacle(numbers: number[], obstacle: number): number { 2 let position: number = 0; 3 let moves: number = 0; 4 while (position < numbers.length) {
During each iteration, we need to check whether the player has encountered an obstacle. If so, we should return the position
at which this obstacle is located:
TypeScript1function moveUntilObstacle(numbers: number[], obstacle: number): number { 2 let position: number = 0; 3 let moves: number = 0; 4 while (position < numbers.length) { 5 if (numbers[position] === obstacle) { 6 return position; 7 }
If the current number is not an obstacle, the player proceeds. The number of steps taken corresponds to the value at the current position
. We add this to position
and increment moves
:
TypeScript1function moveUntilObstacle(numbers: number[], obstacle: number): number { 2 let position: number = 0; 3 let moves: number = 0; 4 while (position < numbers.length) { 5 if (numbers[position] === obstacle) { 6 return position; 7 } 8 position += numbers[position]; 9 moves++; 10 }
Once the loop ends, the player has either reached the end of the array or encountered an obstacle. If the player managed to navigate the entirety of the array without encountering an obstacle, we return the total number of moves
:
The complete function looks like this:
TypeScript1function moveUntilObstacle(numbers: number[], obstacle: number): number { 2 let position: number = 0; 3 let moves: number = 0; 4 5 while (position < numbers.length) { 6 // Check for an obstacle at the current position 7 if (numbers[position] === obstacle) { 8 return position; // Return the index of the obstacle 9 } 10 position += numbers[position]; // Move to the next position 11 moves++; // Increment moves 12 } 13 14 // If loop ends without obstacles, return total moves taken 15 return moves; 16} 17 18// Example usage 19console.log(moveUntilObstacle([2, 3, 3, 4, 2, 4], 4)); // Output: 5 20console.log(moveUntilObstacle([4, 1, 2, 2, 4, 2, 2], 2)); // Output: 2
Congratulations on successfully implementing the "Move Until Obstacle" game using TypeScript! You've tackled the challenges of this task by applying basic array manipulation concepts and operations with numbers while benefiting from the type safety features that TypeScript provides. These features help prevent errors by ensuring that your code behaves as expected. As you continue, explore similar exercises to further reinforce your understanding and enhance your skills. Keep up the great work!
