DEV Community

Cover image for JavaScript beginner help

JavaScript beginner help

roxor on February 16, 2020

Hey guys, I have been studying JavaScript for 3 weeks already. I'm stuck with function and these 3 tasks. If anyone could help me I would really a...
Collapse
 
alvaromontoro profile image
Alvaro Montoro •

What problems or difficulties are you finding when writing these functions?

Collapse
 
roxor profile image
roxor •
  1. this is the first solution, don't know how to settle cubic, even don't is this function right.

function sum(n,m){
let s = 0; //sum
for(let i = n; i<=m; i++){
s+= i;
}
return s;
}
let p = sum (5,6);
console.log(p);

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

You almost have it!

The cube of a number n is n3, which is n * n * n, so you would just need to change one line:

s+= i;

into this:

s+= i*i*i;

Alternatively, you could use the Math.pow function and do something like this:

s+= Math.pow(i, 3);

Or another option would be using the exponentiation operator (**) which is equivalent to Math.pow():

s+= i**3;
Thread Thread
 
alvaromontoro profile image
Alvaro Montoro •

How about the second one? What errors do you get in the second exercise?

Thread Thread
 
roxor profile image
roxor • • Edited

wow, THANK YOU! :D :D :D
My motivation is rising uppp

This is third, but code does not work... what do you think?

function divisible(n,m,k){
result = 1;
for(i = n; i <=m; i++){
if(i%k == 0){
result = result / i;
}
}
console.log(divisble);
}

divisible(1,50,4)

EDIT: second one I have no idea how to set up

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro •

You want to know the number of numbers that are divisible by k and are between n and m, right?

Why are you doing this result = result / i;?