Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 22, 2022 09:53 pm GMT

JavaScript Functions: Learn by Demand

A classic of why you should not start with the technology (theory).
(It is short!)

https://www.youtube.com/watch?v=r2O5qKZlI50

Learn by Demand

When we start to learn something by theory, its easy to feel overwhelmed by the amount of information that exists for a determined subject.

Problem First

First, find a problem to solve, then you'll figure out what theory do you need to study to solve it.

I study what is necessary to accomplish a result, so I can learn it well, without getting frustrated, and dont need to memorize lots of details.

If you just started by now learning JavaScript, you maybe have encountered different terminologies that more create a gatekeeper and might make you feel unmotivated than make you understand what is going on.

So, lets learn with a problem.

The Dog Age Calculator

Its long believed that 1 dog year is equal to 7 human years.

We are going to create a function that will transform the dog's age (which will be inputted by the user) to what it is in human years. It is expected to have an output like the following String.

"Your dog is XX years old in human years"

Untitled

Here is one example of how function (black box) works.

This black box also holds the list of instructions, here is where it tells the function what to output.

  1. Receives an input with the Dog's Age.
  2. Creates a routine to transform Dog's Age to the equivalent in Human Years
  3. Output following the String.

First, we need to know how to declare a function.

Function Declaration

A function is created with an expression that starts with the keyword function, then goes the name of it.

So lets declare a function to calculate the dogs age.

function calcDogAgeToHumanYears(dogAge) {//Function Body}
  • We have used the keyword function.
  • We give it a descriptive name to calculate the dogs age to human years.
  • dogAge? What is this inside the parenthesis?

Parameters and Arguments

It is not easy to understand. When I started to learn to code I got confused with both terminologies. You're going to get used to it with practice.

When we are declaring a function, we use the parentheses or technically known as round brackets (Ive ever listened someone calls this like that) to hold placeholders that our function expects.

  • A function can take zero or more parameters.
  • Its up to you!

There are pre-defined functions (methods) in JavaScript that expect some parameters, this is one case where you cannot change.

  • Parameters or Slots
function calcDogeAgeToHumanYears(dogAge) {};//dogAge is holding a slot, an input that a function should receive
  • Arguments are the actual value, the specific value, of JavaScript data types that we give to run a function
//calling a functioncalcDogAgeToHumarYears(3);>> 'Your dog is 21 years old in human years'

The placeholder, the slot, was replaced by the actual data, the number 3.

Function Body

One great thing about coding is there is not only one, or right, answer.

Everyone here probably will have different ways to think about how to solve the same problem.

The problem: 1 dog year equals to 7 human years

My solution:

  1. Creates a new variable;
  2. Multiply the dogAge by 7, and store it into this new variable;
  3. Output a String with the value.
//keyword function + functionName + (parameter)function calcDogAgeToHumarYears(dogAge) {    //coding    let toHumanYears = dogAge * 7;    console.log(`Your dog is ${toHumanYears} years old in human years`);}//calling the function and using the argument with the number 3calcDogAgeToHumanYears(3);//output>> 'Your dog is 21 years old in human years'

Is it? Is done?

Yes. This function is serving its purpose.

It's time for you to practice! Refactor this with the return statement.

Now, you've one new thing to study and to apply.

Send your code to me, here on comments or on Twitter (@mpfdev)

Are you learning HTML/CSS?

Here is my last post about doing a section with CSS Floats:
Level-Ground: Section with CSS Floats


Original Link: https://dev.to/mpfdev/javascript-functions-learn-by-demand-1641

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To