# Cash Register - instead of adding the numbers are concatenating

Hi Everyone, I’m pulling out my hair on this one and could use some help.

Just starting this challenge and want to total the CID (cash in drawer). I must be missing something very basic. The numbers are being treated like a string and concatenating instead of adding. WHen I do ‘typeof’ is says its a number.

WHen I look it up I get results relating to a float point issue (that is going to be my next hurdle I think?).

Heres the code:

``````function checkCashRegister(price, cash, cid) {

var change;
// Here is your change, ma'am.

var total = cid.reduce(function (tot, curr){
tot += curr[1];

console.log(tot);
});
console.log(total);

return change;
}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````
``````let str ="somestring";
let num = 23;
let result = str+num;
console.log(typeof result); //it will say string
``````

Thanks for the response. I understand you can not do addition with a string and a number. The trouble I’m having is when or why does it consider `curr[1]` a string?

When I do `typeof curr[1]` the result is ‘number’.

I think I found the solution but am not sure why it is necessary.

I added a zero at the end of the following line between the curly and regular brackets:

This gives the ‘total’ variable of the reduce() method an initial value

Problem you have is the initial value for reduce function.

Try with initial value 0.

``````var total = cid.reduce(function (tot, curr){
tot += curr[1];

console.log(tot);
}, 0);
``````

lol, yup, that did it.

Do you know why? If I don’t do this, my `total` also includes `curr[0]` and I’m not sure why.

reduce function has this option where you can provide an initial value for total, in your case 0. The link bellow points exactly to the explanation.

It is good habit to check MDN website or other websites when you using built in functions as they often have options like this.

Yes, I prefer w3 but didn’t see anything about type for the initial value of `total`.

1 Like

If you do not provide an initial value, the accumulator, in your case “tot” is the array [“PENNY”, 1.01], the first element of cid array.

You could do this also to bypass that

``````var total = cid.reduce(function (tot, curr){
if(typeof tot !== 'number') {
tot = 0;
}
tot  += curr[1];

console.log(tot);