-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfindPivot.js
More file actions
53 lines (47 loc) · 1.59 KB
/
findPivot.js
File metadata and controls
53 lines (47 loc) · 1.59 KB
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
/////////////////////////////////////////////////////////////
// Alphabetical Words
// ----------------------------------------------------------
// I have an array of words that are mostly alphabetical,
// except they start somewhere in the middle of the alphabet,
// reach the end, and then start from the beginning of the
// alphabet.
//
// In other words, this is an alphabetically ordered array
// that has been "rotated." For example:
// ['dog','eagle','falcon', /*PIVOT*/ 'apple','bear','cat']
//
// Write a function that returns the index of the 'pivot'
//
/////////////////////////////////////////////////////////////
var findPivotedWord = function (array, start, end) {
// TODO: IMPLEMENT
};
/////////////////////////////////////////////////////////////
// TEST CASES
/////////////////////////////////////////////////////////////
var tests = [{
inputs: ['cat','dog','eagle','falcon','goat','apple','bear'],
expected: 4
}, {
inputs: ['dog','eagle','falcon','goat','hope','apple','bear','cat'],
expected: 4
}, {
inputs: ['dog','eagle','falcon','goat','hope'],
expected: null
}, {
inputs: ['indigo','dog','eagle','falcon','goat','hope'],
expected: 0
}, {
inputs: ['dog','eagle','falcon','apple','bear','cat'],
expected: 2
}];
var tester = function (testCase, functionToTest) {
var testResult = functionToTest(testCase.inputs);
return expectToBeEqual(testResult, testCase.expected);
};
var expectToBeEqual = function (a, b) {
return JSON.stringify(a) === JSON.stringify(b);
};
tests.forEach(function (test, i) {
console.log('Test #' + i + ' PASS? ', tester(test, findPivotedWord));
});