0

The function called HW1q5 takes a string as an argument called message and returns true if the argument contains the word “hello” OR the word “Hello”.

public boolean HW1q5( String  message  )
{
  if (message == "hello" || message == "Hello"){
    return true;
  } else
    return false; 
}

What am I doing wrong?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • That says *equals*, not *contains*. – jonrsharpe Oct 27 '19 at 19:26
  • JavaScript doesn't have strong typing where you declare your return type or argument types in a function. Nor does it have `public` or `private` declarations. – Scott Marcus Oct 27 '19 at 19:27
  • 1
    Sorry, but your question is pretty hard to understand. Your title mentions JavaScript, but your code is Processing (which is built on Java). Can you explain what you expected this code to do, and what it's doing instead? – Kevin Workman Oct 28 '19 at 21:18
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kevin Workman Oct 28 '19 at 21:18

1 Answers1

0

Your code is not JavaScript as JavaScript:

  • Doesn't have public or private scoping.
  • Doesn't have data types declared for arguments or return types.
  • Has the function keyword, which declares the code as a callable function.

If you correct these things, you'll have valid JavaScript which will work, but be warned that your logic so far doesn't check if the argument contains the test strings, it checks to see if the input exactly matches the test strings. Additionally, it is a case-sensitive match.

function HW1q5(message)
{
  if (message == "hello" || message == "Hello"){
    return true;
  } else {
    return false; 
  }
}

console.log(HW1q5("hello"));
console.log(HW1q5("Hello"));
console.log(HW1q5("HELLO"));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71