www.webdeveloper.com
Results 1 to 5 of 5

Thread: Re: Converting a physical string to a integer

  1. #1
    Join Date
    Oct 2011
    Posts
    6

    Re: Converting a physical string to a integer

    Hi all,

    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.

    HTML Code:
    <select id="std_num" onchange="displayValue()">
                	<option value="Two">Two</option>
                    <option value="Zero">Zero</option>
                    <option value="Five">Five</option>
                    <option value="Three">Three</option>
                    <option value="Two">Two</option>
                    <option value="Zero">Zero</option>
                 </select>
    HTML Code:
    function displayValue()
    {
    	alert(document.getElementById('std_num').value);
    	var c = (document.getElementById('std_num').value);
    }

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    1. Why don't you write straight the numbers as values?
    2. Otherwise, use a loop:
    Code:
    <select id="std_num" onchange="displayValue(this.value)">
    Code:
    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

  3. #3
    Join Date
    Oct 2011
    Posts
    6
    Hey Kor,

    Your function works can you somewhat elaborate on how it is converting the written string to its perspective numerical value.

    Thanks for your help

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by putha View Post
    Hey Kor,

    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

    Zero => 0
    One =>1

    and so on.
    Last edited by Kor; 10-13-2011 at 09:44 AM.

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    You could also use an object as a lookup table:
    Code:
    <select id="std_num" onchange="alert( wordToDigit( this.value ) )">
      <option value="">Select</option>
      <option value="Two">Two</option>
      <option value="Zero">Zero</option>
      <option value="Five">Five</option>
      <option value="Three">Three</option>
      <option value="Four">Four</option>
      <option value="Seven">Seven</option>
    </select>
    
    <script type='text/javascript'>
    
    function wordToDigit( v )
    { 
     return { 'Zero':0,'One':1,'Two':2,'Three':3,'Four':4,'Five':5,'Six':6,'Seven':7,'Eight':8,'Nine':9 }[ v ];
    }
    
    </script>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles