-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (62 loc) · 2 KB
/
index.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
/**
* --- Day 1: Inverse Captcha - Part II ---
*
* This module contains a method that review a sequence of digits and find
* the sum of all digits that match the digit halfway around the circular list.
*
* @author Sara Hernández <[email protected]>
*
* @param {string} input - A string representing a numeric value.
* @return {number} - Sum of digits that match the next in the list.
*/
const solveCaptcha = function (input = "") {
const sequence = getDigitSequence(input);
if (!sequence) {
console.error(`Invalid input! Make sure it doesn't contain non-numeric values.`);
return null;
}
if (sequence.length % 2 !== 0) {
console.error(`Invalid input! The sequence must have an even number of digits.`);
return null;
}
let result = 0;
for (let i = 0; i < sequence.length; i++) {
const nextIndex = getNextIndex(i, sequence.length);
if (sequence[i] === (sequence[nextIndex])) {
result += sequence[i];
}
}
return result;
};
/**
* Obtains the index of the digit halfway in the circular list.
* @param {number} currentIndex - Position in the sequence for current digit.
* @param {number} seqLength - Number of elements in sequence.
* @return {number} Index for the item halfway in the list.
*/
const getNextIndex = function (currentIndex, seqLength) {
let nextIndex = currentIndex + (seqLength / 2);
if (nextIndex >= seqLength) {
nextIndex -= seqLength;
}
return nextIndex;
};
/**
* Converts input string into an array of numbers and returns said array.
* If any of the characters in the string is not a number, `null` will be
* returned instead.
* @param {string} input
* @return Array of numeric values or `null` if input is not valid.
*/
const getDigitSequence = function (input) {
let invalidInput = false;
const sequence = input.split("").map((digit) => {
const int = parseInt(digit);
if (isNaN(int)) {
invalidInput = true;
}
return int;
});
return !invalidInput ? sequence : null;
};
module.exports = solveCaptcha;