--- tags: coaching slideOptions: theme: white --- # Coaching Half-time Answers :orange_heart: --- ![](https://i.imgur.com/SGzz1aH.png) Q1: Which parts of the function declaration syntax are fixed, and which parts can be changed? --- Fixed: - The function keyword: `function` - The return keyword: `return` - Something after the return (otherwise, your function will return undefined) - The round brackets after `function` - The curly brackets after the round brackets --- Can be changed: - The name of the function - We can write [functions without a name](https://www.javascripttutorial.net/javascript-anonymous-functions/) - The name of the parameters - How many parameters we need - The logic inside of the function - What we return - Which [arguments](https://stackoverflow.com/questions/12874467/what-is-the-difference-between-arguments-and-parameters-in-javascript) we pass to the function --- As a more visual example, see below: ``` function NAME (ARGUMENTS) { FUNCTION_BODY } ``` Everything in all caps can be changed, everything else is fixed. --- ![](https://i.imgur.com/SGzz1aH.png) Q2: If we wanted to multiply three numbers, how would we alter our function? --- Something like this: ```javascript= function multiplyThreeNumbers(num1, num2, num3){ return num1 * num2 * num3; } ``` --- ![](https://i.imgur.com/57cdyRT.png =550x) Q3: Which of these are objects? --- There are two objects directly assigned to variables: - `myDog` - `dumplings` There is one nested object: - `dumplings.method` --- ![](https://i.imgur.com/BeutsSn.png =550x) Q4: Which of these are arrays? --- There is one array directly assigned to a variable: - `gems` There are two arrays nested in objects: - `myDog.hobbies` - `dumplings.ingredients` If you `console.log(typeof gems)` it will log 'object'. This is because [in JavaScript almost everything is an object](https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object). `colors` is a single string which happens to have three items separated with commas within it. This is what it might look like if ran the following: ```javascript= let myColors = ["red", "green", "blue"]; let colors = myColors.join(", ") ``` --- ![](https://i.imgur.com/k9ELHAp.png) Q5: What does this error tell us? --- Usually this means we've forgotten a piece of syntax. In this case, it's a `;` after `num` and before `i++`. Note the `^` arrow, which points to the exact character that is causing the issue. The error is on line 3, indicated by the number in the first line after the colon. --- ![](https://i.imgur.com/pbXR99f.png) Q6: What does this error tell us? --- This tells us that the variable we're trying to access has not been defined in the scope we're trying to access it within. This could be caused by one of three things: - We have not put `let`, `const` or `var` before declaring `result` - We have not declared `result` at all - We have tried to access result outside of the function (or other type of block) we've declared it Read more [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined). The error occurs on line 9, character 13. --- ![](https://i.imgur.com/VCDC3qN.png) Q7: What would be logged to the console on line 9? --- The output would be `[5,5,5,5,5]`. We are incrementing `i` each time our loop runs. Inside the loop we are pushing *the parameter* to the array. This is probably not what we wanted our function to do. Given the name of the function, we might like to push `i` instead of `num`. Well spotted if you caught this one, it was a little sneaky of me! :female-detective: --- ![](https://i.imgur.com/VCDC3qN.png) Q7b: What would be logged to the console on line 9 if you remove line 6? --- The function would output `undefined` as we have removed the return keyword. --- ![](https://i.imgur.com/KU9Qm9P.png =660x) Q8: What would be the output of the function calls on lines 11, 12 and 13? --- 11: 'Venus' 12: 'a' 13: 'Alpha' I think there's two stumbling blocks here: - `x < 3` catches everything less than 3 *but not* equal to 3. If we want everything *less than or equal to 3* we should use `<= 3` - The [`chatAt()` method](https://www.w3schools.com/jsref/jsref_charat.asp) references characters starting at index 0 - therefore the '1st' Character is 'a' and the 0th is 'c' of the string 'cat' --- ![](https://i.imgur.com/dX2zFAX.png) Q9: What mistakes have I made in this function? --- Quite a few! And a few that I hadn't noticed :sweat_smile: I missed a semi-colon after 'Roasting!' on line 5. This shouldn't break our code, read more [here](https://flaviocopes.com/javascript-automatic-semicolon-insertion/). Similarly, spacing is a little inconsistent but would not cause any issues in JavaScript. Also, using a mixture of `""` and `''` should cause no issues. We haven't defined `heat` before we've tried to use it. This would cause a refrenceError like we saw in Q6. Similarly, we have tried to use a variable on line 10: `thisMonth`. We have passed a parameter called `month` above. However, the `conosole.log()` on Line 10 will never run. A `return` statement will break out of a function. We have an `if(){}else{}` structure, which means everything will be caught before reaching line 10. We are not running the function, so if we'd like to see the output we'd need to call it: `weatherReport(4, 'June')` --- :rocket: