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 11-09-2012, 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 Cycle FOR

This program Calculate the TAX CODE! If the name have less than 3 consonants, is insert the first vocal at the end of 2 consonants; EXAMPLE:
1° case
LUCA ==> L, C, U
ANNE ==> N, N, A
BELA ==> B, L, E
If the name have more of 3 consonants, I take first, and second jump, and I take third and fourth consonants EX:
2° case
ALEXANDER ==> L, N, D
JONATHAN ==> J, T, H
SANDRO ==> S, D, R
3° case
If the name have less to 3 letters, I add X at the end of word
RA ==> RAX
AS ==> ASX
Ok now that i have explained how to work Tax Code, I have a problem; I don't know how to count the consonants in the name EX: LUCA, for insert the "U" at the end of consonants!!!
I say " It's possible insert a VARiable = 0 into the cycle FOR that increases with each consonants, that at the end I have a VAR with a value < 3 or > 3, for continue and create 1°, 2°, and 3° case???"
Thanks you all for your Time and Attention

This is code
Code:
            function calculateName(name)
            {
               
                var myName = "";
                for (var i = 0; i < name.length; i++)
                {
                    var count = 0;
                    if ( (name[i] != "a") && (name[i] != "e") && (name[i] != "i")  && (name[i] != "o") && (name[i] != "u") )
                    {
                        var myName;
                        myName = myName + name[i];
                        myName = myName.substring (0,3);
                        count ++;
                    }
                }
                alert (myName);
                alert (count);
            }
triko is offline   Reply With Quote
Old 11-09-2012, 09:08 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You can get the number you want with a single JavaScript statement (assuming that you have already checked that the name only contains letters).

Code:
consonants = name.replace(/[aeiou]/ig,'').length;
There is no need to use a loop for something so simple.

Of course if this is for a history of JavaScript course then you wouldn't be able to use that solution because the historic version of JavaScript you are learning to use in old versions of the Netscape browser will not cover that.
__________________
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 11-09-2012, 09:16 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
This is the same homework that generated a bunch of responses about a week ago.

You are the 3rd person to come here asking us to do your homework.

The first person sneakily concealed the fact it was homework.

But I will tell you that you have the line
Code:
            var count = 0;
in the wrong place. It needs to be *before* the for( ) statement.
__________________
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 11-10-2012, 12:02 AM   PM User | #4
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 Old Pedant View Post
This is the same homework that generated a bunch of responses about a week ago.

You are the 3rd person to come here asking us to do your homework.

The first person sneakily concealed the fact it was homework.

But I will tell you that you have the line
Code:
            var count = 0;
in the wrong place. It needs to be *before* the for( ) statement.
o.O o.O o.O
Pedant, but..... How did you do???
I stay 2 hour... But don t find the solution... You arrive and write, before the for() statement, and solve my problem..... You are a genius.. And i'm stupid, maybe this isn't my subject... I don't understand and don't find the solution!!!
So, Thank you
triko is offline   Reply With Quote
Old 11-10-2012, 12:55 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
There are two factors that make the difference.

1. You are not very familiar with JavaScript and so do not know how you would expect the script to look or what sots of things to look for that commonly use problems. Old Pedant is very familiar with JavaScript and so is far more likely to spot errors.

2. You wrote the code so you know exactly what it is supposed to do so in looking at the code you easily overlook small errors because you read that piece of code the way you expect it to work rather than as how it actually will work. That's why even stopping to explain the problem to someone else often leads to the solution as you start looking at the code in a different way.

There are two things you can do to make finding errors in your JavaScript easier. First make use of http://jslint.com/ to check your JavaScript syntax - simply paste your script into that page and check the "assume a browser" checkbox and it will give you a list of everything you need to change to fix all the syntax errors as well as a lot of the code that is potentially going to cause errors. Second, all browsers except Firefox have a JavaScript debugger built in that you can use to step through your code and see exactly what each statement does (you can install an debugger as a Firefox extension if you need to debug in that browser).
__________________
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
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 03:24 PM.


Advertisement
Log in to turn off these ads.