Jump to content

Recommended Posts

Hi please could someone point me in the right direction

 

I have to do a piece of javascript that asks the user to type in a sentence then print out each word on a separate line then count out a given letter (say the letter A) and display how many times that letter appears in that word the number also needs to appear beside that word so say apple a day keeps the doctor away would print out

 

apple 1

a 1

day 1

keeps 0

the 0

doctor 0

away 2

 

I hope this makes sense

 

here is my code

 

cheers rob

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Words with an A</title>
<script type="text/javascript">
        var theText = prompt('Please enter the text that you want to check')
        
        function WordsWithA(phrase)
        {
                this.phrase = phrase                                                                            // Assign paramater phrase to the variable phrase
                var wordsArray = phrase.split(" ");                                                     // Split the phrase into indivisual words
                var aWordCount = 0;                                                                                     // Create variable that will contain the number of words with the letter a in
                
                for(var i=0; i<wordsArray.length; i++)                                          // Loop over the words array
                        {
                                document.write(wordsArray[i]+ "<br />")                         // Output each word to the document and add a line break after each one
                                if(wordsArray[i].indexOf("a") > -1)                                     // Check to see if the word contains a letter a
                                        {
                                                aWordCount +=1;                                                         // If the word contains a letter a, add one to the count
                                        }
                        }
                
                alert(aWordCount + " of the provided words contain the letter a");      // Output the number of words with an a in an alert dialogue
                
        }       

        WordsWithA(theText);

</script>

</head>
<body>

Link to comment
https://forums.phpfreaks.com/topic/254728-javascript-help-needed-please/
Share on other sites

This part needs to be modified:

 

document.write(wordsArray[i]+ "<br />")                         // Output each word to the document and add a line break after each one
if(wordsArray[i].indexOf("a") > -1)                                     // Check to see if the word contains a letter a
{
  aWordCount +=1;                                                         // If the word contains a letter a, add one to the count
}

 

You need to remove the linebreak tag from your document.write so that you can add the letter count next to the word. Or better yet, move your document.write below the code that determines how many instances of "a" are in it, so that you can document.write() everything in one go.

 

Then inside your condition..  You need to either loop through each letter in wordsArray and increment a variable (example: numLetter) each time it matches the letter "a", or use a .match() regex instead of .indexOf() to match for it and count the returned results and put it into numLetter. 

 

Also note that you are currently doing a case-sensitive search, so for instance, it will not count the "A" in "Andy".  Since you probably want it to be case-insensitive, you will need to first wordsArray.toLowercase()  or use the i modifier if you opt for the regex approach.

 

Finally, you will need to actually document.write numLetter after this code (and then add the linebreak tag) so that it will show the result next to the word.

 

 

Hi thanks for that info but I forgot to say I'm a newbie so need abit of help with what you said as a friend helped with the code I have saw but he doesn't know how to get it to do the line count thing we kind of cobbled that together

 

Sorry for being dim and a paid

 

Thanks

 

Rob

You asked for a push in the right direction and I explained what more you need to do.  If you and your friend "cobbled" that together then you should have enough understanding to read what I wrote and apply it.  Was there something in particular you didn't understand in my response? Do you need some clarification on a particular point in my reply?

 

No offense, but I'm not sure what more I can do for you, short of writing the code for you...responding something vague like "I need help" when I gave you the help you asked for to me means what you're *really* asking for is for me to write your code for you, which I'm not going to do, because:

 

1) This smells like homework.  What is the point in taking a class in something if you're just going to c/p and not actually learn anything

2) I'm not here to write your code for you.  Unless you wanna pay me, then I will write it for you.  This sort of conflicts with the first point but I'm not your dad; if you wanna waste your time in class not learning anything then that's on you.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.