Object property has different many string values

29 views Asked by At

I have objects, that has different properties, one of them is programingLanguage. So, with the function, I want to check if the object property - programmingLanguage has a value of "Java". if it has it returns true, if it doesn't it returns no data available. Here is the code below of my function.

I am stuck in the case when in object programmingLanguage property has a string value like this "C, Java", in this case, I also want to return true, when one of them is Java, but with my function, it returns no data available.

The problem is, I do not know how to check when there is 2 string values for the property if one of them matches to "Java"

function hasExamplesInJava(books) {
  if (books.programmingLanguage === "Java") {
    return true;
  } else if (books.programmingLanguage !== "Java") {
    return "No data available";
  }
}



{
  title: "Algorithms",
  author: ["Robert Sedgewick", "Kevin Wayne"],
  programmingLanguage: "Java",
}

{
  title: "Structure and Interpretation of Computer Programs",
  author: ["Harold Abelson", "Gerald Jay Sussman", "Julie Sussman (Contributor)"],
  programmingLanguage: "JavaScript",
}

{    
  title: "Operating System Concepts",
  author: ["Abraham Silberschatz", "Peter B. Galvin", "Greg Gagne"],
  programmingLanguage: "C, Java",
}
2

There are 2 answers

1
Vinay Danidhariya On
function hasExamplesInJava(books) { 
 if (books.programmingLanguage.includes("Java")) {
      return true;
 } else {
      return "No data available";
 }
}
0
Milos Stojanovic On

What you need to do is not to check if entire string is equal to "Java" but to check if string contains (or maybe better to say, includes) substring "Java". You can do it in a few different ways, so let's play a little.

  1. First, you can check if entire string includes word "Java". Problem with that approach is that if string includes words that have "Java" in them, like "Javascript" for example, your function will return true. That's probably not what you want. That's shown with function stringIncludesJava in snippet.

  2. Second, if languages in value of property programmingLanguage are always separated by comma, you can make array from them by splitting value with comma and getting array of languages. After that, check if array includes word "Java". That's shown with function stringIncludesJavaWithSplit in snippet.

  3. Third, you can check if property includes "Java" using regex. If you are not familiar with regex, check it out, it can be really helpful when searching for words or phrases in text. That's shown with function stringIncludesJavaWithRegex in snippet.

By the way, it's not really good for functions to return different types of values as you did by returning boolean or string. It's better to return one type (in your example, return boolean and after function call check if boolean is true or false and go on from there). Also, it's often good to perform case-insensitive check unless you have need not to.

let books = [
  {
    title: "Algorithms",
    author: ["Robert Sedgewick", "Kevin Wayne"],
    programmingLanguage: "Java",
  },
  {
    title: "Structure and Interpretation of Computer Programs",
    author: ["Harold Abelson", "Gerald Jay Sussman", "Julie Sussman (Contributor)"],
    programmingLanguage: "JavaScript",
  },
  {    
    title: "Operating System Concepts",
    author: ["Abraham Silberschatz", "Peter B. Galvin", "Greg Gagne"],
    programmingLanguage: "C, Java",
  }
]

function stringIncludesJava(book) {
  if (book.programmingLanguage.toLowerCase().includes("java"))
    return true;
  else
    return false;
}

function stringIncludesJavaWithSplit(book) {
  let languages = book.programmingLanguage.split(',').map(lang => lang.trim().toLowerCase());
  if (languages.includes("java"))
    return true;
  else
    return false;
}

function stringIncludesJavaWithRegex(book) {
  const regex = /\bjava\b/i;
  
  if (regex.test(book.programmingLanguage.toLowerCase()))
    return true;
  else
    return false;
}

for (let book of books) {
  console.log(`${book.programmingLanguage} -> ${stringIncludesJava(book)}`)
}

console.log("----------------")

for (let book of books) {
  console.log(`${book.programmingLanguage} -> ${stringIncludesJavaWithSplit(book)}`)
}

console.log("----------------")

for (let book of books) {
  console.log(`${book.programmingLanguage} -> ${stringIncludesJavaWithRegex(book)}`)
}