Skip to content
← Back to Community
Please help me understand this.
Profile icon
JonMarsh

No tutorial video has shown me how to work with multiplying or defining functions with parameters. Does anyone have an example that this lesson is trying to teach me?
repl.it - easy-1 - Google Chrome 1_1_2020 1_58_04 PM

Voters
Profile icon
HaydenFuller1
Profile icon
JonMarsh
Comments
hotnewtop
Profile icon
Highwayman

Looks like python..
ok.. um.

When defining a function in python, simply state it with the def key word like so.

def func_name (param1,param2,param3): # stuff # stuff # stuff return # either a val or no val at all # Done.
Profile icon
JonMarsh

this is what I have....what am I doing wrong?
repl.it - easy-1 - Google Chrome 1_3_2020 2_29_03 PM
@Highwayman

Profile icon
Highwayman

@JonMarsh actually that looks like js in which case I will have to show you because all you syntax is correct but it ins’t doing what they asked at all. Let me walk you through it.

first, you define your function as you have:

function productOfValues(obj){ // ... }

(I will use obj instead of object since object is too close to the Object keyword)

Now that we have a function with an object guaranteed to have only numbers in it, we figure out a way to loop through the object. To do this we will use to super cool features of the Js language: the of keyword and the Object.keys() function.

There are many ways to use the for loop, on of these ways is called a ranged based for loop, this involves the of operator. Basically the way you use the of operator is when you declare a for loop and the var inside of it, instead of initializing the var to a number you say of and then the name of the array your going to be looping though. The var then automatically holds the next value of the array in the loop throughout. It’s quite handy. Here’s an example:

var arr = [ "Hello,", " ", "World!", "\n", "How", " ", "are", " ", "you?" ]; for(var i of arr){ // now i will be the next element each time. console.log(i); }

That will output

Hello, World! How are you?

The Object.keys() function takes a single json object and returns an array with all the current valid keys/members of that object.
Here’s an example:

var obj = { 'key1':'hello', 'key2': [ "keis", "loop", 46 ], 'Bob': 34, '14' : 'jk' }; Object.keys(obj); // => [ "key1", "key2", "Bob", "14" ]

Now we are going to use the of keyword to loop through all the keys generated by the object.keys() function to multiply the members together and return the result.

function productOfValues(obj){ var product = 1; for(var key of Object.keys(obj)){ product *= obj[ key ]; } return product; }

Yay.

Profile icon
JonMarsh

wow very in depth, thank you. I will see if I can understand [email protected]

Profile icon
JonMarsh

what is "i" again? what does it [email protected]

Profile icon
Highwayman

@JonMarsh maybe. I still don’t know what language you are using so that’s js. What language are you using?

Profile icon
Highwayman

@JonMarsh i is just a representation of the first variable you declare for that for loop. It’s a super popular convention actually to name that variable i.
for(var i = 0;i < 10;i++) // the var could be named anything, but I was calling it i.