You are viewing our Forum Archives. To view or take place in current topics click here.
Javascript resource code please
Posted:

Javascript resource code please Posted:

Pou
  • Ladder Climber
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 323
Reputation Power: 12
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 323
Reputation Power: 12
I'm a pretty big noob in javascript and need help in a script for a chat room.
The script that I'm trying to make is a) Finds strings of text for e.g "Apple juice" if the text doesn't contain apple the script will activate.
Another example "Orange" The string did not contain the word apple the script now will execute.

I now this might be on the incorrect forum and I'm a noob in programming but I really need help in this.
Thanks for even reading, I would appreciate if you would respond if you knew even something about this subject !
~Pou
#2. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
You can use the .includes() method which will return true or false depending on whether the original string contains the substring.

Example:

var myString = Welcome to The Tech Game;
var a = myString.includes("The"); // a will be true
var b = myString.includes("Hello"); // b will be false



You can use the return value from the .includes() method to act upon the original string.

var myString = "OrangesApplesBananas";
if (myString.includes("apple")) {
    // The string contains "apple".
    // Do stuff here
} else {
    // The string does not contain "apple".
    // Do other stuff here
}


One thing to note is that I'm fairly certain that the .includes() method is NOT case-sensitive.
#3. Posted:
Pou
  • Ladder Climber
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 323
Reputation Power: 12
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 323
Reputation Power: 12
-Deano wrote You can use the .includes() method which will return true or false depending on whether the original string contains the substring.

Example:

var myString = Welcome to The Tech Game;
var a = myString.includes("The"); // a will be true
var b = myString.includes("Hello"); // b will be false



You can use the return value from the .includes() method to act upon the original string.

var myString = "OrangesApplesBananas";
if (myString.includes("apple")) {
    // The string contains "apple".
    // Do stuff here
} else {
    // The string does not contain "apple".
    // Do other stuff here
}


One thing to note is that I'm fairly certain that the .includes() method is NOT case-sensitive.

Ah thanks for replying! I appreciate your help a lot !! <3
#4. Posted:
CriticaI
  • Christmas!
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
-Deano wrote You can use the .includes() method which will return true or false depending on whether the original string contains the substring.

Example:

var myString = Welcome to The Tech Game;
var a = myString.includes("The"); // a will be true
var b = myString.includes("Hello"); // b will be false



You can use the return value from the .includes() method to act upon the original string.

var myString = "OrangesApplesBananas";
if (myString.includes("apple")) {
    // The string contains "apple".
    // Do stuff here
} else {
    // The string does not contain "apple".
    // Do other stuff here
}


One thing to note is that I'm fairly certain that the .includes() method is NOT case-sensitive.


Since OP said they were new to javascript. I will add to this.
If you want to check if it doesn't include something, most beginners would set their code up like this.

if (myString.includes('apple') === false) {
  alert("foo");
}


Which is fine, but it isn't the simplified version.

So here is a simpler way of writing it. (not the most simple though)

if(!myString.includes('apple'){
  alert("foo");
}
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.