Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to log function(cities) return value to console in JavaScript for testing whether the output is correct?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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' ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading