• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why doesnt my code work

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im doing a DIY Javascript book and one of the problems i cant seem to solve involves the following code


any help as to why this doesnt count the numbers of "a"'s in a string would be very much appriciated, im pretty sure its somthing small.

thanks in advance
[ July 09, 2006: Message edited by: Ricci Dolan ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the HTML and JavaScript forum.
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing that sticks out is the 'while' command is almost certainly incorrect. Reformatting your code, I can see that the while command is actually just a single statement:So, unless you happen to enter 'z' as the very first letter, you will end up in an infinite loop whereby you are just setting the value for 'total' over and over again.

To fix that particular problem, change the code to:Try that - you should then see that the program works (for a given value of 'works' ). A hint for the next problem - what is the difference between '=' and '=='?

Regards, Andrew
 
Ricci Dolan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for that,

== equal to
= equals
?

either way it still wont count the number of a's

[ July 09, 2006: Message edited by: Ricci Dolan ]
[ July 09, 2006: Message edited by: Ricci Dolan ]
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what results are you getting now?

Regards, Andrew
 
Ricci Dolan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well....



it counts the number of times i enter a string containing "a" not the number of "a" in a string

and


doesnt count at all
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this is a good start:

Originally posted by Ricci Dolan:
== equal to
= equals

But you didnt think of all the places where you might be doing an assignment rather than a comparison. Specifically:Your statement is "if (assignment)" and in many languages (Javascript included) this will always evaluate to true.

Does that help?

Regards, Andrew
[ July 09, 2006: Message edited by: Andrew Monkhouse ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you need to count how many occurences of 'a' inside a string, you
could use string.charAt(index) to compare.

something like:



--Nimchi
 
Ricci Dolan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your statement is "if (assignment)" and in many languages (Javascript included) this will always evaluate to true.

Does that help?

Regards, Andrew

[ July 09, 2006: Message edited by: Andrew Monkhouse ][/qb]<hr></blockquote>

thanks for sticking with me Andrew you are helping
ok I get what your saying and have changed the code



as i read the code it makes sence in my head and i think it should work
if not equal to z it keeps asking for the string
if equal to A it adds 1 to the total
BUT
it doesnt.

Also thanks Nimchi your program works fine but i think its using commands that are above my level and it doesnt really help my understanding why my exising code doesnt work.Thanks again though.
[ July 10, 2006: Message edited by: Ricci Dolan ]
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, we are getting closer all the time

You have the following line of code:Unfortunately this line does not give tell the Javascript interpreter what it should do with the result of adding "total + 1". That is, "total + 1" will give us a value, but that value is not assigned to anything.

Your previous version of the code had which is assigning the value created by the "total + 1" back into "total".

So try putting that section only of the code back to the way it was. Keep your new if (letter=='a') statement, just change what happens when that conditional is true.

Regards, Andrew

P.S. Do you realize that this code can only handle a single character being entered at a time? That is, if you enter "astrology" it will not match "a", likewise entering "zoo" wont match "z" (so you wont exit).
 
Ricci Dolan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup andrew i realised this last night
i knew that it stopped when i entered a single Z so i entered a single "a" and found that it was counting

thanks for everyones help ive finaly moved on from this code
 
reply
    Bookmark Topic Watch Topic
  • New Topic