#Multiples of 3 or 5

208 messages · Page 1 of 1 (latest)

gilded breach
#

https://www.codewars.com/kata/514b92a657cdc65150000006/train/javascript


Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0 (for languages that do have them).

Note: If the number is a multiple of both 3 and 5, only count it once.```
```function solution(number){
  
}```
Codewars

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

#

my first thought is to make an if statement

#

but i don't know what key words to use

#
  do something...
}```
#

but i don't think that's right

#

i think i need to use like a '%' ?

random kestrel
gilded breach
#

if(number%5 == 0) ?

#

something like that?

vivid jewel
#

Looks right to me

gilded breach
#

but how do i nest all possible numbers into an array that's derived from 'number'

vivid jewel
gilded breach
#

yeah but that checks if it's true

#

what is the next step to list all possible division results?

random kestrel
#

What does your code look like?

gilded breach
#

no code yet

random kestrel
#

like the whole thing

#

start with a loop

gilded breach
#
  if (number%5==0){
    let numArr[]
  }
}```
random kestrel
#

and generate numbers from 1 to number

gilded breach
#

yeah i tried a for loop, but i have no idea how to set that up

#

function solution(number){
for (let i = 0; ;i++){

}
}

#
  for (let i = 0; ;i++){
    
  }
}```
random kestrel
#
  console.log(i)
}
gilded breach
#
  for (let i = 0; i%5==0 || i%3==0 ;i++){
    
  }
}```
gilded breach
#

why would i count from 0 up to number?

random kestrel
#

That just means make a variable i that is 0, if i is less than or equal to number, increment i

gilded breach
#

oh i guess i need to do the check

gilded breach
#

trying to figure out how to use it

random kestrel
#

That will give you all the number from 0 to number. Now, you would use that to filter the numbers that are divisible by 3 or 5

gilded breach
#
  for (let i = 0; i<=num; i++){
    if (i%5==0){
      i.push();
    }
  }
}```
#

something like that?

#

but i need to make an array to push the numbers into

random kestrel
#

yes

#

remember to call the push method on the array

#

i.push();

gilded breach
random kestrel
#

const solArr = []

gilded breach
#

that's what i put

#

oh lol

#

haha thanks

#
  const solArr=[];
  for (let i = 0; i<=num; i++){
    if (i%5==0){
      solArr.push(i);
    } else if (i%3==0){
      solArr.push(i);
    }
  }
}```
#

so now how do i add up all the numbers inside the array?

random kestrel
#

You can combine the if conditions using the OR (||) operator

#

like you showed us before

gilded breach
#

okay so how do i add up all the numbers inside the array?

random kestrel
#

think how you would do it in real life

#

or think how you would explain it to a child

#

because that's pretty much what a computer is, but faster 😄

gilded breach
#

i need another loop statement?

random kestrel
#

yes

gilded breach
#
  const solArr=[];
  for (let i = 0; i<=num; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=...; i++){
    
  }
}```
#
  const solArr=[];
  for (let i = 0; i<=num; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    
  }
}```
random kestrel
#

there you go

#

now you need to get each element from the array

#

by using i

gilded breach
#

hmm

#

it's not coming to me

random kestrel
#

hint: my_array[0] gives you the first element, my_array[1] gives you the second element in the array

#

try to print all the elements in the array first

gilded breach
#
  const solArr=[];
  for (let i = 0; i<=num; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    return solArr[i]++;
  }
}``` xD
random kestrel
#

you got the right idea, but not quite haha

#

return will end the function immediately

gilded breach
#

wait i need to create another array?

random kestrel
#

return solArr[i]++; This will get the element at i index, increment it and return it

random kestrel
gilded breach
#

okay good

random kestrel
#

why do you think you need another array?

gilded breach
#

i don't know how to pull the numbers out

gilded breach
#

it's cycling through the array now

#

but where do i put the individual values

#

so that i can add them all

random kestrel
gilded breach
#
  const solArr=[];
  for (let i = 0; i<=num; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    return solArr[i] ;
  }
}``` this is what i have rn
random kestrel
#

yeah, you don't need return

#

return ends the function

gilded breach
#

yeah i'm trying to end the function at this point

#

add all numbers at a clean last line

random kestrel
#

but you haven't summed the numbers

gilded breach
#

i'm trying to sum them all together in the return

random kestrel
#

you can't do it like this

gilded breach
#

okay i see

random kestrel
#

try printing the numbers in the array

gilded breach
#

cout?

#

how do i print in js?

random kestrel
#

console.log

#

now, how would you add them up?

gilded breach
random kestrel
#

it should number instead of num

gilded breach
#

xD ty

random kestrel
#

cool

gilded breach
#

it works!

random kestrel
#

almost there

gilded breach
#

almost >.<

random kestrel
#

how would you add them?

gilded breach
#

i need to take each number out individually

#

then add them all together

#

i guess i need a for loop?

#

i have no idea

random kestrel
#

Think like a computer. You can only look at one number at a time. Whereas humans can look at the whole thing. Now, let's say you have numbers 1, 4, 8. Now,

pick the first number, 1
add it to your total (1)
pick the second number, 4
add it to your total (5)
pick the third number, 8
add it to your total (13)
total is 13
gilded breach
#

oh

#

hmm

#

still don't know how to put that in

random kestrel
#

use a variable

#

to keep track of the total

gilded breach
#

total = solArr[i]

random kestrel
#

total = total + solArr[i]

gilded breach
#

solArr[i] + solArr[i];

random kestrel
gilded breach
#
  const solArr=[];
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    let total = solArr[i];
    solArr[i]++;
  }
}```
random kestrel
gilded breach
#

