I am having a problem converting a physical string to a integer. I have a dropdown list of a few items but are numbers though in chracters. I ned to output the value and the text of the selected item. As I cannot use a if-else statement, any help would be great.
function displayValue(v){
var num=['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'], c, n, i=0;
while(n=num[i++]){
v==n?c=i-1:null;
}
alert(c);
}
As I cannot use a if-else statement
Why not? It is required, there is no other way, as the CPU does not know the English language
Your function works can you somewhat elaborate on how it is converting the written string to its perspective numerical value.
Thanks for your help
It is not a conversion, it is an univoque correspondence between an element in an array and his index in that array. In JavaScript the counting index of the array elements starts from 0, thus I use this fortuitous chance to make the human logical correspondence. "Zero" is the first element, thus it has the index 0, "One" is the second, thus it has the index 1
Bookmarks