Jump to content

javascript indexOf()

shooter2749
Go to solution Solved by 79wjd,
11 minutes ago, shooter2749 said:

so I have to show every month with the letter R in it (month is "mois" in french, all the month name are in french sorry)

I think I got it right but for some reason it displays all the months idk why 


var mois =['Janvier','Février','Mars','Avril','Mai','Juin','Juillet',
            'Aout','Septembre','Octobre','Novembre','Décembre'];
            for(var i =0;i<mois.length;i++)
            {
                
                if(mois[i].indexOf('r'))
                {
                    document.writeln(mois[i])
                }
            }

 

The if branch will always be taken. You need to make it:

if (mois[i].indexOf('r') >= 0)

so I have to show every month with the letter R in it (month is "mois" in french, all the month name are in french sorry)

I think I got it right but for some reason it displays all the months idk why 

var mois =['Janvier','Février','Mars','Avril','Mai','Juin','Juillet',
            'Aout','Septembre','Octobre','Novembre','Décembre'];
            for(var i =0;i<mois.length;i++)
            {
                
                if(mois[i].indexOf('r'))
                {
                    document.writeln(mois[i])
                }
            }

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, shooter2749 said:

so I have to show every month with the letter R in it (month is "mois" in french, all the month name are in french sorry)

I think I got it right but for some reason it displays all the months idk why 


var mois =['Janvier','Février','Mars','Avril','Mai','Juin','Juillet',
            'Aout','Septembre','Octobre','Novembre','Décembre'];
            for(var i =0;i<mois.length;i++)
            {
                
                if(mois[i].indexOf('r'))
                {
                    document.writeln(mois[i])
                }
            }

 

The if branch will always be taken. You need to make it:

if (mois[i].indexOf('r') >= 0)

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

You need to check if mois.indexOf('r') !== -1. indexOf returns -1 if it cannot find the given element, and -1 evaluates to true, which is why all months are being displayed

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, PAEz said:

If your using a modern browser you could also have used 'includes'....

 


 if(mois[i].includes('r'))

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

This better as you're only after if an item is there not where the item is.

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×