Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2013, 04:37 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Problem with uppercase letters :D

Hi all guy!!! I have writing this code but my var word don't count the uppercase letters!!!
HOW TO FIX????
Code:
<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            function alphabet (letter)
            {
                var returnValueAlphabet = true;
                if (letter == "a" || letter == "b" || letter == "c" || letter == "d" || letter == "e" || letter == "f" || letter == "g" || letter == "h" || letter == "i" || letter == "j" || letter == "k" || letter == "l" || letter == "m" || letter == "n" || letter == "o" || letter == "p" ||letter == "q" || letter == "r" || letter == "s" || letter == "t" || letter == "u" || letter == "v" || letter == "w" || letter == "x" || letter == "y" || letter == "z")
                {
                    returnValueAlphabet = false;    
                }
                return returnValueAlphabet;
            }
            function start ()
            {
                var len = [];
                var count = 0;
                var phrase = 0;
                var numberWord = 0;
                var word = 0;
                var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present."; <------ *UPDATING  var arrayPhrase = ["Yesterday is history....ecc"];
                *UPDATING ---> ---> ---> ---> ---> --->    ---> ---> ---> ---> ---> --->      ---> ---> ---> ---> --->    
                for (i = 0; i < arrayPhrase.length; i++)
                {
                    if (arrayPhrase[i] == ".")
                    {
                        len[count] = numberWord;
                        numberWord = 0;
                        count++;
                        phrase++;
                    }
                    if (!alphabet (arrayPhrase[i]))
                    {
                        numberWord++;
                        word++;
                    }
                }
                alert ("Total words =" + " " + word + "\n\n" + "Total phrase =" + " " + phrase + "\n\n" + "Words in the phrase =" + " " + len.join (", "));
            }
	</script>
    </head>
    <body>
	<button type="button" onclick="start ()">TRY ME</button>
    </body>
</html>
*UPDATING, I reread my problem and call that the stringh "Yesterday...... ecc" is insert into a array!!!! And now all programs don t work....

Last edited by triko; 01-02-2013 at 04:47 PM..
triko is offline   Reply With Quote
Old 01-02-2013, 05:17 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try using .toLowerCase()

Quizmaster: A monocracy is a form of government in which how many people rule?
Contestant: Twelve.
__________________

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.
Philip M is offline   Reply With Quote
Old 01-02-2013, 05:19 PM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 457 Times in 444 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            function alphabet (letter) {
                var returnValueAlphabet = true;
                 letter =letter.toLowerCase();
                if (letter == "a" || letter == "b" || letter == "c" || letter == "d" || letter == "e" || letter == "f" || letter == "g" || letter == "h" || letter == "i" || letter == "j" || letter == "k" || letter == "l" || letter == "m" || letter == "n" || letter == "o" || letter == "p" ||letter == "q" || letter == "r" || letter == "s" || letter == "t" || letter == "u" || letter == "v" || letter == "w" || letter == "x" || letter == "y" || letter == "z")
                {
                    returnValueAlphabet = false;
                }
                return returnValueAlphabet;
            }
            function start ()
            {
                var len = [];
                var count = 0;
                var phrase = 0;
                var numberWord = 0;
                var word = 0;
                var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";
 //               <------ *UPDATING  var arrayPhrase = ["Yesterday is history....ecc"];
 //               *UPDATING ---> ---> ---> ---> ---> --->    ---> ---> ---> ---> ---> --->      ---> ---> ---> ---> --->
                for (i = 0; i < arrayPhrase.length; i++)
                {
                    if (arrayPhrase.charAt(i) == ".")
                    {
                        len[count] = numberWord;
                        numberWord = 0;
                        count++;
                        phrase++;
                    }
                    if (!alphabet (arrayPhrase.charAt(i)))
                    {
                        numberWord++;
                        word++;
                    }
                }
                alert ("Total words =" + " " + word + "\n\n" + "Total phrase =" + " " + phrase + "\n\n" + "Words in the phrase =" + " " + len.join (", "));
            }
	</script>
    </head>
    <body>
	<button type="button" onclick="start ()">TRY ME</button>
    </body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 01-02-2013, 06:32 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Try RECODING the entire function to be more sensible.
