-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddOneToArray.js
More file actions
28 lines (26 loc) · 928 Bytes
/
Copy pathaddOneToArray.js
File metadata and controls
28 lines (26 loc) · 928 Bytes
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
// Problem:
// Create a function that will add one to a number that is represented as an array and return the new number represented as an array of numbers (0-9).
// Each element in the input array will be a number and will be between 0-9.
// E.g 1: [1,2,3,6] -> [1,2,3,7]
// E.g. 2: [1,2,4,9] -> [1,2,5,0]
// E.g 3: [9,9,9,9] -> [1,0,0,0,0]
// Solution
const addOneToArrayAgain = (arr) => {
let carry = 1; let result = [];
for (let i = arr.length - 1; i >= 0; i--) {
let total = arr[i] + carry;
// If total is now 10 (cannot be higher than 10 ie 9 + 1) push 0 to the array
if (total === 10) {
result.unshift(0);
// If we are on the first element we need to add 1 to the start of the array
if (i === 0) { // e.g. [9, X, X, X] we want [1, 0, X, X, X]
result.unshift(1);
carry = 0;
}
} else {
carry = 0;
result.unshift(total);
}
}
return result;
}