dimanche 4 octobre 2020

How do I add to a function in Javascript?

I've been going through a bootcamp, and I can't seem to figure out how to add to a function in Javascript, specifically when I've already placed my return value. For example, this function runs, but if someone said "okay, now if there is no one that matches the studying string, return a message that reads "No matches." Where do I add a new if statement saying "if (newArr.length = 0) {return "No matches.} Desperately needing help on this, and I've got an exam on JS functions tomorrow. Thought I had everything down, until this big ole curveball. Any and all help is greatly appreciated, thank you!

let people = [{
    name: "Nick",
    studying: "JS",
    age: "33"
  },

  {
    name: "Joseph",
    studying: "JS",
    age: "137"
  },

  {
    name: "Jeff",
    studying: "education",
    age: "1897374"
  }
];

function returnArrOfNames(people, string) {
  let newArr = []
  for (let i = 0; i < people.length; i++) {
    if (people[i].studying === string) {
      newArr.push(people[i].name)
    }
  }
  return newArr
}

console.log(returnArrOfNames(people, "JS"));

Aucun commentaire:

Enregistrer un commentaire