what about this

#
  const solArr=[];
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    let total = solArr[i];
    return solArr[i]++;
  }
}```
#
  const solArr=[];
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    let total = solArr[i];
    return total = total + solArr[i];
  }
}```
random kestrel
#

That code is basically incrementing the element at first index and returning it

gilded breach
#

the increment will be the next in line in the array

random kestrel
#

return ends the function right there. It won't let the for loop finish. So, you never reach the end of the array. You see the first element and quit

gilded breach
#
  const solArr=[];
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    let total = total + solArr[i];
  }
  return total;
}```
random kestrel
#

better

#

now

gilded breach
#

that's recurssion right?

random kestrel
#

No, recursion is when a function calls itself

gilded breach
#

oh so i'm trying to make a variable call itself, which it wont do?

random kestrel
#

it is fine. The right side of the assignment is executed first

#

and is assigned to whatever is on the left

gilded breach
random kestrel
#

It is trying to execute total + solArr[i], but it doesn't know what total is

gilded breach
#
  const solArr=[];
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    let total = solArr[i];
    total = total + solArr[i];
  }
  return total;
}```
random kestrel
#

This will reset total on each iteration

gilded breach
#

i have to define total outside?

random kestrel
#

yes!

gilded breach
#
  const solArr=[];
  let total = 0;
  for (let i = 0; i<=number; i++){
    if (i%5==0 || i%3==0){
      solArr.push(i);
    }
  }
  for (let i = 0; i<=solArr.length - 1; i++){
    total = total + solArr[i];
  }
  return total;
}```
random kestrel
#

it should work...

gilded breach
#

i had to make a minor change!

#

it worked!

#

wow why was that so hard for me tf

random kestrel
#

You did pretty good, actually

gilded breach
#

i did?

#

i feel like i just dredged through mud

random kestrel
#

I just gave you some hints, and you figured it out

gilded breach
#

can we be friends lol

#

i'm gonna need more of your time hahaha

random kestrel
#

I barely have any free time 🥲

gilded breach
#

dope it passed 111 tests and failed 0

#

hell yeah

random kestrel
#

great

gilded breach
#

thank you so much for your help

#

breaking it down like that really helped

random kestrel
#

👍

astral capeBOT
gilded breach
#

oh wow

#

there's way simpler solutions

#
  var sum = 0;
  
  for(var i = 1;i< number; i++){
    if(i % 3 == 0 || i % 5 == 0){
      sum += i
    }
  }
  return sum;
}```
#

i want to be able to code like this

random kestrel
#
    return sum(filter(lambda x: not x % 3 or not x % 5, range(3, number)))``` This is my solution in python
#

There are people who did better than me haha

gilded breach
#

how do i get good like that

#

i'm like a year in

random kestrel
gilded breach
#

honestly this second year has been more clarity of direction for me

random kestrel
gilded breach
#

it's so hard to find out where to start

random kestrel
#

you can also learn from others

gilded breach
#

like i know exactly what to work on now cuz i know where it's all applicable, but i just suck at coding lol

#

i would like to get a job soon

gilded breach
#

i don't think it can be condensed any further though

#

what do you think?

random kestrel
#

I mean, JS has built in methods that will let you do stuff like that in one or two lines.

gilded breach
#

can you show me?

#

you're talking about this very exercise right?

random kestrel
#

yeah

#

There are probably some in the solutions section

gilded breach
#

the one i showed you was the most upvoted

random kestrel
#
  return number < 1 ? 0 : [...new Array(number).keys()].filter(n => n % 3 == 0 || n % 5 == 0).reduce((a, b) => a + b);
}```
#

This is a one line solution

gilded breach
#

oh yeah

random kestrel
#

You can make it even shorter

gilded breach
#

i don't even know what's going on in that one

random kestrel
#

solution = number => {number < 1 ? 0 : [...new Array(number).keys()].filter(n => n % 3 == 0 || n % 5 == 0).reduce((a, b) => a + b);}

#

That should work too...

gilded breach
#

this is ecs6 or whatever right?

random kestrel
#

yeah

gilded breach
#

doesn't that mean this kind of code will become outdated?

#

deprecated?

random kestrel
#

I don't think so

#

but idk

gilded breach
#

so wouldn't it be better to use the foundational method?

random kestrel
#

yeah, that's more readable too

gilded breach
#

also what are the processing speeds of the variances?

random kestrel
#

That has to be tested

gilded breach
#

i don't know how to do that lol

#

but i wonder which is faster

unkempt yarrow
#

Nah chaining methods look better imo. You just need to format them well

unkempt yarrow
#

Btw i think there is an O(1) solution for this

#

Gimme a sec to lay it out in my phone cus lazy to program it properly with pc

random kestrel
unkempt yarrow
#

Yep

#

Sum serie of diff by 3 plus sum serie of by 5 negate sum serie of diff by 15

#

Can i optimize it? Most likely

#

But too lazy to optimize cus that is math stuff