View Single Post
Old 12-02-2012, 09:39 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Can I point out that North Korea, Saudi Arabia, South Africa, United States and United Kingdom are so spelled with spaces?

We have had similar homework at http://www.codingforums.com/showthread.php?t=281661

As this is plainly homework here is no point in offering the OP (this is his first post here) advanced/sophisticated solutions. Teacher may be dim, but not so dim as not to suspect external help!

Code:
<script type = "text/javascript">

var names = ["AFGHANISTAN", "ALGERIA", "ARGENTINA", "BANGLADESH", "BRAZIL", "BURMA",
"CANADA", "CHINA", "COLUMBIA", "CONGO", "EGYPT", "ETHIOPIA", "FRANCE",
"GERMANY", "GHANA", "INDIA", "INDONESIA", "IRAN", "IRAQ", "ITALY",
"JAPAN", "KENYA", "MALAYSIA", "MEXICO", "MOROCCO", "NEPAL", "NIGERIA",
"NORTH KOREA", "PAKISTAN", "PERU", "PHILIPPINES", "POLAND", "RUSSIA",
"SAUDI ARABIA", "SOUTH AFRICA", "SOUTH KOREA", "SPAIN", "SUDAN", "TAIWAN",
"TANZANIA", "THAILAND", "TURKEY", "UGANDA", "UKRAINE", "UNITED KINGDOM",
"UNITED STATES", "UZBEKISTAN", "VENEZUELA", "VIETNAM", "YEMEN"];

function filterByLetter(letter) {

var letter = letter.toUpperCase();
var counter = 0;
var results = [];

for(var i=0; i<names.length; i++) {
if (names[i].indexOf(letter)>=0) {  // -1 means the letter was NOT found
results[counter] = names [i];
counter ++;
}
}

var final = results.join("\n");
alert ("The letter " + letter + " was found in the following " + counter + " country names\n\n" + final)
}

filterByLetter("u");
filterByLetter(" ");


</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-02-2012 at 10:17 AM..
Philip M is offline   Reply With Quote