Also, these are evaluating as strings, not integers. Did you want integer values?
__________________ ^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
Rewriting *ONLY* what you showed there, not attempting to extend it as WolfShade did:
Code:
var s = arrayNumber[1] + arrayNumber[2];
if ( ( /^1[1-9]$/ ).test(s) )
{
// pattern is 1 followed by any digit except 0
} else {
// pattern is not matched
}
Maybe if you *DESCRIBED* what you want instead of writing bad JS code we could help you more?
__________________
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.
Yes, you're right!!
In my problem i do translate from number ex : 125020 to letters, one hundred twenty-five thousand and twenty!
And i use more if and put an condition if number from 1 to 9
But can you explain me what means this symbol ? : if ( ( /^1[1-9]$/ ).test(s) )
In my problem i do translate from number ex : 125020 to letters, one hundred twenty-five thousand and twenty!
So what is the point in testing just the FIRST TWO characters, as your ugly code was trying to do?
Quote:
But can you explain me what means this symbol ? : if ( ( /^1[1-9]$/ ).test(s) )
/ ==>> beginning of a regular expression
^ ==>> beginning of text (that is, nothing can appear before what we are testing)
1 ==>> the digit 1, only. Nothing else allowed.
[1-9] ==>> any digit from 1 to 9
$ ==>> the end of the text (nothing can appear after what we are testing)
/ ==>> the end of the regular expression
In short, that says "Look for text that has a 1 as the first character and a 1 through 9 as the second character and is exactly 2 characters long."
But who cares? It's entirely irrelevant to what you really need.
__________________
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.
<!DOCTYPE html>
<html>
<head>
<title>Numbers to words</title>
<style type="text/css">
* { font-size: x-large; }
</style>
</head>
<body>
<div>
<form>
Enter a number: <input id="theNumber" />
<hr />
That number in words:<br />
<span id="words"></span>
</form>
</div>
<script type="text/javascript">
var UnitNames = [" zero"," one"," two"," three"," four",
" five"," six"," seven"," eight"," nine" ];
var TeenNames = [" ten"," eleven"," twelve"," thirteen"," fourteen",
" fifteen"," sixteen"," seventeen"," eighteen"," nineteen" ];
var DecadeNames = [" zero"," ten"," twenty"," thirty"," forty",
" fifty"," sixty"," seventy"," eighty"," ninety" ];
document.getElementById("theNumber").onchange =
function( )
{
var text = document.getElementById("words");
text.innerHTML = NumberAsWord( parseInt(this.value) );
};
function NumberAsWord( num )
{
var millions, thousands, hundreds, decades, result;
if ( isNaN(num) )
{
return "<i>That is NOT a valid number!</i>";
}
if ( num == 0 )
{
return "zero";
}
result = "";
if ( num < 0 )
{
num = - num;
result = "<i>NEGATIVE</i> ";
}
millions = Math.floor( num / 1000000 );
num %= 1000000;
if ( millions > 0 )
{
if ( millions > 999 )
{
return "BILLIONS and BILLIONS";
}
result += NumberAsWord( millions ) + " million"
if ( num == 0 ) return result;
}
thousands = Math.floor( num / 1000 );
num %= 1000;
if ( thousands > 0 )
{
result += NumberAsWord( thousands ) + " thousand"
if ( num == 0 ) return result;
}
hundreds = Math.floor( num / 100 );
num %= 100;
if ( hundreds > 0 )
{
result += UnitNames[ hundreds ] + " hundred"
}
decades = Math.floor( num / 10 );
num %= 10;
if ( decades == 1 )
{
result += TeenNames[ num ];
} else {
if ( decades > 1 )
{
result += DecadeNames[ decades ];
}
if ( num > 0 )
{
result += UnitNames[ num ];
}
}
return result;
}
</script>
</body>
</html>
:O :O :O :O :O :O :O
Don ' t have word!!! Very unbelievable program.... You are a pro!!! my program is very bad....
and don t work well!!!
Code:
<!DOCTYPE>
<html>
<head><title>Converter</title>
<script>
function convert ()
{
var final = "";
var foo = 0;
var text = document.getElementById("number").value;
var len = text.length;
var arrayUnità = ["", "uno" , "due" , "tre", "quattro", "cinque", "sei", "sette", "otto", "nove"];
var arrayPrimi = ["", "undici", "dodici", "tredici", "quattordici", "quindici", "sedici", "diciassette", "diciotto", "diciannove"];
var arrayDecine = ["", "dieci", "venti", "trenta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta"];
var cento = "cento";
var mille = "mille";
var mila = "mila";
var arrayNumber = [];
for (i = 0; i < len; i ++)
{
arrayNumber[foo] = text[foo];
foo++;
}
for (i = 0; i < len; i++)
{
if (len == 4)
{
for (a = 0; a < len; a++)
{
if (arrayNumber[0] == "1" && arrayNumber[1] == "1" && arrayNumber[2] == "1")
{
var f = arrayNumber[a];
final = mille + cento;
a = 3;
var f = arrayNumber[a];
final += arrayPrimi[f];
alert (final);
return;
}
if (arrayNumber[0] == "1" && arrayNumber[1] != "1" && arrayNumber[2] != "1")
{
final = mille;
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
a++;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
return;
}
if (arrayNumber[0] == "1" && arrayNumber[1] != "1" && arrayNumber [2] == "1" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
{
final = mille;
a++;
var f = arrayNumber[a];
final += arrayUnità[f] + cento;
a = 3;
var f = arrayNumber[a];
final += arrayPrimi[f];
alert (final);
return;
}
if (arrayNumber[0] == "1" && arrayNumber[1] == "1" && arrayNumber[2] != "1")
{
final = mille + cento;
a = 2;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
return;
}
if (arrayNumber[1] == "1" && arrayNumber[2] == "1")
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila + cento;
a = 3;
var f = arrayNumber[a];
final += arrayPrimi[f];
alert (final);
return;
}
if (arrayNumber[1] == "1" && arrayNumber[2] != "1")
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila + cento;
a = 2;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
return;
}
if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] == "0" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila;
a = 3;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
return;
}
if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] != "1")
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila;
a = 2;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
return;
}
if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] == "1" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila;
a = 2;
a++;
var f = arrayNumber[a];
final += arrayPrimi[f];
alert (final);
return;
}
else
{
var f = arrayNumber[a];
final = arrayUnità[f] + mila;
a++;
var f = arrayNumber[a];
final += arrayUnità[f] + cento;
a++;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert (final);
}
}
}
if (len == 3)
{
for (a = 0; a < len; a++)
{
if (arrayNumber[1] == "1" && (arrayNumber[2] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
{
final = cento;
a = 2;
var f = arrayNumber[a];
final += arrayPrimi[f];
alert (final);
return;
}
if (arrayNumber[0] == "1")
{
// ora è 9, ad ogni ciclo passa al numero dopo!!
final = cento;
a++;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
aler(final);
return;
}
else
{
var f = arrayNumber[a] // ora è 9, ad ogni ciclo passa al numero dopo!!
final = arrayUnità[f] + cento;
a++;
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a];
final += arrayUnità[f];
alert(final);
return;
}
}
}
if (len == 2)
{
for (a = 0; a < len; a++)
{
if (arrayNumber[0] == 1 && (arrayNumber[2] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
{
a++;
var f = arrayNumber[a];
final = arrayPrimi[f];
}
else
{
var f = arrayNumber[a];
final += arrayDecine[f];
a++;
var f = arrayNumber[a]
final += arrayUnità[f];
}
alert (final);
}
}
if (len == 1)
{
if (arrayNumber[0] == "0")
{
alert ("zero");
}
else
{
a = 0;
var f = arrayNumber[a];
final = arrayUnità[f];
alert (final);
}
}
len = 0;
}
}
</script>
</head>
<body>
<p>Insert number of max 4 character</p>
<input type = "text" id = "number" /> <br /><br />
<button type="button" onclick="convert ()"> CONVERT TO STRING </button>
</body>
</html>
The proposed code treats strings rather than numbers. The function anyNumber cuts simply and logically the numbers in three digits slices with a replace method (the regular expression captures each one to three digit slice, before a multiple of three digits) not to replace this slices with return values, but only to call the function for triplets and to define the future response chn...
EDIT : Change the chn.replace(/tre$/g,'tré') with a chn.replace(/(\w)tre\b/g,'$1tré') to replace tre by tré only at the end of a word (after a letter).
Last edited by 007julien; 01-14-2013 at 09:04 AM..
Reason: errors