Need help with Functions, Arguments, & Parameters

Hello, I’m new to the website and new to coding. I’m a student currently working on a precourse assignment for Lambdaschool, and I am stuck on an assignment that I can’t seem to figure out. I have Exercise One correct, but I can’t get Exercise Two right.

// Exercise One: 
// Step One: Create a function called 'parametersExercise'. 
// Step Two: This function will need to take two parameters, call them 'param1' and 'param2'
// Step Three: The function should console log the second parameter.


// DO NOT CHANGE ANYTHING ON THE NEXT FOUR LINES!
function exerciseTwo(){
  function sayMyName(parameter1){
    console.log(parameter1);
  }
}

// Continue below this line:
function parametersExercise(param1, param2){
  console.log(param2);
}

// Exercise Two:
// Step One create a variable called 'myName' and assign it the a string of your name.
// Step Two: Call the function called "sayMyName",passing the 'myName' variable as it's only argument.
// NOTE: You do NOT need to create the function (sayMyName), doing so will break the test. 
//       It has been created for you.
let myName = "Chris";
sayMyName(myName);

This is the only instruction given to me, and when I attempt to run the code, I receive the message.

ReferenceError: sayMyName is not defined
at eval:25:1
at eval
at new Promise

The issue here is, sayMyName() is a function defined INSIDE another function, exerciseTwo(). It is local to that function, so you won’t have access to sayMyName() outside of exerciseTwo(), which is where you’re trying to use it.

How to resolve this? Not easily, if you aren’t allowed to change anything on those first four lines.

Can you perhaps provide a link to the original exercise?

I’m not allowed to provide links just yet on this website yet since I am still new apparently. I just removed the html portion of the web link. I hope I shared this correctly.

repl.it/talk/share/JS-Assignment-7-Arguments-and-Parameters-1/13674

So I see the problem. Where it says “don’t change anything in the next four lines”? You added a line right after that, a fifth line, closing off the exerciseTwo()before you create your function. They want your functions to all be created INSIDE of exerciseTwo() – and if they had been, they would have access to any functions defined within it.

1 Like

Ok, that makes sense. I decided to give it another look using that knowledge, and I made the minor change of simply switching the close off from line 12 to the very end. Now I’m getting an “undefined” message when I try to run it, but it won’t tell me specifically what is undefined. I tried figuring it out on my own by looking up some information online, but I am still having trouble understanding it.

// Exercise One: 
// Step One: Create a function called 'parametersExercise'. 
// Step Two: This function will need to take two parameters, call them 'param1' and 'param2'
// Step Three: The function should console log the second parameter.


// DO NOT CHANGE ANYTHING ON THE NEXT FOUR LINES!
function exerciseTwo(){
  function sayMyName(parameter1){
    console.log(parameter1);
  }
  

// Continue below this line:
function parametersExercise(param1, param2){
  console.log(param2);
}
// Exercise Two:
// Step One create a variable called 'myName' and assign it the a string of your name.
// Step Two: Call the function called "sayMyName",passing the 'myName' variable as it's only argument.
// NOTE: You do NOT need to create the function (sayMyName), doing so will break the test. 
//       It has been created for you.
let myName = "Chris";
sayMyName(myName);

}

Try adding a single line AFTER closing that function off – you still need to call the outer function. So, at the very very very end, add this:

 exerciseTwo();

and see what happens.

Ok, I included exerciseTwo(); after the final closing tag on line 26 in Exercise Two. I ran it, and received this message.

Chris
=> undefined
1 Like

And that is exactly right. If your function actually returned anything, that would be shown there instead of undefined. The Chris is the console.log(parameter1) statement inside the sayMyName(), so you’ve got it working.

For some reason it still tells me that I failed both assignments.

My assignment allows me to preview my test results, and they came back as failed.

How about a screenshot of the failed messages? Do they include some extra detail we’re missing?

EDIT: Reuploaded.

What does ‘show details’ tell you?

The More Info tab? It just brings up what looks to be an error log.

Error: Expected 'undefined' to be 'function'.
    at https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:284873
    at t (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:284913)
    at e.I [as expectationResultFactory] (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:268766)
    at e.addExpectationResult (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:264218)
    at t.r [as addExpectationResult] (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:267935)
    at t.toBe (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:284240)
    at eval (eval at n.evaluate (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:223:152082), <anonymous>:18:39)
    at https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307677
    at n.run (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307901)
    at n.execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:306672)
    at e.R [as queueRunnerFactory] (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:269341)
    at e.execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:264960)
    at e.fn (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:317788)
    at https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307611
    at n.run (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307901)
    at n.execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:306672)
    at R (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:269341)
    at e.fn (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:317606)
    at https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307611
    at n.run (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:307901)
    at n.execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:306672)
    at R (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:269341)
    at t.execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:318340)
    at execute (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:670:270142)
    at i.<anonymous> (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:314:51219)
    at i.emit (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:314:190070)
    at r (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:141:9965)
    at Object._custom_runSingleUnitTests (https://replbox.repl.it/public/replbox_javascript.4166a72583fcf5bcf9e4.bundle.js:141:11434)
    at https://replbox.repl.it/public/secure/runner.js:90:15

I’ve already completed the HTML and CSS section of the precourse, and this my 7th Javascript assignment, I have not had this much trouble with completing a test yet. Is it possible that it might be a problem on their end with creating the assignment?

Alright, from what I can tell you have a few things out of order for some reason or another.

Lines 15 through 17 are in the wrong place, which might be throwing off your code. That particular line of code should be on lines 5-7. Then, you need to delete your code on line 27.

After that, I think you should be good to go. I think one random issue I had on this particular problem was getting an error for the name parameter I passed. I passed ‘Nick’ and it said it expected ‘Dan’ for some reason. So, I changed it to ‘Dan’ and it worked fine, but that may be a random hiccup.

1 Like

Oh wow, that actually worked. I thought where it read “// Continue below this line:” Meant that I had to actually put my Exercise One answer right below that one. Thanks!

1 Like

Yeah, that is actually a little confusing. They should maybe change that problem up to read a little easier. Glad it works for you though!

1 Like