简体   繁体   中英

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.

您应该使用正则表达式搜索Billy:

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

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

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.

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,

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

You can also use includes like:

if( sentence.includes('Billy') )

Check Browser support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM