Home > Enterprise >  Is it possible to log function(cities) return value to console in JavaScript for testing whether the
Is it possible to log function(cities) return value to console in JavaScript for testing whether the

Time:10-16

I am busy with a challenge and I know the code is correct as it passes the assignment but I'm having a tough time testing the out put.

<code>
function nonMutatingSplice(cities) {
  // Only change code below this line
  return cities.slice(0, 3);

  

  // Only change code above this line
}

console.log(cities)

const inputCities = ["Chicago", "Delhi", "Islamabad"]
nonMutatingSplice(inputCities); 

</code>

When I initiate the console log() call, the output to the console returns "ReferenceError: cities is not defined"

How can I console log the return value of the function to return the correct mutated array's values?

I am expecting to see the following in the console to validate that the code is returning the correct output:

["Chicago", "Delhi", "Islamabad"]

CodePudding user response:

You need to log the output from nonMutatingSplice

cities is never defined, because you don't allocate a value to the variable.

You can do it by using the following code.

const cities = nonMutatingSplice(inputCities);

Here's the code with a working output.

function nonMutatingSplice(cities) {
  return cities.slice(0, 3);
}

const inputCities = ["Chicago", "Delhi", "Islamabad"]
console.log(nonMutatingSplice(inputCities))

non-mutable example:

function nonMutatingSplice(cities) {
  return cities.slice(0, 2);
}

const inputCities = ["Chicago", "Delhi", "Islamabad"]
const cities = nonMutatingSplice(inputCities);
console.log(cities) //[ 'Chicago', 'Delhi' ]
console.log(inputCities) //[ 'Chicago', 'Delhi', 'Islamabad' ]

  • Related