Code:
    function alphabet (letter)
    {
        return ( letter >= 'a' && letter <= 'z') || ( letter >= 'A' && letter <= 'Z' );
    }
** OR **
Code:
    function alphabet( letter )
    {
        var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return letter.indexOf(letter) >= 0;
    }
** OR **
Code:
    function alphabet( letter )
    {
        return ( /^[a-zA-Z]$/ ).test(letter);           
    }
** OR **
Code:
    function alphabet( letter )
    {
        return ( /^[a-z]$/i ).test(letter);           
    }
Or serveral other possible ways.

But *PLEASE* not by making 26 *SEPARATE* tests.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-02-2013, 06:51 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Of course, there is no reason at all to even need that alphabet function.
Code:
function start ()
{
    var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";

    var phrases = arrayPhrase.split(".");
    var wordsPerPhrase = [ ];
    var totalWords = 0;
    for ( var p = 0; p < phrases.length - 1; ++p )
    {
        var words = phrases[p].replace(/^\s+/,"").replace(/\s+$/,"").split(" ");
        wordsPerPhrase[p] = words.length;
        totalWords += words.length;
    }
    alert (  "Total words = " + totalWords + "\n\n" 
           + "Total phrases = " + phrases.length + "\n\n" 
           + "Words in each phrase = " + wordsPerPhrase.join(", ")
          );
}
start();
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-03-2013, 05:40 PM   PM User | #6
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
I don t see anyone of your method Pedant!!! xD
And your last comment!! It's a mystery for me \s+\... I don't think it's a code for beginner
However thanks for all example, I'll remember for the next time!!!!
triko is offline   Reply With Quote
Old 01-03-2013, 05:45 PM   PM User | #7
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by vwphillips View Post
Code:
<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            function alphabet (letter) {
                var returnValueAlphabet = true;
                 letter =letter.toLowerCase();
                if (letter == "a" || letter == "b" || letter == "c" || letter == "d" || letter == "e" || letter == "f" || letter == "g" || letter == "h" || letter == "i" || letter == "j" || letter == "k" || letter == "l" || letter == "m" || letter == "n" || letter == "o" || letter == "p" ||letter == "q" || letter == "r" || letter == "s" || letter == "t" || letter == "u" || letter == "v" || letter == "w" || letter == "x" || letter == "y" || letter == "z")
                {
                    returnValueAlphabet = false;
                }
                return returnValueAlphabet;
            }
            function start ()
            {
                var len = [];
                var count = 0;
                var phrase = 0;
                var numberWord = 0;
                var word = 0;
                var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";
 //               <------ *UPDATING  var arrayPhrase = ["Yesterday is history....ecc"];
 //               *UPDATING ---> ---> ---> ---> ---> --->    ---> ---> ---> ---> ---> --->      ---> ---> ---> ---> --->
                for (i = 0; i < arrayPhrase.length; i++)
                {
                    if (arrayPhrase.charAt(i) == ".")
                    {
                        len[count] = numberWord;
                        numberWord = 0;
                        count++;
                        phrase++;
                    }
                    if (!alphabet (arrayPhrase.charAt(i)))
                    {
                        numberWord++;
                        word++;
                    }
                }
                alert ("Total words =" + " " + word + "\n\n" + "Total phrase =" + " " + phrase + "\n\n" + "Words in the phrase =" + " " + len.join (", "));
            }
	</script>
    </head>
    <body>
	<button type="button" onclick="start ()">TRY ME</button>
    </body>
