Reading part of Chapter 3 (Functions & Scope), pages 85-99 from JavaScript & jQuery: Interactive Front-End Web Development
-
If we have a function defined as
function sayHello(){console.log("Hello!")}, what is the difference between enteringsayHelloandsayHello()in the console?sayHello()is correct function name. In round brackets parameters of the function is placed. If the function doesn't have any parameter name of the function is typed with round brackets without any signs inside it. -
What is the difference between function parameters and arguments?
Parameter is variable in the declaration of function.
Argument is the actual value of this variable that gets passed to function.
-
What is the keyword return used for?
The keyword return is used to return a value to the code that called the function.
-
How are local variables better than global variables? Are there instances you can think of where you might want to use a variable that is globally scoped over local?
When a variable is created inside a function using the var keyword, it can only be used in that function. It is called a local variable or function-level variable.
If you create a variable outside of a function, then it can be used anywhere within the script. It is called a global variable.
Global variables are stored in memory for as long as the web page is loaded into the web browser. This means they take up more memory than local variables, and it also increases the risk of naming conflicts. For these reasons, you should use local variables wherever possible.
Java Script
-
Work through problems in the console about functions.
- The Fortune Teller
function tellFortune(numberOfChildren, partnerName, geoLocation, jobTitle) { var predict = "You will be a " + jobTitle + " in " + geoLocation + ", and married to " + partnerName + " with " + numberOfChildren + " kids."; return predict; } tellFortune(2, "Joe", "San Diego", "teller");- The Age Calculator
function calculateAge(birthYear, currentYear) { var withBirthday = currentYear - birthYear; var NoBirthday = withBirthday - 1; var possibleAge = "You are either " + NoBirthday + " or " + withBirthday + "."; return possibleAge; } var dateToday = new Date(); var currentYear = dateToday.getFullYear(); calculateAge(1993, currentYear);- The Lifetime Supply Calculator
function calculateSupply(currentAge, estimatedAmount) { var maximumAge = 90; var yearLeft = maximumAge - currentAge; var perYear = estimatedAmount * 365; var perFullYear = estimatedAmount * 366; var numberFullYear = yearLeft / 4; var number365Year = yearLeft - numberFullYear; var totalEat = (numberFullYear * perFullYear) + (number365Year * perYear); var message = "You will need " + totalEat + " to last you until the ripe old age of " + maximumAge + "."; return message; } calculateSupply(24, 81); -
Practice through some JavaScript basics exercises on w3resource and check your code against their solutions.
Command Line Crash Course
- Do all of the exercises: 1-15
Git and GitHub
-
We want you to git really good at Git and Github, please read through the the Git handbook from GitHub here.
-
Also, this blog has references to commands and their meaning that might be helpful.