forked from sf-wdi-30/problem-set-js-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping-pong.js
49 lines (39 loc) · 1.56 KB
/
ping-pong.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
In this challenge you will simulate the movement of a ping-pong, bouncing back and forth across a
table.
Create a function `pingPong` that accepts an array, and returns the *same* array
with the ping-pong moved one cell. Each time the ping-pong moves, you must increment the
ping-pong's `steps` counter by 1. The direction of movement should be determined soley by the
current state of the array and the current `steps` value of the ping-pong.
``` javascript
var table = [{steps: 0}, null, null, null];
pingPong(table); //=> [null, {steps: 1}, null, null]
pingPong(table); //=> [null, null, {steps: 2}, null]
pingPong(table); //=> [null, null, null, {steps: 3}]
pingPong(table); //=> [null, null, {steps: 4}, null]
pingPong(table); //=> [null, {steps: 5}, null, null]
pingPong(table); //=> [{steps: 6}, null, null, null]
pingPong(table); //=> [null, {steps: 7}, null, null]
table; //=> [null, {steps: 7}, null, null]
```
Keep in mind that I should be able start this process at *any* point:
```
var table = [null, {steps: 13}, null, null];
pingPong(table); //=> [null, null, {steps: 14}, null]
```
Bonuses
- Allow arrays of *any length*
- We can think of the ping-pong as having an internal "speed" of 1. Allow this value to be
explicity set at a higher number (i.e. move 2 cells at a time, or 3 cells at a time).
- Support multiple ping-pongs.
*/
// YOUR CODE HERE
function pingPong(arr){
var stepcount = 0;
for (i = 0; i < arr.length; i++){
if (arr[i]){
arr[i+1] = {steps: stepcount};
stepcount ++;
}
}
}