Home > Blockchain >  How can I use variable in method function? (JavaScript)
How can I use variable in method function? (JavaScript)

Time:12-27

let newArr = 0;

function solution(arr) {
  for (let i = 0; i < arr.length; i  ) {
    newArr  = arr[i];
  }
}

console.log(solution([1,2,3,4])) //undefined ;

I want to add all of arrays. But error says, 'newArr' is assigned a value but never used.

How can I use variable in method function?

I tried to declare variable in method.

function solution(arr) {
  for (let i = 0; i < arr.length; i  ) {
    let newArr = 0;

    newArr  = arr[i];

  }
}

Of course it didn't work :(

CodePudding user response:

Minimal changes, return newArr, though this isn't the cleanest solution

let newArr = 0;

function solution(arr) {
  for (let i = 0; i < arr.length; i  ) {
    newArr  = arr[i];
  }
  return newArr // add this line
}

console.log(solution([1,2,3,4]))

Alternatively, call the function (without a return) then output newArr because the function is changing its value. Global variables like this are generally not good practice, but it does depend on the situation

let newArr = 0;

function solution(arr) {
  for (let i = 0; i < arr.length; i  ) {
    newArr  = arr[i];
  }
}
solution([1,2,3,4]) // call function
console.log(newArr) // output the value

Define newArr in the function and return it

function solution(arr) {
  let newArr = 0; // define local variable

  for (let i = 0; i < arr.length; i  ) {
    newArr  = arr[i];
  }
  return newArr // return value
}

console.log(solution([1, 2, 3, 4]))
// console.log(newArr) // would be undefined

CodePudding user response:

Hi @Josh Yeom,

The newArr variable is declared inside the solution function. So, guess what, it is only accessible within that function. The Value of newArr outside the function you can't use, because it's not in the same scope.

It's a concept of lexical scoping.

The scope of a variable is determined by its position in the code. Variables that are declared inside a block of code, such as a function or loop, are only accessible within that block. This is known as lexical scoping.

Or in other words.

Lexical scoping is a way of determining the accessibility of variables in a program based on their position in the code. It is a fundamental concept in many programming languages, including JavaScript.

In your case, need to do your code like this.

function solution(arr) {
  let newArr = 0;
  for (let i = 0; i < arr.length; i  ) {
    newArr  = arr[i];
  }
  return newArr;
}

console.log(solution([1,2,3,4])) // 10

The newArr variable is declared outside the for loop, so it is accessible throughout the function. The value of newArr is then returned from the function using the return statement, and you can access this value by calling the solution function.

Here is a simple example of how lexical scoping works in JavaScript.

let globalVariable = "I am a global variable";

function outerFunction() {
  let outerVariable = "I am an outer variable";

  function innerFunction() {
    let innerVariable = "I am an inner variable";

    console.log(innerVariable);  // "I am an inner variable"
    console.log(outerVariable);  // "I am an outer variable"
    console.log(globalVariable); // "I am a global variable"
  }

  console.log(innerVariable);  // ReferenceError:innerVariable is not defined
  console.log(outerVariable);  // "I am an outer variable"
  console.log(globalVariable); // "I am a global variable"
}

console.log(innerVariable);  // ReferenceError: innerVariable is not defined
console.log(outerVariable);  // ReferenceError: outerVariable is not defined
console.log(globalVariable); // "I am a global variable"

CodePudding user response:

That is because your function does not return the calculated value after completion.

let newArr = 0;
function solution(arr) {
for (let i = 0; i < arr.length; i  ) {
     newArr  = arr[i];
  }
  return newArr;
}

console.log(solution([1,2,3,4]));

CodePudding user response:

function doesn't have return value like you have to check then put console inside that function

For Exa

let newArr = 0;
function solution(arr) {
for (let i = 0; i < arr.length; i  ) {
  newArr  = arr[i];
}
return newArr;
}

console.log(solution([1,2,3,4]))
  • Related