Functions - Reading Assignment

const gata = function(x, y) {
let numb = 1;
for (let count = 0; count < y; count++) {
numb *= x;
}
return numb;
};

console.log(gata (2,55));

  1. var and let
    let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

  2. Pure function.
    A pure function is a specific kind of value-producing function that not only
    has no side effects but also doesn’t rely on side effects from other code—for
    example, it doesn’t read global bindings whose value might change. A pure
    function has the pleasant property that, when called with the same arguments,
    it always produces the same value (and doesn’t do anything else). A call to
    such a function can be substituted by its return value without changing the
    meaning of the code.

Solution to the Recursion exercise

function isEven(wholenum) {

if (wholenum < 0) {
return (isEven(-wholenum))
}else if (wholenum == 0) {
return true;
}else if (wholenum == 1) {
return false;
}else {
return isEven(wholenum - 2)
}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1))

  1. There are three ways to define a function in JavaScript:
    • Using: const functionNameHere = function(variables_for_function) { /* Function here */};
    • Using: function functionNameHere(variables_for_function) { /* Function here */};
    • Using: const functionNameHere = (variables_for_function) => { /* Function here */};

  2. “let” makes the variable work only for the block where it was defined, “var” makes is work for the whole function or the script depending how is defined.

  3. A pure function is a function that directly spits an output with no side effect involved.

  1. In JavaScript you need to use the keyword function, define a name and within parenthesis pass in argument(s). Than you need to write in curly braces, the instructions for the function to execute.

  2. With let you can limit the scope of the variable to the block, statement or expression in which it is placed. If a function has multiple blocks, it won’t interact with other blocks within the same function. This is unlike var which does not care about the scope of blocks or functions. You cannot re-declare a let variable and they cannot be globally accessed unlike a var variable.

  3. A pure function describes a function in which an input will always produce the exact same result output. A pure function produces no side effects.

A function is created with an expression that starts with the keyword function. They can but don’t need a set of parameters. By using the keyword function then giving it a parameter ( ) and then a body { } to create an expression.

let applies locally to the block and cannot be seen from a global perspective
var applies globally to the block and outside provided its not in the function

A pure function is a value producing function that can be substituted with its return value without changing the meaning of the code. A pure function does not product a side effect and does not rely on global binding that may change. When called with the same arguments, it always produces the same value.

1. How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) and a body, which contains the statements that are to be executed when the function is called. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.

function functionName() {
// function body or block
}

2. What is the difference between var and let in regards to scope?
Bindings declared with let and const are in fact local to the block that they are declared in, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it. In pre-2015 JavaScript, only functions created new scopes, so old-style bindings, created with the var keyword, are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function.

3. What is a pure function?
A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code—for example, it doesn’t read global bindings whose value might change. A pure function has the pleasant property that, when called with the same arguments, it always produces the same value (and doesn’t do anything else). A call to such a function can be substituted by its return value without changing the meaning of the code. When you are not sure that a pure function is working correctly, you can test it by simply calling it and know that if it works in that context, it will work in any context. Nonpure functions tend to require more scaffolding to test.

How can you create a function in Javascript?

function name(parameter){
body ;
return expresion ;
}

it is not always necessary to have a return part (for instance
if the function just implements a side effect).

What is the difference between var and let in regards to scope?

Variables defined with let , that are inside the body of a function are local
to that body.

Variables defined with var, that are inside the body of a function are global

For instance, if you have

if(true){
let y=20;
var z=30;
console.log(x+y+z)
};
first, run it. If later, on the console, you type y it says not defined but if you type z returns 30.

What is a pure function?
A function that produce a value and this value does not depend on side
effects of other functions nor acts as a side effect in any other part of code.

For instance, the next is pure:

function sum(a,b){
return a+b;
}

The function:

function TodayIs(){
console.log("Today is "+Date());
}
is not pure, because depends on information not inside the scope
of the function.

