Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  136] [ 3]  / answers: 1 / hits: 23619  / 11 Years ago, fri, may 3, 2013, 12:00:00

So let's say I have an array that looks like this:



weather = [sun, clouds, rain, hail, snow]


And I want to find and display all of the strings which have the letter s in them. This is what I think I should do...



for(var i = 0; i < weather.length; i++)
{
if(weather[i].indexOf('s') != -1)
{
alert(weather);
}
}


But that just displays all of the weather strings as many times as there are strings with the letter s in them. (It will just alert: sun, clouds, rain, hail, snow 3 times)



How do I get it to alert just the specific names of the weather which contain the letter s?


More From » arrays

 Answers
11

You need to do alert(weather[i]) instead of alert(weather)



Check this fiddle


[#78440] Thursday, May 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tai

Total Points: 466
Total Questions: 87
Total Answers: 116

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;