Functions, what is it really used for and am stuck


#1

<

Function syntax

A function takes in inputs, does something with them, and produces an output.

Here's an example of a function:
var sayHello = function(name) {
console.log('Hello ' + name);
};

  1. First we declare a function using var, and then give it a name sayHello. The name should begin with a lowercase letter and the convention is to use lowerCamelCase where each word (except the first) begins with a capital letter.
  2. Then we use the function keyword to tell the computer that you are making a function
  3. The code in the parentheses is called a parameter. It's a placeholder word that we give a specific value when we call the function. Click "Stuck? Get a hint!" for more.
  4. Then write your block of reusable code between { }. Every line of code in this block must end with a ;.

You can run this code by "calling" the function, like this:
sayHello("Emily");

Calling this function will print out Hello Emily.

Instructions

  1. On line 11, call the greeting function and put in a name that you want the greeting function to include.
  2. Press "Save & Submit Code" and see the function get into action! Saves you so much time.

?

Hint

When we want to join together two strings, we put a plus sign.

console.log("Hey" + "you"); will print out Heyyou. That's not what we want!

If you want a space between words, you must add that space as well!

console.log("Hey" + " " + "you"); will print out Hey you

This joining of strings is called concatenation
.>

My answer below

// Below is the greeting function!
// See line 7
// We can join strings together using the plus sign (+)
// See the hint for more details about how this works.

var greeting = function (name) {
console.log("Great to see you," + " " + name);
};

// On line 11, call the greeting function!
function ("jo").>

<

and result is
.SyntaxError: Expected identifier

`


#2

when you call a function, you should use the function name (greeting) not the function keyword, that will create a new function

Also, please use markup so we can clearly see your code:


#3

Tying it all together

Why is the code organized like it is on lines 2-5?

The computer can understand the code without such spacing. But it makes editing a lot easier and is best practice.

Do I have to put a semi-colon at the end of each line of code in the reusable block? And at the end of the entire function?

Yes. At the end of each line of code (within the { }) and after the entire function (after the { }), please put a semi-colon. The semi-colon acts like a period in a sentence. It helps the computer know where there are stopping points in the code.

Instructions

A big part of programming is debugging. That just means figuring out what the heck went wrong with your code. Why didn't it run?
01. Look at line 9. It has many syntax errors. See how lack of spacing makes debugging hard?
02. Fix the function on line 9. Make sure the syntax is right. Make sure it looks nice.
03. Call the greeting function once it is fixed! Don't forget to pass in a specific name.

How do you correct this code, I tried everything, I just didn't really understand the question properly code is below.

// Nicely written function:
var calculate = function (number) {
var val = number * 10;
console.log(val);
};

// Badly written function with syntax errors!

greeting var func{name}(console.log(name)))}


#4

url exercise for anyone who is trying to help:
https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/0/5?curriculum_id=506324b3a7dffd00020bf661

the problem is here:

greeting var func{name}(console.log(name)))}

this function is poorly written (one line, and many mistakes) you have to make this function work


#5

Return keyword

Nice job! Now, when we call a function, we don't always want to just print stuff. Sometimes, we just want it to return a value. We can then use that value (ie. the output from the function) in other code. Let's learn about the return keyword, then we'll see how to use functions with an if / else statement in the next exercise!

The return keyword simply gives the programmer back the value that comes out of the function. So the function runs, and when the return keyword is used, the function will immediately stop running and return the value.

Instructions

In our example we have a function called timesTwo() that takes in a number and returns the number multiplied by two.
01. On line 7, after the equals sign, call the function timesTwo with any parameter you want
02. Line 8 prints out newNumber. Notice how the value we return from timesTwo() is automatically assigned into newNumber.

?

Hint

To call the function, we just use the name of the function. We then put in a value for the number parameter. eg. timesTwo(8);

This is what i wrote.

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var timesTwo = function (timesTwo)
{return (timesTwo * 2)};

timesTwo (5);

But its on about a function, no idea.


#6

this bit of code:

// Call timesTwo here!
var timesTwo = function (timesTwo)
{return (timesTwo * 2)};

timesTwo (5);

timesTwo(5) (last line) is the function call, not sure what the rest is. I recommend to remove it


#7

#8