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!
Task Answer - Video Linkβ
πΉ 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.