How can you create a function in Javascript?
function FunctionName( Par1, Par2 …) { //do stuff }

What is the difference between var and let in regards to scope?
var’s not inside a function are global, let’s are local to their block

What is a pure function?
a function with no side effects or affected by side effects of other functions

  1. function square(x) { return x * x; }

  2. let statement declare a block scope local variable.
    var is a keyword which defines a variable globally or locally to an entire function regardless of block
    scope.

  3. A pure function is a specific kind of value-producing function that not only has no side effects but also
    doesn’t rely on side effects from other code.
    E.G console.log(Math.max(2, 4)); is not a pure function.

–Functions are created using an expression that starts with the keyword “function”. Functions can also be created using the “=>” character.
—const shaggy = function() {
document.write(“Zoinks!”);
}
–“let” is used to create a binding that is local to a function, expression, or statement. “var” is used to created a binding that used globally or locally (they end up in the nearest function scope or the global scope).
–A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code. A pure function always produces the same value and doesn’t do anything else.

How can you create a function in Javascript?
By declaring a function:

fuction (x,y) {return x y;}

What is the difference between var and let in regards to scope?
let focuses in local environments, while var can be called globally within the program.

What is a pure function?
A pure function has no side effects, however, it does not do anything else aside of having a constant value.

  1. How can you create a function in Javascript?
    var func1 = function(arg) { // body };
    function func1 (arg) { // body }
    var func1 = (arg) => { // body };
  2. What is the difference between var and let in regards to scope?
    let is for declaring local variable to the block, var has larger scope, can be used by function or global if it is not defined in any function.
  3. What is a pure function?
    A pure function does not produce any side effects and it does not rely on side effects either, it will always produce the same output when give the same input.

How can you create a function in Javascript?
keyword function then giving it a parameter ( ) and then a body { }
What is the difference between var and let in regards to scope?
var is accessible throughout the program while let is local to the code block
What is a pure function?
It is a function which does not do side-effects and always you call it with the same arguments, you receive the same result

  1. A function is created with an expression that starts with the keyword function.
    Functions have a set of parameters and a body, which
    contains the statements that are to be executed when the function is called.
    The function body of a function created this way must always be wrapped in
    braces, even when it consists of only a single statement.
  2. Var keyword, are visible throughout the whole function they appear in or throughout the global scope
    Let are local to the block that they are declared in, so if you create one of those inside of a loop, the code before and after cannot see it.
  3. A pure function is a specific kind of value-producing function that not only
    has no side effects but also doesn’t rely on side effects from other code—for
    example, it doesn’t read global bindings whose value might change. A pure
    function has the pleasant property that, when called with the same arguments,
    it always produces the same value (and doesn’t do anything else).

1/ How can you create a function in Javascript?

By declaring the function with a binding such as var or const. Const will keep the value of the binding locally within the block of text, while var will references the binding throughout the entire program (i think).

const blockchain = function() {
console.log(“Is cool!”);
};

What is the difference between var and let in regards to scope?

Var will reference the value of the function throughout the entire program. Let will only reference the value of a function within the loop that it is used in.

What is a pure function?

A function that does not produce a side effect!

1. How can you create a function in Javascript?
Starting with the keyword function and a possible (parameter), or several. Followed by a {body} that has the statements that need to be executed. i.e.

function square(x) {
return x * x;
}

2. What is the difference between var and let in regards to scope?
The var bind has a bigger scope than the let bind. i.e.

let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// → 60 Here the let bind is included because the console.log is inside the }
the var bind also is included.
}
console.log(x + z);
// → 40 Here the let bind is not included because the console.log is outside the }
the var bind however is included.

3. What is a pure function?
This is a function that not only produces a value and has no side effects. But that value is not affected by global bindings.

  1. Create a function
    function myfunction(parameters, para…){
    function to be carried out
    }

  2. a var can be used globally withing the script while let is for a certain function

  3. a pure function is one that has no side effects connected to it.

1. How can you create a Function in JavaScript?

A function is defined with a ‘function’ keyword, followed by a ‘name’, followed by a ‘parentheses’ ()

2. What is the difference between ‘var’ and ‘let’ in regards to scope?

'let’ allows you to declare 'variables’ that are limited in 'scope’ to the block, statement or expression it is used.

3. What is a Pure Function?

A ‘pure function’ does not depend on and does not modify the states of the variables out of its scope. Meaning a pure function always returns the same result given same parameters.

  1. A function may be created by using the function keyword. When used as an expression it can create a function value, e.g. function hello() {}. When used as a statement, it can be used to declare a binding and give it a function as it’s value.
  2. var is scoped to the nearest function block, whilst let is scoped to the nearest enclosing block (which may be smaller than a function block). Furthermore, variables declared with let are not accessible before they are declared in their enclosing block.
  3. A pure function is a value-producing function which does not produce a side effect and does not rely on side effects from other code. Thus, a pure function, when called with the same arguments, always produces the same value.
  1. In JS a function can be created by writing an expression which starts with a function key word. The function expression consists of a set of parameters and a body. The body is a statement executed when a function called. A function can be written in three different ways:
  • const name = function(parameters){
    body}
  • function name (parameters) {
    body}
  • const name = (parameters) => {
    body}
  1. The difference between a var and a let keywords is different binding visibility in program. A let is in fact a local binding visible only inside of a block the let is created. It is cannot be read before and after a particular statement. The var is global binding which can be seen from elsewhere in program.

  2. A pure function is a function produces a value without any side-effect and doesn’t rely on any side-effects produced by other code.

How can you create a function in Javascript? → A function is created with an expression that starts with the keyword “function”. Functions have a set of parameters, and a body which contains the statements that are to be executed when the function is called. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement. A function can have multiple parameters or no parameters at all. A return statement determines the value the function returns.
What is the difference between var and let in regards to scope? → In pre-2015 JavaScript, only functions created new scopes, so old-style bindings, created with the var keyword, are visible throughout the whole function that they appear in - or throughout the global scope, if they are not in a function. A let binding is local to the block that it is declared in, si if you create one inside of a loop, the code before and after the loop cannot see it.
What is a pure function? → A specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code - for example, it doesn’t read global bindings whose value might change. A pure function has the property that, when called with the same arguments, it always produces the same value (and doesn’t do anything else).