-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgoW1D5.js
53 lines (43 loc) · 1.44 KB
/
algoW1D5.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
50
51
52
53
/***************************************************************************************************** */
/*
Recreate the built in .reverse method
Given an array, alter the array in place to have all of it's elements reversed
https://login.codingdojo.com/m/201/7911/54786 <-- hints on swapping values
*/
const arrA = ["a", "b", "c", "d", "e"];
const expectedA = ["e", "d", "c", "b", "a"];
const arrB = ["s", "l", "a", "p"];
const expectedB = ["p", "a", "l", "s"];
const arrC = ["k", "n", "i", "t", "s"];
const expectedC = ["s", "t", "i", "n", "k"];
/**
* Reverses a given array in place
* @param {Array<any>} items
* @param {number} startIdx
* @param {number} endIdx
* @returns {Array<any>} The original array, altered to be reversed
*
*/
function reverse(items) {
// code here
var newArr = [];
for (var i=items.length-1; i>=0; i--){
newArr.push(items[i])
}
items=newArr
return items
}
// Tests
console.log("\n************Algo #2***********");
console.log("A");
const test1 = reverse(arrA);
console.log(test1, "should be", expectedA); //tests return value
console.log(arrA, "should be", expectedA); //tests that original array was altered
console.log("\nB");
const test2 = reverse(arrB);
console.log(test2, "should be", expectedB);
console.log(arrB, "should be", expectedB);
console.log("\nC");
const test3 = reverse(arrC);
console.log(test3, "should be", expectedC);
console.log(arrC, "should be", expectedC);