pops
pops

Reputation: 19

Simple Javascript If Statement Issue

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.

Upvotes: 1

Views: 100

Answers (7)

Naruto
Naruto

Reputation: 708

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,

Upvotes: 1

pid
pid

Reputation: 11607

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!");
}

Upvotes: 3

Firemen26
Firemen26

Reputation: 1045

You can also use includes like:

if( sentence.includes('Billy') )

Check Browser support

Upvotes: 0

mhk
mhk

Reputation: 11

<script>
if(prompt('Enter Sentence Here: ').toLowerCase().indexOf("billy") >= 0)
{
    console.log('Great!');
}
</script>

Upvotes: 1

Bamieh
Bamieh

Reputation: 10906

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;

Upvotes: 1

AHB
AHB

Reputation: 558

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.

Upvotes: 2

Mayday
Mayday

Reputation: 5136

You should use regular expressions, searching for Billy:

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

Upvotes: 4

Related Questions