Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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);
                }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,026
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    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.

  • #4
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •