Skip to content

Instantly share code, notes, and snippets.

@ippaiva
Created October 4, 2018 18:57
Show Gist options
  • Save ippaiva/05e997c17367e92b1b686576214d285b to your computer and use it in GitHub Desktop.
Save ippaiva/05e997c17367e92b1b686576214d285b to your computer and use it in GitHub Desktop.
intro-functions created by ippaiva - https://repl.it/@ippaiva/intro-functions
// *********Return values
function addNumbers(){
return 5 + 2;
}
var result = addNumbers();
console.log("The return total is : " + result);
console.log('\n');
// ***********Arguments
function sumNumbers(numberOne, numberTwo) {
return numberOne + numberTwo;
}
var result = sumNumbers(2,2);
console.log("The result is " + result);
console.log('\n');
// ************Function arguments value
var animalArray = ["Dog", "Fish", "Cat"];
function printLength(arrayToCount){
var theLength = arrayToCount.length;
console.log("The length of your array is " + theLength);
}
printLength(animalArray);
console.log('\n');
//********Difference between global variable and local variable.
// *********Declaring a global variable:
var company = "Ironhack";
function sayHello(){
var firstName = "Josh"; //local var
console.log("Name inside of the function: " + firstName);
console.log("We are in a " + company + " course.");
}
sayHello();
console.log(company);
//console.log(firstName);// Throws an error because there's no variable firstName in scope
console.log('\n');
//************Example How Js look First into Local Variables.
var city = "SãoPaulo";
function printCityName (city) {
// It will print "Miami" since it is declared the local scope as an argument.
console.log(city);
}
printCityName("Miami");
function printCityName2 () {
// It will print "Madrid" since it has no variables in the local scope with that reference.
console.log(city);
}
printCityName2();
console.log(city);
console.log('\n');
//Create a program that accomplishes the following: Create a function:
// addNumbers that takes 2 parameters and returns the sum of those parameters.
function addNumbers(a,b) {
return a + b;
}
var result = addNumbers(5,10);
console.log("The result is " + result);
console.log('\n');
//subtractNumbers that takes 2 parameters and returns the difference of those parameters.
function subNumbers(a,b) {
return a - b;
}
var result = subNumbers(5,10);
console.log("The result is " + result);
console.log('\n');
//multiplyNumbers that takes 2 parameters and returns the product of those parameters.
function multiplyNumbers(a,b) {
return a * b;
}
var result = multiplyNumbers(5,10);
console.log("The result is " + result);
console.log('\n');
//divideNumbers that takes 2 parameters and returns the quotient of those parameters.
function divideNumbers(a,b) {
return a / b;
}
var result = divideNumbers(5,10);
console.log("The result is " + result);
console.log('\n');
//calculator that takes 3 parameters. The first two parameters are the numbers. The third parameter, called operation is the operation you will execute.
//If the operation is “addition”, you should execute the function addNumbers.
function calculatorAdd (a, b, operation) {
return addNumbers(a,b);
}
var result = calculatorAdd(20,10);
console.log("The operation addition is " + result);
console.log('\n');
//If the operation is “subtraction”, you should execute the function subtractNumbers.
function calculatorSub (a, b, operation) {
return subNumbers(a,b);
}
var result = calculatorSub(50,25);
console.log("The operation subtraction is " + result);
console.log('\n');
//If the operation is “multiplication”, you should execute the function multiplyNumbers.
function calculatorMult (a, b, operation) {
return multiplyNumbers(a,b);
}
var result = calculatorMult(1,50);
console.log("The operation multiplication is " + result);
console.log('\n');
//If the operation is “Division”, you should execute the function divideNumbers.
function calculatorDiv (a, b, operation) {
return divideNumbers(a,b);
}
var result = calculatorDiv(1565,3);
console.log("The operation Division is " + result);
console.log('\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment