Reputation: 33
So Im looking for help for writing a simple function in javascript. What I am trying to do is create an array of 6 name and print only those ending in "ie" or "y".
Here is my code - any help is appreciated. It unfortunately never reaches the function :(
//An array of names
var nameList = new Array("Freddie", "Mary", "Thomas", "Suzie", "Terry", "Kevin");
//Prints names before searching
document.write(nameList);
function e_names(nameList) {
for (var index = 0; index < arrayList.length; index++) {
var name = arrayList[index];
if (name == /\bie\b/ || name == /\by\b/)
document.write("",name, " </ br>");
index++;
} //End of for loop
} //End if method
Upvotes: 2
Views: 1026
Reputation: 10028
It unfortunately never reaches the function :(
In your code, you are defining a function but then never executing it. Execute it like so:
e_names(namesList);
The loop has a bug:
for (var index = 0; index < arrayList.length; index++) {
arrayList
is not defined, at least not in this code. It could be a previously defined or global variable, but then why does the function take a parameter called nameList
? I think this is a simple typo.
if (name == /\bie\b/ || name == /\by\b/)
I am not sure what it means to equal a regular expression, but I would hazard a guess it won't do what you want. I think you want the .test function such as:
if (/\bie\b/.test(name) || /\by\b/.test(name))
Your pattern includes \b
, the word boundary both before and after the y or ie. Therefore, it will only match strings that have i or ie as exact strings or with spaces around them. They won't match array
or key
or cookie
. I think you meant to anchor them to end of string as suggested by @xdazz's answer. He even OR'ed the two expressions together for you:
if (/(ie|y)$/.test(name))
Finally, you double-increment index
:
index++;
This will only iterate over "Freddie", "Thomas", and "Terry" because you increment twice. The index++
at the end of the loop construct is sufficient. Take this line out.
Altogether now:
//An array of names
var nameList = [ "Freddie", "Mary", "Thomas", "Suzie", "Terry", "Kevin" ];
//Prints names before searching
document.write(nameList);
function e_names(names) {
for (var index = 0; index < names.length; index++) {
var name = arrayList[index];
if (/(ie|y)$/.test(name)) {
document.write("",name, " </ br>");
}
} //End of for loop
} //End if method
e_names(nameList);
Note that I renamed the parameter so that it doesn't cause confusion with scoping and I also put curly braces around your if block. I usually don't change other people's code style, but I felt it was needed in this case to make it clearer.
I also changed the array construction to use the literal notation. Much debate has been had around the new Array
constructor. It's probably safe for this example, but if there is a single element or the array contents are dynamic, it gets confusing. For example, what does new Array(17)
mean? It means create an array with 17 null references in it. It does not create an array with the number 17 in it. For that reason, my advice is to use the [ ]
syntax. For details, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Upvotes: 1
Reputation: 160833
Use /(ie|y)$/
($ is used to match the end) to test it:
if (/(ie|y)$/.test(name))
Upvotes: 6