Challenge: Return the Cat
This is an advanced level challenge to test your skills in implementing array and object destructuring in JavaScript.
We'll cover the following
Problem statement #
You’re given a function returnNthCat
, which takes a variable n
and has an object state
defined in it.
Carefully study the structuring of this object.
function returnNthCat(n){
const state = {
cats : [
{catId : 1 , name : "tom"},
{catId : 2 , name : "tiggy"},
{catId : 3 , name : "leo"},
{catId : 4 , name : "nixie"}
],
curpage : 3
}
}
Your task is to return the name
value of the n
th cat object.
This question tests your understanding of destructuring arrays/objects, so use this technique to solve the problem.
Input #
A value n
Output #
name
value of the n
th cat object
A sample input/output for the
state
object is given below.
Sample input #
n = 0
Sample output #
tom
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.