Preview:
const facebookProfiles = [
  {
    firstName: "Akash",
    lastName: "Agarwal",
    number: "111111111",
    likes: ["music", "movies"],
    hasDrivingLicense: false,
    address: {
      location: "rampur",
      state: "up",
    },
    emails: ["abc@outlook.com", "efg@gamil.com", "ghj@gmail.com"],
  },
  {
    firstName: "Pritesh",
    lastName: "Kumar",
    number: "222222222",
    likes: ["code", "driving"],
    hasDrivingLicense: false,
    address: {
      location: "gurgaon",
      state: "haryana",
    },
    emails: ["fgdfg@gmail.com"],
  },
  {
    firstName: "Sabiha",
    lastName: "Khan",
    number: "333333333",
    hasDrivingLicense: false,
    address: {
      location: "lucknow",
      state: "up",
    },
  },
  {
    firstName: "Suyash",
    lastName: "Kashyap",
    number: "444444444",
    likes: ["travel", "driving"],
    hasDrivingLicense: true,
    address: {
      location: "alwar",
      state: "rajasthan",
    },
    emails: ["abc@yahoo.com"],
  },
  {
    firstName: "Jay",
    likes: ["food", "mobile"],
    hasDrivingLicense: true,
    address: {
      location: "gurgaon",
      state: "haryana",
    },
    emails: ["abc@gmail.com", "efg@yahoo.com", "ghj@outlook.com"],
  },
];

// IMPORTANT: SOLVE the following questions using two methods
// 1. use for loop
// 2. USE HIGHER ORDER FUNCTIONS TO SOLVE THE ABOVE QUESTIONS(map, filter, find, forEach etc. )

// ==================================== 0 ==================================== //

function profileLookup(name, property) {
  //write your code here
  for (const profile of facebookProfiles) {
    if (profile.firstName === name) {
      return profile[property] ? profile[property] : "no such property";
    }
  }
  return "person does not exist";
}
console.log(profileLookup("Pritesh", "likes"));
// complete the above function such that when it is called with name and property, then it should return its value
// ex
// profileLookup("Pritesh", "number"), then it should return his number

// handle edge cases like:
//      if name is not in the list, return "person does not exist"
//      if property is not present then return "no such property"
//
// Hint: dynamically access properties using square bracket

// ================================== 1 ====================================== //

function getNamesFromGurgaon(facebookProfiles) {
  const newArray = [];
  facebookProfiles.forEach((profile) => {
    // return profile.address.location === "gurgaon" ?? profile.firstName;
    if (profile.address.location === "gurgaon") {
      newArray.push(
        `${profile.firstName}${profile.lastName ? ` ${profile.lastName}` : ``}`
      );
    }
  });
  return newArray;
}
//complete the above functin such that it returns the list of full names of all people of gurgaon.
// ex = ['Jay ', 'Pritesh Kumar']
console.log(getNamesFromGurgaon(facebookProfiles));
// ==================================2 ====================================== //

function findFullName(stateName) {
  //write your code here
  for (let i = 0; i < facebookProfiles.length; ++i) {
    if (facebookProfiles[i].address.state === stateName) {
      return `${facebookProfiles[i].firstName}${
        facebookProfiles[i].lastName ? ` ${facebookProfiles[i].lastName}` : ``
      }`;
    }
  }
}
console.log(findFullName("up"));
// 2. complete this function, which takes state name as argument and return the name
// of one of its residents

// ================================== 3 ====================================== //

function getDLStatus(facebookProfiles) {
  //write your code here
  const dlStatus = [];
  for (let i = 0; i < facebookProfiles.length; ++i) {
    if (facebookProfiles[i].address.location === "gurgaon") {
      if (facebookProfiles[i].hasDrivingLicense) {
        dlStatus.push(`${facebookProfiles[i].firstName} - D/L`);
      } else {
        dlStatus.push(`${facebookProfiles[i].firstName} - N D/L`);
      }
    }
  }
  return dlStatus;
}
getDLStatus(facebookProfiles);
//3. write a function which returns full names of all people of gurgaon with their driving license status, in an array.
// like shown in the example below
// ex = ['Jay - D/L', 'Pritesh Kumar - N D/L']

// =================================== 4 ===================================== //

function getFullName(facebookProfiles) {
  return facebookProfiles.map((profile) => {
    return `${profile.firstName}${
      profile.lastName ? " " + profile.lastName : ``
    }`;
  });
}

getFullName(facebookProfiles);
// 4. write a function which returns full names in an array
//ans = ['Akash Agarwal', 'Pritesh Kumar', 'Sabiha Khan', 'Suyash Kashyap', 'Jay' ]

// ===================================== 5 =================================== //

function getLikes(facebookProfiles) {
  //write your code here
  let likes = [];
  facebookProfiles.forEach((profile) => {
    if (profile.likes) {
      likes = [...likes, ...profile.likes];
    }
  });
  return likes;
}
getLikes(facebookProfiles);
// 5. write a function which returns all likes of all the people in an array
//hint: use spread syntax
//ans = ['music', 'movies', 'code', 'podcasts', 'travel', 'driving', 'food', 'mobile']

// ====================================== 6 ================================== //

function getNameFromDelhiWithDL(facebookProfiles) {
  //write your code here
  for (let i = 0; i < facebookProfiles.length; ++i) {
    if (
      facebookProfiles[i].address.location === "delhi" &&
      facebookProfiles[i].hasDrivingLicense
    ) {
      return facebookProfiles[i].firstName;
    }
  }
  return "no such person";
}
getNameFromDelhiWithDL(facebookProfiles);
//6. write a function which return  the name of the any one person who live in delhi and has driving license
//ans => "no such person"

// ======================================= 7 ================================= //

function getNameOfDriverWithoutDL(facebookProfiles) {
  // write your code here
  for (let i = 0; i < facebookProfiles.length; ++i) {
    if (!facebookProfiles[i].hasDrivingLicense) {
      if (facebookProfiles[i].likes.indexOf("driving") > 0) {
        return facebookProfiles[i].firstName;
      }
    }
  }
}

getNameOfDriverWithoutDL(facebookProfiles);
//7. write a function which return the name of the any one person who like driving but doesnt have driving license
//asn => pritesh
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter