Functions - Reading Assignment

How can you create a function in Javascript?

There are three different ways. One way is to define a const like so…

const cube = function(x) {
return x * x * x;
};

Then use the function keyword after = with any number of parameters. Then use return to get the result.

Alternatively you can use the declarative form (which is closer to what I’m used to)

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

Alternatively one can also write the same thing like this…

const cube = x => x * x * x;

Then you can call like this

cube(4);
// 64

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

Let is only visible from within the block it’s declared in or in nested blocks below it. Var is like declaring a global variable which has scope throughout the unit it is written in.

What is a pure function?

A function that has no side effects and returns a value.

  1. a function is created with the keyword function. It must have a body containing the set of statements, which is wrapped in braces, even if there is only one statement.

  2. Variables created with the keyword let are local to the block they are declared in. So if they are created inside a loop the code before or after the loop can’t see them. Where as variables created with the keyword var are visible throughout the whole function they are appear in, or throughout the global scope if they are not declared in a function.

  3. A pure function is a specific type of function. It takes input, executes statements and returns an output without modifying any variables outside the scope of the function. It has no side effects, does not change the state of the programme and does not rely on side effects from other code. It always produces the same value and does not do anything else.

Help ! Problem with the nested scope script in the book page 42
I have a problem the nested scope script ! I cant seem to get this to work !!!
Has anyone had the same issue and managed to fix ?

const hummus = function(factor) {
const ingredient = function(amount, unit, name) {
let ingredientAmount = amount * factor;
if (ingredientAmount > 1) {
unit += “s”;
}
console.log$(’${ingredientAmount} ${unit} ${name}’);
};
ingredient(1, “can”, “chickpeas”);
ingredient(0.25, “cup”, “tahini”);
ingredient(0.25, “cup”, “lemon juice”);
ingredient(1, “clove”, “garlic”);
ingredient(2, “tablespoon”, “olive oil”);
ingredient(0.5, “teaspoon”, “cumin”);
};

1 .To creat a function
const FunctionName = function(input values) {
Code body
return return values
};

  1. let has a local scope to the function and var has a global scope and is visable to the rest of the program.
  2. A pure function is one that returns a value and is does not rely on side effects or global bindings. Also it returns the same value when it is called.
  1. You can create a function in two ways, example:
    function functionname(arg1, arg2) {
    CODE INSIDE TO RUN
    }

    OR
    const constname = function(arg1, arg2) {
    CODE INSIDE TO RUN
    };

  2. The difference is that var is scoped to the nearest closing function and let is scoped to the nearest closing block.

  3. A pure function always produces the same value and doesn’t rely on global bindings that can change so that the function can produce a different value. It doesn’t rely on side effects from other code aswell.

With the keyword “function”, a set of parameters and a body.

Let and const are local bindings and when created in a functions they are not visibible for the code outside the function.

When a function causes no side affects and always produces the same output given the same parameters.

  1. We create functions by first naming the function with a keyword, function. Then we can decide whether to set a parameter for it. Add the body within a block, and lastly we call the function outside of the block.

  2. Setting a binding or variable using VAR is based on global scope so it can be referred to outside of a block of a function. The LET/CONST is limited locally to within that functions block and it cannot be accessed from outside of that block.

  3. A pure function is a function that produces a value, no side effects, and is not tied to side effects from other code (such as a global binding which has the potential to change values)

How can you create a function in Javascript?

different ways but this will do the trick:

scopeDeclaration(const let or var) varName= funcName(arg1;arg2;argx…) = { //Code }

What is the difference between var and let in regards to scope?
Let has a local scope to the block or functionas Var has a global scope and can be called and/or modified

What is a pure function?
One that has no side effects and uses only local bindings thus rely on no other external value resulting on the same return anytime the function is used.

 function myFunction(param1, param2 /*, ..., paramN */){
        //code here
      }

var defines variables that are visible everywhere (if not defined in a function), or visible everywhere within the function (if defined inside a function) let defines variables that are local to the block they are defined in.

A pure function is a function whose output depends only on the values of the parameters passed to the function.

1- There are three ways to create functions in Javascript:

  • const a = function(x) to create a binding and give a function as its value
  • function a(x) declares a function called “a” with x as its variable
  • a = x => is a simpler way to define a function

2- let and const are bindings that are only used in the block they are declared in, whilst var bindings are visible throughout the whole function they are declared in, or the whole program if not inside a function

3- A pure function is one that produces no side effects nor does it depend from side effects from other functions. It works as a standalone and will always produce the same results if called with the same arguments.

How can you create a function in Javascript?

function g (a, b) {return a * b*4;}

or

let h = a => a%3;

What is the difference between var and let in regards to scope?
let and const are local to the block that they are declared in, so if you create one of those inside of the loop, the code before and after cannot see it; binding created with var keyword are visible throughout the whole function that they appear in or - throughout the global scope, if they are not in a function

What is a pure function? a pure 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 doesn’t read global binding whose value might change

  1. How can you create a function in Javascript?
    Use keyword function to define a function. A function has the following syntax: const func = function (x){
    Return x +1;
    }

  2. What is the difference between var and let in regards to scope?
    Let declares a local binding within a block {}, whereas var declares a binding that is local within an entire function or global if outside of any 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.

  1. by using the function keyword specifying the function parameters , if any, and the code that the function defines and the return value (if any). Alternatively you can use some shorter forms such as the arrow notation form. A more generic way is that you define a variable that points to a function (it holds a function value)

  2. ‘let’ will give pure local scope to the block in which the ‘let’ is used. Using ‘var’ will give the variable a scope that is visible within the overall function.

  3. A pure function has no side affects, needs no side affects from other sources and just simply produces a value.

  1. const name = function(x) {
    return /* insert function here /*;
    }

or function(x) {
return /* insert function here /*;
}

  1. var has a global scope, let has a local scope.

  2. 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.

How can you create a function in Javascript?
a function is a block of code that has a name and can be started by calling the name.
The handy thing is that it allows multiple use of the function without being rewritten.
In addition, the function may have an argument, or it can return a result .
function “name of function”(argument){
code
}

What is the difference between var and let in regards to scope?
“let” is local binding,visible only in the current block .
“var” can also be used locally, with preference value read locally.
The value set outside the block will not be read in the block.

What is a pure function?
“Pure” always gives the same result if is call with same argument.
It does not read the global binding.Easy for test working or not.

Greetings Everyone I Have Really been Struggling lately with this course, and I realize I need to do much More independent study. Please Know that I am working every other day to see this project through. I have learned so much than I already knew before…Always Forward.

1 Like
  1. How can you create a function in Javascript?
    You could create a function in three different ways in JavaScript with small or none different features.
    let foo=function(number1, number2){body;};
    function(number1,number2){body}
    const foo= (number1, number2) =>{body};

  2. What is the difference between var and let in regards to scope?
    let would just be possible to use in its own block, its a local variable, as for var is a global variable which could be called upon out of the local scope its created in.

    Let would then be created in its block, but when the block is done it would delete the binding(delete the element) thereby not possible for blocks higher to call it.

  3. What is a pure function?
    A pure function is a function that does not give any side effects at all and if out of the block values as global variables are used they could not either change or provide side effects, thereby a pure function would always provide the same return given the same arguments.

  • How can you create a function in Javascript?
    By binding and expression or value to preset function.
  • What is the difference between var and let in regards to scope?
    if var is binded outside of the function than it is global and can be seen throughout the entire program wile the let local to the block its defined in.
  • What is a pure function?
    a function that doesnt rely on any previous side effects produces same value
  1. A function is created with an expression that starts with the keyword function and has a set of parameters and a body which contains the statements that are to be executed.
  2. Var are visible throughout the global space. Let are local to the block declared in.
  3. Is a specific kind of value producing function that not only has no side effects but also does not rely on side effects from other code.

How can you create a function in Javascript?

  • function a(x){}
    What is the difference between var and let in regards to scope?
  • let smaller size than var
  • let not accsessible before decalartion
  • let only visible in for() but var visible to whole function
  • var ca redeclare
    What is a pure function?
  • it does no side effect