1
var sentence = prompt('Enter Sentence Here: ')
if(sentence === 'Billy'){
console.log('Great!');
}

I was wondering if there is a way to return "Great!' if the sentence isn't just "Billy" For Example how could you return "Great!" if the sentence was "My name is Billy" so what what I'm asking for is how to I get the if statement to scan the sentence and determine if that word is present then return what I wish.

My apologies for the simplicity and stupidity, this is my first day learning JS.

3
  • if( sentence.indexOf("Billy") > -1) would be one way. Commented Jun 11, 2017 at 8:40
  • Naive approach: array = string.split(); for(i of array) if(i=='Billy') console.log('Great'); Commented Jun 11, 2017 at 8:40
  • i mean console.log "Great!" Commented Jun 11, 2017 at 8:41

7 Answers 7

4

You should use regular expressions, searching for Billy:

/billy/i.test("I am Billy")
Sign up to request clarification or add additional context in comments.

7 Comments

Using regular expressions to search for a fixed string? Really?
@NiettheDarkAbsol i dont see why not? using test looks much neater than some other solution, plus its one of the fastest solutions.
This is the fastest solution because of how optimised regex matching is.
@AhmadBamieh would you mind to give me a little explanation why is using regexp test method better than the match I did use? Always had that curiosity ^^
Just benchmarked it, and as expected a literal string lookup is about 20% faster using indexOf than a static regex. Throw case-insensitivity into the mix, however, and regex barely breaks a sweat while toLowerCase().indexOf() suffers halved performance.
|
3

Use indexOf as follows:

if (sentence.toLowerCase().indexOf("billy") !== -1) {

All lower case is an additional laxing of the condition.

This is your full code:

var sentence;

sentence = prompt("Enter Sentence Here: ");

if (sentence.toLowerCase().indexOf("billy") !== -1)
{
  console.log("Great!");
}

Comments

2

You can use .includes or .indexOf which scan a string for a substring.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes

includes takes a string to search for and returns true if it finds it, otherwise false.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

indexOf takes a string to search for and if it finds it, will return the starting index of that string, so 'hello'.indexOf('ello') => 1 If it doesn't find it, it returns -1;

.includes

var sentence = prompt('Enter Sentence Here: ');

if (sentence.includes('Billy')) {
  console.log('Great!');
}

.indexOf

var sentence = prompt('Enter Sentence Here: ');

if (sentence.indexOf('Billy') > -1) {   
  console.log('Great!'); 
}

Something to note is that it is case sensitive, so make sure you are typing 'Billy'. You can make use of toLowerCase and search for billy as well, and that way it would be case insensitive.

Comments

1

Regular expression

/billy/i.test("I am Billy");

es6 includes

"I am Billy".toLowerCase().includes("billy");

es6 contains

"I am Billy".toLowerCase().contains("billy");

old indexOf

"I am Billy".toLowerCase().indexOf("billy") !== -1;

1 Comment

@Coldspeed thanks, you should mention this in the edit comment, not here
1
<script>
if(prompt('Enter Sentence Here: ').toLowerCase().indexOf("billy") >= 0)
{
    console.log('Great!');
}
</script>

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
1

you can use indexOf

var sentence = prompt('Enter Sentence Here: ');//"My name is Billy"

if(sentence.indexOf("Billy") !== -1){
     console.log('Great!');
}

Incase you want case insensitive , you can use toLower() on string and then search for "billy"

  if(sentence.toLowerCase().indexOf("billy") !== -1){
       console.log('Great!');
  }

Using regex is also one of the good options.

if(/billy/i.test(sentence)){ 
     console.log('Great!');
 }

Thanks for reading,

Comments

0

You can also use includes like:

if( sentence.includes('Billy') )

Check Browser support

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.