0

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",
}
1
  • I would suggest that you check out includes() method. If structure of programmingLanguage string is always comma separated values, I would suggest that you split it by comma and then check that array contains Java. Right now, you can change condition like this: if (books.programmingLanguage.includes("Java")), but then it will be problem if, for example, word JavaScript appears because function will return true. Commented Mar 11 at 17:50

2 Answers 2

0
function hasExamplesInJava(books) { 
 if (books.programmingLanguage.includes("Java")) {
      return true;
 } else {
      return "No data available";
 }
}
1
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community? Commented Mar 12 at 18:18
0

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)}`)
}

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.