...

View Full Version : Need help with array problem



ciwwebdude
10-15-2005, 05:57 AM
I have an assinment that I have to do and I need help with the problem. The following is what I need it to do;

Make a Javascript program that will read an array of 10 to 20 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array.

I can make the array and every thing but I cannotfind anyway to do the counting portion of it and I need help soon. If someone can give me the code to do it taht would be awesome.

Thanks, ciwwebdude

Mr J
10-15-2005, 09:29 AM
Post the code you have

ciwwebdude
10-15-2005, 04:35 PM
Here is one of the code that I have started for my problem. Let me know if you can help. Thanks

<html>
<body>

<script type="text/javascript">

function sortNumber(A,E,I,O,U)
{
return A,E,I,O,U
}
var characters = new Array(10)
characters[0] = "A"
characters[1] = "1"
characters[2] = "B"
characters[3] = "J"
characters[4] = "I"
characters[5] = "6"
characters[6] = "E"
characters[7] = "W"
characters[8] = "Y"
characters[9] = "U"

document.write(characters + "<br />")
document.write(characters.sort(sortNumber))


</script>

</body>
</html>

Mr J
10-15-2005, 06:39 PM
All you have there is the array, where's the rest of the code you have been working on?

ciwwebdude
10-16-2005, 04:08 AM
I must have deleted the one with the sorting that I did. Ill have to look again and see if I can find it. Any suggestions though on how to do the sorting count for the vowels? Any suggestions or an example of code that I could use would be great.

Thanks Ciwwebdude

AlexVN
10-16-2005, 10:35 AM
var chars = new Array('a', '1', 'b', 'j', 'i', '6', 'e', 'w', 'y', 'u');
var counts = new Object();
counts['a'] = counts['i'] = counts['o'] = count['u'] = 0;
for (var i = 0; i < chars.length; i++) {
switch (chars[i]) {
case 'a':
counts['a']++; break;
case 'i':
counts['i']++; break;
case 'o':
counts['o']++; break;
case 'u':
counts['u']++; break;
}
}


Then the counts['a'] element will contain the numder of 'a' character occurrences in the chars array.

Mr J
10-16-2005, 12:41 PM
Well AlexVN, you have just done ciwwebdude homework for him/her so I might as well post mine now.

The following will also check for mulitple instances of a vowel within the same index



<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<div id="display"></div>

<script type="text/javascript">
<!--
var characters = new Array()
characters[0] = "A"
characters[1] = "1"
characters[2] = "B"
characters[3] = "J"
characters[4] = "I"
characters[5] = "6"
characters[6] = "E"
characters[7] = "W"
characters[8] = "Y"
characters[9] = "U"

vowel=new Array("a","e","i","o","u")

characterCount=new Array()

for(var i=0;i<characters.length;i++){
checkCharacters=characters[i].toLowerCase()

for(var j=0;j<vowel.length;j++){
if(i==0){characterCount[j]=0}

nextCharacter=checkCharacters.indexOf(vowel[j])

while(nextCharacter!=-1){
characterCount[j]++
nextCharacter=checkCharacters.indexOf(vowel[j],nextCharacter+1)
}

}

}

for(k=0;k<characterCount.length;k++){
document.getElementById("display").innerHTML+="Number of character "+vowel[k].toUpperCase()+" = "+characterCount[k]+" <br>"
}

//-->
</script>

</BODY>
</HTML>

ciwwebdude
10-16-2005, 08:42 PM
I would ike to thank you, MR J and Alexvn. I have been looking for the code and examples on how to do what i needed. But I could not figure it out. I cant get any help or examples from my instructor and I was at a dead end. i dont think he is any good at java script and he doesnt seem to want to help students. I would like to say that ya'lls javascript skills are awesome. Can you recommend any good books or website to learn java script and areas like the count that you did in the code. I wil probly be talking to you guys through the forum. Again, thanks so much for the help.

Ciwwebdude



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum