Comparisons with the Logical And Operator question

Comparisons with the Logical And Operator question
0

#1

Tell us what’s happening:
General question. Why do they put a blank variable at the beginning of the function?. And than return the result?.

Your code so far

// Setup
function phoneticLookup(val) {
var result = “”;

// Only change code below this line
switch(val) {
case “alpha”:
result = “Adams”;
break;
case “bravo”:
result = “Boston”;
break;
case “charlie”:
result = “Chicago”;
break;
case “delta”:
result = “Denver”;
break;
case “echo”:
result = “Easy”;
break;
case “foxtrot”:
result = “Frank”;
}

// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);


    function testLogicalAnd(val) {
  // Only change code below this line

  if (val < 50  && val >= 25) {
      return "Yes";
  }
  // Only change code above this line
  return "No";
}

// Change this value to test
testLogicalAnd(25);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-and-operator


#2

That will be easier to write, otherwise you will need to type “var” in every expression like var result = "Adams"; and var result = "Boston"; and so on. Or you can do just return "Adams"; return "Boston"; which will also works too but it will be inconvenient if you want to do something with the result from the switch statement.


#3
Why do they put a blank variable at the beginning of the function?

Because, if we don’t define blank variable then result will be wrong. So they define as blank variable.

Case 1. With Blank variable gets

blank value e.g. " "

Case 2. Without blank variable you will get result like this :

undefined

hope this helpful to understand the concept. Any Query reply on this thread.


#4

Javascript function has own scope. ‘var’, ‘let’, ‘const’ declared in the function body it is declared in that scope and can not be accessed from the global scope.

Scope determines the accessibility (visibility) of these variables.

Variables defined inside a function are not accessible (visible) from outside the function.

Whenever you see a function within another function, the inner function has access to the scope in the outer function, this is called Lexical Scope or Closure - also referred to as Static Scope.

Lexical scope is easy to work with, any variables/objects/functions defined in its parent scope, are available in the scope chain