ch 4 - declaring functions

Get help with Code for Teens
DanceFourFun

ch 4 - declaring functions

Post by DanceFourFun » Tue Sep 18, 2018 6:40 pm

In the DIY for chapter 4 -

I don't understand enough about what went on in the chapter to be able to implement it in the DIY, even after the follow along and looking at the answer. Can you give some guidance or assistance? Thank you.

JeremyMoritz
Site Admin
Posts: 8
Joined: Mon May 14, 2018 10:19 pm
Location: Kansas
Contact:

Re: ch 4 - declaring functions

Post by JeremyMoritz » Tue Sep 18, 2018 9:20 pm

I am happy to assist. Can you please be a more specific about what it is that you're asking? Is there a line in particular that you find confusing? Also, approximately how old are you (this helps me to tailor my answer toward your age range)?

To get you started, answer this question: Can you create a function that will generate a random number every time you call it?

For instance, do you know how to complete this code?

Code: Select all

function getRandomNumberBetweenOneAndHundred() {
    // do you know what to `return` here to make this function work?
}

getRandomNumberBetweenOneAndHundred();  // this should return the value you want after you've finished working out the function above
Jeremy Moritz
Author of Code for Teens
www.CodeForTeens.com

dancefourfun

Re: ch 4 - declaring functions

Post by dancefourfun » Wed Sep 19, 2018 4:39 pm

I am 14 years old. I don't understand most of declaring functions. I was following along in the book really well until this chapter. I understand what to type once I have read the code from the book once or twice, but without something to look at, I don't know how to type it. I also don't know what to type to create the code you were asking about, or complete the other code, without something to copy.

I can create a random number between 0 and 1 by myself, but I don't understand how to put that in a function. I also don't understand the Math.floor even by itself, the console.log, and nesting functions.

JeremyMoritz
Site Admin
Posts: 8
Joined: Mon May 14, 2018 10:19 pm
Location: Kansas
Contact:

Re: ch 4 - declaring functions

Post by JeremyMoritz » Wed Sep 19, 2018 10:31 pm

If you can create a random number between zero and one, start with that. If you multiply that number by 100, you'll end up with a random number between z and 100:

Code: Select all

let randNumZeroToOne = Math.random();
let randNumZeroToHundred = randNumZeroToOne * 100;
console.log(randNumZeroToHundred);
Notice that this number has lots of digits after the decimal point. You use Math.floor() to strip away all those digits (rounding your number down to the floor):

Code: Select all

let newRandNum = Math.floor(randNumZeroToHundred);
Then, you need to RETURN this number inside the function:

Code: Select all

return newRandNum;
Put all that code inside of the function (between the { and the } ). Then whenever you need one of the numbers, just call that function

Code: Select all

getRandomNumberBetweenOneAndHundred();


If you're still having trouble, try doing a google search for "What is..." Math.random(), Math.floor(), console.log(), etc. You'll probably find many clear examples about how to use each one.

Hope this helps!

Oh and one more thing: after doing the google search, if you're still struggling with this concept, go ahead and try out the next chapter. Sometimes it takes seeing new examples to understand some of the foundational concepts. There's no shame in not getting it right away. If you can read code enough to understood the book well up until this chapter, then you've already got a big headstart over most 14 year olds (most don't know the first thing about coding). So don't be too hard on yourself. You'll be able to figure it out.
Jeremy Moritz
Author of Code for Teens
www.CodeForTeens.com

dancefourfun

Re: ch 4 - declaring functions

Post by dancefourfun » Thu Sep 20, 2018 3:15 am

Thank you so much! I think it will really help me. I appreciate you answering my questions.

Post Reply