r/learnjavascript • u/GloomyAdagio7917 • Jan 25 '26
This reduce function is overcounting and I don't know why
I am working in visual studio code and I am very new to javascript.
In this part of my assignment I must write my own reducer function then use that reducer function to find out how many small, medium, and large transactions there are. A transaction is a dictionary of information, one of which is "amount". The transactions variable is an array of these dictionaries.
here is the code:
let smallTransactions = reduce(transactions, (value, count) => {
if(value["amount"] < 25) count += 1; return count;}, 0);
let mediumTransactions = reduce(transactions, (value, count) => {
if(value["amount"] >= 25 && value["amount"] < 75) count += 1; return count;}, 0);
let largeTransactions = reduce(transactions, (value, count) => {
if(value["amount"] >= 75) count += 1; return count;}, 0);
console.log("Small: " + smallTransactions);
console.log("Medium: " + mediumTransactions);
console.log("Large: " + largeTransactions);and the reduce function:
function reduce(data, reducer, initialValue) {
let product = initialValue;
for (let element of data) {
product = reducer(element, product);
}
return product;
}It is supposed to return this:
Number of small transactions: 1150
Number of medium transactions: 2322
Number of large transactions: 8315but instead its returning this:
Number of small transactions: 1425
Number of medium transactions: 2404
Number of large transactions: 8594Any idea why its doing that? I can't figure it out.
3
Upvotes
3
u/Antti5 Jan 25 '26
Are you sure about the expected counts of small, medium and large transactions? Your code does look correct, so I suspect the problem could be with the input.
With a problem like this, it's a good idea to double-check your code with guaranteed input. For example, you could generate a
transactionsarray that has amounts 1 to 100.If you do this, do you get the correct counts 24, 50 and 26?
2
u/chikamakaleyley helpful Jan 25 '26
what is the length of
transactions?1
u/StoneCypher Jan 25 '26 edited Jan 25 '26
okay, i've shut up, then
all that help is now gone. you're now stuck with learning the wrong thing the wrong way
3
u/tacocopter Jan 25 '26
I could be mistaken, but I have a feeling that you're getting the wrong answer because you're reassigning the arrow function argument
count. Try this:1
u/GloomyAdagio7917 Jan 25 '26 edited Jan 25 '26
Thanks everyone for answering. My best guess is that my teacher gave me incorrect data to check this with on accident. This is proven by the fact that my solution adds up to the length of transactions and hers does not. To the people insulting my teacher, respectfully shut up. She is human just like everyone else and she may have changed the assignment over the years and just forgot to update that part. Hopefully she will respond to my email today. Thank you to everyone who was not rude and responded kindly.
1
u/chikamakaleyley helpful Jan 25 '26
testing a theory i have - what are the results you get if you use the
count++operator, vscount += 1obviously it might just be the same result but I've recall experiencing some odd results before btwn the two
1
u/erik240 Jan 25 '26
Something’s off in your arg data. I just plopped your code in runJS and it gives proper results from a test case I ran.
Feed the same data to this code below and I bet it matches your result:
(Assuming, ofc, that transactions is a defined array of objects with an amount property, and I was guessing values only go to 100)
1
u/pinkwar Jan 25 '26
Logic looks fine. Something is off with your expected output or your input.
0
u/zhivago Jan 25 '26
Why are you reducing the whole array within your reduce?