Tyler Morales
Tyler Morales

Reputation: 1830

Trying to add all values in one one array to another in JS using reducer function

Edit: I found the issue: I have not accounted for when multiples of 3 and 5 match! Any hints on how to eliminate the duplicate numbers?

I'm trying to solve the first problem of the Euler Problems from Free Code camp: Multiples of 3 and 5 https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-1-multiples-of-3-and-5

The problem is this: Find the sum of all the multiples of 3 or 5 below the provided parameter value number.

When I look at my code, it works for the case of 10, but not any of the others. I have looked, tried another option but can not find the issue. Here is what I did.

My Thought Process and current implementation of task

  1. Made a loop listing all numbers until the parameter number
  2. Made two while loops one for multiples of 3 and one for multiples of 5
  3. In each while loop I wanted to continuously add the multiplier (3 or 5) until the num is less than the total of threes or fives I had to add +3 and +5 so the last number in the total array would not go over num

  4. I then took the total array and implemented the reduce function to get the sum of the threes and fives totals

Note: I am able to get an array of the values. In the case of 10, I got [3, 6, 9, 5]

My Code

function multiplesOf3and5(num) {
    let total = [];
    let threes = 0;
    let fives = 0;
    const reducer = (accumulator, currentValue) => accumulator + currentValue;

    for (let i = 1; i < num; i++) {
        while (num > threes+3) {
            //total.push(threes);
            threes += 3;
            total.push(threes)
        }
        while (num > fives+5) {
            //total.push(fives);
            fives += 5;
            total.push(fives)
        }
    }
    total = total.reduce(reducer);
    return total;
}
console.log(multiplesOf3and5(10))
console.log(multiplesOf3and5(49))
console.log(multiplesOf3and5(1000))

What I've tried to solve it:

-Tried individually summing within each while loop to get a sum for all the multiples of 3 up to num and the same for the second while loop

-I did this by adding two arrays, total1 and total to account for the sums of threes and fives respectively.

Test Cases:

multiplesOf3and5(1000) should return 233168. (I got 266333)

multiplesOf3and5(49) should return 543. (I got 633)

multiplesOf3and5(10) should return 23. (I got 23)

Upvotes: 0

Views: 109

Answers (2)

gaetanoM
gaetanoM

Reputation: 42044

My proposal with a reduced number of operations and no modulus op is:

function multiplesOf3and5(num) {
    let total = 0;

    for (let i = 3, j = 5; i<num; i += 3, ((j+5)< num && !!j) ? j += 5 : j=0) {
        total += (i + j);
    }
    for (let i= 0, j=0; j < num; i++, j=(i*3*5)) {
        total -= j;
    }
    return total;
}
console.log(multiplesOf3and5(10))
console.log(multiplesOf3and5(49))
console.log(multiplesOf3and5(1000))

Upvotes: 0

steliosbl
steliosbl

Reputation: 8921

Your current solution could be fixed by eliminating the duplicates from the two arrays (the ones that store the multiples). However, it is still a very roundabout way of achieving what you want. From your problem statement I don't see why you need to store the multiples and then get the sum as you have chosen to do.

Since you are looping through all integers between 1 and your parameter anyway, you could instead add them to the sum as soon as you come across them:

let sum = 0
for (let i = 1; i < num; i++) {
    if (i % 3 == 0 || i % 5 == 0) {
        sum += i
    }
}

That is, unless you have a particular reason why you have chosen to solve it this way, which you have not stated.

Upvotes: 1

Related Questions