 # The correct sum of the doubled array is 29

I am doing the cc checker project and keep getting 90 when I call validateCred(mystery1)

The correct sum of the doubled array is 29. Not sure what I’m doing wrong here. Thank you in advance!

mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]

``````const validateCred = arr => {
let total = 0
let doubledArray = []
let dropCheckNumber = arr.slice(0, -1)
let reverseArr = dropCheckNumber.reverse()

for (let i = 0; i < reverseArr.length; i+=2) {
if (reverseArr[i] * 2 > 9) {
doubledArray.push(reverseArr[i] * 2 - 9)
} else {
doubledArray.push(reverseArr[i] * 2)
}
for (let n of doubledArray)
total += n
}
console.log(total)

};

validateCred(mystery1)
``````

The sum of the array is 60, and going through (by eyeball math) the array we end up with a plus 1, so the finished array sum is not divisible and should test, false.

Eg.

``````    v     v     v     v     v     v     v
[3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]
8     7     2     3     6     1     2
+4    -1    +1    -3    +3    -4    +1
0     0           0     0     0     0
^
|
after cancelling

``````

Thank you. I have a follow-up question. when I call this function, the output is several lines in the console before arriving at 29, the correct answer. How can I correctly format the code so that 29 is the only answer printed to the console?

`

const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; const validateCred = arr => { let total = 0; let reversedArr = arr.reverse(); for (let i = 1; i < reversedArr.length; i+=2) { if (reversedArr[i] * 2 > 9){ total += reversedArr[i] * 2 -9 } if (reversedArr[i] * 2 <= 9) { total += reversedArr[i] * 2 } console.log(total) } }; validateCred(mystery1)

`

Where does 29 come from? Here is my original sum of the array…

`````` > [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4].reduce((a,b) => a + b)
<- 60
``````

Given the cancellation method used above, plus 1 makes that 61. It’s not divisible by 10.

Please, how do you arrive at, 29?

The code I wrote loops through every other number starting at i =1, multiplies those numbers by 2, and subtracts 9 from each number that is over 9.

``````original array = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]

array after loop (and reversed per project requirements)= [1,5,3,6,1,8,4]

array doubled = [2, 10, 6, 12, 2, 16, 8]

Array substracting 9 from numbers over 9 = [2, 1, 6, 3, 2, 7, 8] = 29

``````

??? That’s the third element, not the second, which is where you should be starting.

Regardless of length, the starting position is `Array.length % 2`. An odd length will start at position 1 (the second element).

Think about this. We don’t need to reverse the list. Just compute where to begin and two step the rest of the way. The last element will never be involved.

As for the subtract 9, did you ever notice that the result is the same as if you add the digits?

``````9 * 2   =>  18

18 - 9  =>   9

1 + 8   =>   9
``````

To implement the digit sum approach means introducing an array to store intermediate values, then joining all the numbers into a string of digits, then splitting on each digit and adding up the values. Ends up being the same, either way.

Skip all this, and go back to the 29. Once you apply the changes, what is that number?

Ok, I have adjusted the code. But still when I try to log `total` to the console it has several lines of output instead of just one answer. I’ve included the codebyte so that you can see yourself. it should only say `29` in the console but instead it says

``````8
15
17
20
26
27
29
``````
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; const validateCred = arr => { let total = 0; for (let i = 1; i < arr.length; i+=2) { if (arr[i] * 2 > 9){ total += arr[i] * 2 -9 } if (arr[i] * 2 <= 9) { total += arr[i] * 2 } console.log(total) } }; validateCred(mystery1)

I Solved it thank you!!!

1 Like

Can’t believe how this was right under my nose…

``````8     7     2     3     6     1     2
``````

D’oh!

Still fail to see where this fits into the logic, but we must be getting closer.

``````v   v   v   v   v   v   v   v
4 5 3 9 6 7 7 9 0 8 0 1 6 8 0 8
8   6   3   5   0   0   3   0     <== there's y'r tickler
4   3  -3  -2   0   0  -3   0
0   0
``````

Does the fact the net is `-1` mean anything? The previous was positive one and failed, but this one passed.

Definitely some weirdness that turns to our way of seeing this…

``````const sum = (a, b) => +a + +b;
undefined
'4539677908016808'.split('').reduce(sum)
81
``````

Given the net is `-1` we end up with a divisible number.

2 Likes

Thank you for all your help I completed the project!

1 Like