-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.js
118 lines (93 loc) · 2.84 KB
/
algo.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Given an array and an item to search for,
return the first index where the item is found,
return -1 to represent not found
*/
const arrA = ["a", "b", "c"];
const searchItemA = "c";
const expectedA = 2;
const arrB = ["a", "b", "c"];
const searchItemB = 5;
const expectedB = -1;
const arrC = ["c", "a", "b", "c"];
const searchItemC = "c";
const expectedC = 0;
const arrD = [];
const searchItemD = 5;
const expectedD = -1;
/**
* Finds the index from the given array where the given item is found.
* @param {Array<any>} items An array of any kind of items.
* @param {any} searchItem The item to find.
* @returns {number} The index of found item, or -1 if not found.
*/
function indexOf(items, searchItem) {
// code here
if (items.length == 0) {
return -1
}
for (var i = 0; i < items.length; i++)
if (items[i] = searchItem) {
return i
}
return -1
}
// Tests
console.log("\n************Algo #1***********")
const resultA = indexOf(arrA, searchItemA);
console.log(resultA, "should be", expectedA);
const resultB = indexOf(arrB, searchItemB);
console.log(resultB, "should be", expectedB);
const resultC = indexOf(arrC, searchItemC);
console.log(resultC, "should be", expectedC);
const resultD = indexOf(arrD, searchItemD);
console.log(resultD, "should be", expectedD);
/*****************************************************************/
/*
Given an array and an int which represents the position starting from the back,
return the nth-to-last element.
JS has introduced the .at method for this purpose, but solve this algo w/o it.
*/
// Last element:
const arr1 = ["a", "b", "c", "d"];
const idx1 = 1;
const expected1 = "d";
// Second to last element:
const arr2 = ["a", "b", "c", "d"];
const idx2 = 2;
const expected2 = "c";
const arr3 = ["a", "b", "c", "d"];
const idx3 = 0;
const expected3 = null;
const arr4 = ["a", "b", "c", "d"];
const idx4 = -1;
const expected4 = null;
const arr5 = [];
const idx5 = 2;
const expected5 = null;
/**
* Retrieves the nth to last indexed item from the given array.
* @param {Array<any>} items An array of any kind of items.
* @param {number} nthToLast
* @returns {any} The item at the nthToLast index or null.
*/
function nthLast(items, nthToLast) {
// code here
if (nthToLast <= 0 || items.length == 0) {
return null
}
var index = items.length - nthToLast;
return items[index]
}
// Tests
console.log("\n************Algo #2***********")
const result1 = nthLast(arr1, idx1);
console.log(result1, "should be", expected1);
const result2 = nthLast(arr2, idx2);
console.log(result2, "should be", expected2);
const result3 = nthLast(arr3, idx3);
console.log(result3, "should be", expected3);
const result4 = nthLast(arr4, idx4);
console.log(result4, "should be", expected4);
const result5 = nthLast(arr5, idx5);
console.log(result5, "should be", expected5);