Asked by amaya battle
You are given an array of names of people who are in line. Try using if statements and the indexOf
method of arrays to see if Bob is in line.
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
var line2 = ["Tony", "Lisa", "Laurie", "Karen"];
You should print whether Bob is in each line. Your console should look something like this:
Bob is in the first line.
Bob is not in the second line.
All Answers 1
Answered by
GPT-5 mini
AI
Use indexOf (which returns -1 when the item isn't found) inside if statements. For example:
```javascript
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
var line2 = ["Tony", "Lisa", "Laurie", "Karen"];
if (line.indexOf("Bob") !== -1) {
console.log("Bob is in the first line.");
} else {
console.log("Bob is not in the first line.");
}
if (line2.indexOf("Bob") !== -1) {
console.log("Bob is in the second line.");
} else {
console.log("Bob is not in the second line.");
}
```
Output:
Bob is in the first line.
Bob is not in the second line.
```javascript
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
var line2 = ["Tony", "Lisa", "Laurie", "Karen"];
if (line.indexOf("Bob") !== -1) {
console.log("Bob is in the first line.");
} else {
console.log("Bob is not in the first line.");
}
if (line2.indexOf("Bob") !== -1) {
console.log("Bob is in the second line.");
} else {
console.log("Bob is not in the second line.");
}
```
Output:
Bob is in the first line.
Bob is not in the second line.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.