...

View Full Version : Problem Cycle FOR



triko
11-09-2012, 05:37 PM
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 :D

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

felgall
11-09-2012, 10:08 PM
You can get the number you want with a single JavaScript statement (assuming that you have already checked that the name only contains letters).


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.

Old Pedant
11-09-2012, 10:16 PM
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


var count = 0;

in the wrong place. It needs to be *before* the for( ) statement.

triko
11-10-2012, 01:02 AM
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


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!!! :confused:
So, Thank you :D

felgall
11-10-2012, 01:55 AM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum