DEV Community

Cover image for JavaScript beginner help
roxor
roxor

Posted on

JavaScript beginner help

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 appreciate it:

  1. Write sum function that returns the sum of cubes of numbers from n to m.
    Call the function and print its result in the console.

  2. Write function year that passes the Birth parameter.
    Birth year parameter represents the birth year of a person. The function should return how old a person is.
    The number of years a person has calculated based on their birth year and the current year that you can pull from your computer.
    Call the function and print the result in the console.

  3. Write function divisible by passing three parameters n, m and k. The function on the screen should print the number of numbers n to m that is divisible by k. Call the function

Top comments (23)

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;?

Thread Thread
 
roxor profile image
roxor • Edited

correct

hm, I try the divisible result. but now, when I better think about it that is stupid

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

It's not stupid. It is just not what you need.

So, inside that if you have verified that the number is divisible, you don't really need to do more operations with it. Just have a counter –you could use result– to track how many there are. You could do something like result++; instead.

Thread Thread
 
roxor profile image
roxor • Edited

I try is not working

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

Hmmmm... What did you end up doing? and what result are you expecting? I do divisible(1,10,3) and get 3 which is correct (because 3, 6, and 9 and divisible by 3).

Thread Thread
 
roxor profile image
roxor • Edited

my console looks like this

Can you show me how u write a code?

ƒ divisible(n, m, k){
result = 1;
for (i = n; i <= m; i++) {
if (i % k == 0) {
result ++
}
}
console.log(divisible);
}
divisible(1, 10, 3)

Thread Thread
 
roxor profile image
roxor

I do exactly like you, in console this ƒ show up

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro • Edited

The console.log should be with result. You are printing the wrong variable ;)

+result should be initialized to 0 instead of 1, but apart from that, you are one :)

Thread Thread
 
roxor profile image
roxor

oh man, now is working...

I start with 1 cuz when we do addition we start from 0, when we multiply we start from 1

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

Nice. So you got everything working?

Thread Thread
 
roxor profile image
roxor

yes, and the last one is number two. I don't have clue how to do this

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

For the second one you have to write a function that gets a number as a parameter that represents a year, and returns the age of the person. For that one you'll need to use the Date object (and the getFullYear() method) to get the current year and calculate the difference of the two.

Thread Thread
 
roxor profile image
roxor • Edited

let day = new Date();
let birthday = day.getFullYear;
function birh(n){
let calculate = day.getFullYear - n;
return(calculate)
}

console.log(n);

Thread Thread
 
roxor profile image
roxor

something like this?

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

Those are steps in the right direction. But take into account some things:

  • You should have the variables in the scope of the function (right now you are defining the variables globally)
  • The method getFullYear() is part of Date. Considering that you are doing let day = new Date() then you'd need to do day.getFullYear() to get the current year.
  • In the console.log, you should return the result of the function and not the value that you pass to it.

With those changes, the code would look like this:

function birth(n){
  let day = new Date();
  let day1 = day.getFullYear();
  let calculate = day1 - n;
  return(calculate)
}

let n = 2000;
console.log(birth(n));

One of the things that you should do too is having descriptive names for variables. Right now you are storing dates in day, the year in day1, the birth year in n... That will be helpful in the future to maintain the code and help debug it.

Thread Thread
 
roxor profile image
roxor

Understood, in case if u want to call in another function day.getFullYear must make new, cuz this is work only in block of function birth, right?

Thank you for advice, I must try do give more descriptive names for var.

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

The variables will only work in the function/block where they were defined. So if you want to use it somewhere else, you'll need to redefine it or make it global.

Thread Thread
 
roxor profile image
roxor • Edited

I get it, thank you for the explanation you help me a lot.
When I get a job, from my first paycheck I will buy you a coffee.

thank you one more time. Cheers!