Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-17-2005, 01:31 AM   #1 (permalink)
watson6
Registered User
 
Join Date: May 2005
Posts: 1
watson6 is on a distinguished road
Counting letters

I am new to JavaScript and would appreciate a little help.
I have a small program to count the number of times a letter a is input. it finishes when a z is input and displays the total number of a's at the end.

This is the code i have and i am sure i am missing something very simple but cannot see it. many thanks.

<html>

<script>

var letter, total;
total = 0;

letter = window.prompt('Please enter a letter, z to
stop', '');

while (letter != 'z')
{


if (letter ='a')
{
total = total + 1
}

letter = window.prompt('Please enter a letter, z to
stop', '')
}

document.write('The number of \'a\'s you have input is
' + total)

</script>

</html>
watson6 is offline   Reply With Quote
Old 05-17-2005, 01:31 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
you probably want to check each letter instead of assuming the user will enter a z. try this:
HTML Code:
<script>

var letter, length, total;
total = 0;

letter = window.prompt('Please enter a letter, z to stop', '');
length = letter.length;

var my_array = letter.split("");

for(var i=0;i<length;i++){
  
  if( my_array[i] == 'a' ){
    total++;
  }
}


document.write('The number of \'a\'s you have input is ' + total)

</script>
it will only work in IE though ( i think )
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me out with Counting Colours inox Java 5 02-17-2005 08:26 PM
numbers to letters?? keystoneman Standard C, C++ 12 10-21-2004 05:37 PM
Character Counting Program DemosthenesB Standard C, C++ 8 04-03-2003 05:10 PM


All times are GMT -8. The time now is 11:46 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting