-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 14 - MaximalAdjacentDifference.js
80 lines (56 loc) · 1.68 KB
/
Day 14 - MaximalAdjacentDifference.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
/*Maximal Adjacent Differencec
https://scrimba.com/scrim/coa104bbe90b7d734889eb3ce
Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.
Example:
For inputArray = [2,4,1,0], the output should be arrayMaximalAdjacentDifference(inputArray) = 3
For input Array = [2,9,1,0], the output should be arrayMaximalAdjacentDifference(inputArray) = 8
Hints: Math.abs()
*/
function arrayMaximalAdjacentDifference(nums) {
let max = 0
for(i=0; i<nums.length-1; i++) {
let absDiff = Math.abs(nums[i] - nums[i+1])
if(absDiff > max) {
max = absDiff
}
}
return max
}
//or:
/*function arrayMaximalAdjacentDifference(nums) {
let max = 0
for(i=0; i<nums.length-1; i++) {
let diff = nums[i] - nums[i+1]
let absDiff = Math.abs(diff)
if(absDiff > max) {
max = absDiff
}
}
return max
}
*/
/**
* Test Suite
*/
describe('arrayMaximalAdjacentDifference()', () => {
it('returns largest difference between adjacent values', () => {
// arrange
const nums = [2, 4, 1, 0];
// act
const result = arrayMaximalAdjacentDifference(nums);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(3);
});
it('returns largest difference between adjacent values example 2', () => {
// arrange
const nums = [2, 9, 1, 0];
// act
const result = arrayMaximalAdjacentDifference(nums);
// log
console.log("result 2: ", result);
// assert
expect(result).toBe(8);
});
});