Skip to main content

Task 7: Understanding and Using JavaScript Functions

Start with Affirmation​

  • "I am destined for greatness. Every step I take brings me closer to my goals. I embrace challenges, knowing they are stepping stones to success. My mind is focused, my heart is determined, and my actions are aligned with my dreams."

Daily Affirmations for Success

  • βœ… I am capable, confident, and unstoppable.
  • βœ… Success flows to me because I take consistent action.
  • βœ… I attract opportunities that align with my purpose.
  • βœ… My hard work and persistence create abundance.
  • βœ… I am always learning, growing, and evolving.
  • βœ… I believe in my potential and trust my journey.
  • βœ… I turn setbacks into comebacks with resilience.
  • βœ… I am grateful for every success, big and small.

Success Reflection​

"Every effort I made today has planted seeds for my future success. I release any doubts and trust the process. My dreams are possible, and I am getting closer every day. I am grateful, I am strong, and I am ready for more!"

πŸ’ͺ Repeat daily, feel the power, and watch your success unfold!


πŸ”Ή If you don’t understand the task, watch the video above for the answer.


Task​


** Step 1: What is a Function?​

Imagine you have a robot assistant. Instead of telling it every little step, you just say: β€˜Make me a sandwich!’ and it knows what to do. A function is like thatβ€”it’s a set of instructions that does something when you call it."*

Here’s how you write a function in JavaScript:

"This function does one thing: it returns the text 'Hi, Great to have you here !' whenever we use it."

function greet() {
return "Hi, Great to have you here!";
}
console.log(greet());

Step 2: Functions with Parameters​

What if we want to greet different people instead of always saying the same thing?

That’s where parameters come in! They allow us to pass information to the function."*

function greet(name) {
return "Hi " + name + ", nice to meet you!";
}

console.log(greet("Enoch"));


Now, instead of always greeting the same person, we can pass different names."

Step 3: Returning Values in a Function​

A function can return a result that we can store and use later. Let’s create a function that doubles a number.

function doubleMe(number) {
return number * 2;
}
let result = doubleMe(5);
console.log(result);

Step 4: Functions with Multiple Parameters​

function addNumbers(num1, num2) {
return num1 + num2;
}

console.log(addNumbers(3, 7)); // Output: 10
console.log(addNumbers(15, 5)); // Output: 20


Step 5: Functions That Return Multiple Values​

One solution is to return an object:

function getUserInfo(name, age) {
return {
username: name,
userAge: age
};
}
let user = getUserInfo("Enoch", 39);

What Did We Learn?

Awesome job! Let’s quickly go over what we learned today:

  • βœ… What functions are and how they make code reusable.
  • βœ… How to create a function using function functionName().
  • βœ… How to pass values (parameters) into a function.
  • βœ… How to return a value from a function.
  • βœ… How to return multiple values using objects.