Java script "Includes" Watch

SamRob85
Badges: 0
Rep:
?
#1
Report Thread starter 5 years ago
#1
I was wondering whether there is a way to check if a variable includes something e.g.

if (varX includes "Hello){
console.log "hello";
0
reply
LukeyJB
Badges: 16
Rep:
?
#2
Report 5 years ago
#2
You can check if a variable is set to null, so

If (varX != null) {
console.log "x"();
}
0
reply
Dez
Badges: 20
Rep:
?
#3
Report 5 years ago
#3
Code:
if (varX.indexOf('Hello') != -1) {
    // varX contains Hello
}
0
reply
SamRob85
Badges: 0
Rep:
?
#4
Report Thread starter 5 years ago
#4
(Original post by Dez)
Code:
if (varX.indexOf('Hello') != -1) {
    // varX contains Hello
}
I tried this but it did not work
Code:
<script>
confirm('Hello!!');
var feeling = prompt('Feel free to talk to me');
if (varfeeling.indexOf('Hello') != -1) {
}
confirm('How are you?');
</script>
0
reply
Damask-
Badges: 16
Rep:
?
#5
Report 5 years ago
#5
(Original post by SamRob85)
I tried this but it did not work
Code:
<script>
confirm('Hello!!');
var feeling = prompt('Feel free to talk to me');
if (varfeeling.indexOf('Hello') != -1) {
}
confirm('How are you?');
</script>

Your if statement is empty.
0
reply
Dez
Badges: 20
Rep:
?
#6
Report 5 years ago
#6
(Original post by SamRob85)
I tried this but it did not work
Code:
<script>
confirm('Hello!!');
var feeling = prompt('Feel free to talk to me');
if (varfeeling.indexOf('Hello') != -1) {
}
confirm('How are you?');
</script>
Replace "varfeeling" with "feeling". var is a keyword used to define a variable, it's not part of the name of that variable.
0
reply
SamRob85
Badges: 0
Rep:
?
#7
Report Thread starter 5 years ago
#7
(Original post by Dez)
Replace "varfeeling" with "feeling". var is a keyword used to define a variable, it's not part of the name of that variable.
I tried it but it does the same for any input.
0
reply
Dez
Badges: 20
Rep:
?
#8
Report 5 years ago
#8
(Original post by SamRob85)
I tried it but it does the same for any input.
I refer you to:

(Original post by Damask-)
Your if statement is empty.
0
reply
SamRob85
Badges: 0
Rep:
?
#9
Report Thread starter 5 years ago
#9
(Original post by Damask-)
Your if statement is empty.
What do you mean?
0
reply
incipientT
Badges: 16
Rep:
?
#10
Report 5 years ago
#10
(Original post by SamRob85)
What do you mean?
Your if statement doesn't contain any code, so regardless of what happens at the if statement the program will do the same thing.
0
reply
Planto
Badges: 16
Rep:
?
#11
Report 5 years ago
#11
To be explicit, here's what you have:

Code:
<script>
confirm('Hello!!');
var feeling = prompt('Feel free to talk to me');

// Check whether input contains hello
if (feeling.indexOf('Hello') != -1) {
    // Do absolutely nothing
}

// Show 'How are you?' confirm regardless of what happens
confirm('How are you?');
</script>
0
reply
SamRob85
Badges: 0
Rep:
?
#12
Report Thread starter 5 years ago
#12
(Original post by Planto)
To be explicit, here's what you have:

Code:
<script>
confirm('Hello!!');
var feeling = prompt('Feel free to talk to me');

// Check whether input contains hello
if (feeling.indexOf('Hello') != -1) {
    // Do absolutely nothing
}

// Show 'How are you?' confirm regardless of what happens
confirm('How are you?');
</script>
Would moving the confirm('how are you') to the //do absolutely nothing make it work?
0
reply
Planto
Badges: 16
Rep:
?
#13
Report 5 years ago
#13
(Original post by SamRob85)
Would moving the confirm('how are you') to the //do absolutely nothing make it work?
Bingo! The 'if' statement means, "if what's in these brackets is true, do what's between the following curly braces." The code outside of those braces runs regardless.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Roehampton
    Department of Media, Culture and Language; School of Education; Business School Undergraduate
    Tue, 19 Feb '19
  • Edge Hill University
    Undergraduate and Postgraduate - Campus Tour Undergraduate
    Tue, 19 Feb '19
  • University of Kent
    Sciences, Social Sciences and Humanities Further education
    Tue, 19 Feb '19

Do you give blood?

Yes (61)
8.5%
I used to but I don't now (17)
2.37%
No, but I want to start (269)
37.47%
No, I am unable to (172)
23.96%
No, I chose not to (199)
27.72%

Watched Threads

View All