</html>
Hi guy... This code don't work, my anti-bug crah when arrive at this part:
if (arrayPhrase.charAt(i) == ".")
Don't know how to works with the array!!!
triko is offline   Reply With Quote
Old 01-03-2013, 07:50 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
My code explained:
Code:
function start ()
{
    var arrayPhrase = "Yesterday is history. Tomorrow is a mystery. But today is a gift. For this it is called present.";

    // split breaks a sting into an ARRAY of strings, all broken
    // apart on the character (or regular expression) you give:
    var phrases = arrayPhrase.split(".");
    // so now we have each sentence (that used to end in a period)
    // in a separate element of the array named phrases
    
    var wordsPerPhrase = [ ];
    var totalWords = 0;

    // loop through all the phrases/sentences in the phrases array
    // notice that we do *NOT* process the last element of the array
    // because it will be the blank string after the last period
    for ( var p = 0; p < phrases.length - 1; ++p )
    {

        // this is the trickiest part, so let's break it down in pieces:
        //      phrases[p] :: get one phrase from the array
        //      .replace(/^\s+/,"") :: replace all *leading* spaces with nothing
        //      .replace(/\s+$/,"") :: replace all *trailing* spaces with nothing
        //      (in other words, trim the spaces off both ends
        //      .split(" "); :: split the phrase into words, just as we split into phrases
        var words = phrases[p].replace(/^\s+/,"").replace(/\s+$/,"").split(" ");

        // so now we have an array named words...and it has as many elements
        // as there are words in the phrase!

        // put the count of words in to proper element of the wordsPerPhrase array  
        wordsPerPhrase[p] = words.length;

        // and bump the count of the total number of words
        totalWords += words.length;
    }
    
    // and then just show the final numbers:
    // I corrected the value for Total Phrases from my prior post
    alert (  "Total words = " + totalWords + "\n\n" 
           + "Total phrases = " + (phrases.length-1) + "\n\n" 
           + "Words in each phrase = " + wordsPerPhrase.join(", ")
          );
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-04-2013, 12:26 AM   PM User | #9
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 457 Times in 444 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Quote:
Hi guy... This code don't work, my anti-bug crah when arrive at this part:
if (arrayPhrase.charAt(i) == ".")
Don't know how to works with the array!!!
arrayPhrase is not an array it is a string

to access a specific character charAt(nu) is required

I tested the script I posted with IE and FF
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 01-04-2013, 03:05 AM   PM User | #10
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by vwphillips View Post
arrayPhrase is not an array it is a string

to access a specific character charAt(nu) is required

I tested the script I posted with IE and FF
But in my inizial post i write arrayPhrase = ["Yesterday.....];
This is an array!!! I would use an array!!!
triko is offline   Reply With Quote
Old 01-04-2013, 04:42 AM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by triko View Post
But in my inizial post i write arrayPhrase = ["Yesterday.....];
This is an array!!! I would use an array!!!
That would then be an array containing ONE element which is a string. Then arrayPhrase[0] will be the entire string and arrayPhrase[1] will be beyond the end of the array.

Using array notation to reference characters in a string has been recently introduced as an alternative to using charAt but there are still lots of browsers in use that don't support it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-04-2013, 04:19 PM   PM User | #12
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
That would then be an array containing ONE element which is a string. Then arrayPhrase[0] will be the entire string and arrayPhrase[1] will be beyond the end of the array.

Using array notation to reference characters in a string has been recently introduced as an alternative to using charAt but there are still lots of browsers in use that don't support it.
An ok!!!! But however if I use charAt, still don't work !!!!!!!!!!!
triko is offline   Reply With Quote
Old 01-04-2013, 07:13 PM   PM User | #13
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by triko View Post
An ok!!!! But however if I use charAt, still don't work !!!!!!!!!!!
So what does your code look like now. Also are there any error messages on the error console?
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-04-2013, 10:58 PM   PM User | #14
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
So what does your code look like now. Also are there any error messages on the error console?
No error in console, but I find error!!!
When I write array = ["Yehxcwj ecc...."]
the programs read this how to one element of array, so when start cicle for (i = 0; i < array.length; i++), the programs execute only one for because the length of array is 0. How to fix this problem!?!?! Beacause my problem ask: " Is stored in an array a text of char. Print the numbers of word, phrase and word for phrase!!!".
If I use an simple var the progrma work perfectly
triko is offline   Reply With Quote
Old 01-04-2013, 11:07 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Felgall already answered that question:
Quote:
... Then arrayPhrase[0] will be the entire string ...
So just do
Code:
var text = arrayPhrase[0];
and then replace all uses of arrayPhrase after that point with text.

But I strongly suspect you are misreading the homework assignment. Or the instructor just used the wrong words. Maybe you should copy the assignment here *VERBATIM*.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:32 PM.


Advertisement
Log in to turn off these ads.