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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to replace an letters?

    Hi all!!!!
    By the title I would replace the letters, but without use the method replace
    Insert in a VAR even = ABCD, and in an other VAR odd = EFGH; and now I have to replace these letters with an number specific!!! Ex:
    Value Convert
    A 1
    B 4
    C 12
    D 6
    E 8
    F 17
    G 9
    H 2

    Now I have used an Array, but don't work, because after I had write the Array, I don't say how to do!!
    Code:
    (This is example that I have used, but it's a Fail!)
    var myWord = ["1","4","12",.....];
    for (myWord = 0; myWord < value.length; i++)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Something like this, perhaps?

    Code:
    <script type = "text/javascript">
    
    var letters =["A","B","C", "D", "E", "F", "G", "H"];
    var numbers = [ "1", "4", "12", "6", "8", "17", "9", "2"]
    var result = [];
    
    var word = "HABFACDEFGH";
    var wordsplit = word.split("");
    
    for (var i=0; i < wordsplit.length; i++) {
    for (var j = 0; j <letters.length; j++) {
    if (wordsplit[i] == letters[j]) {
    result[i] = numbers[j];
    }
    }
    }
    
    var output = result.join (" ");
    alert (output);
    
    </script>

    “Sex is one of the most wholesome, beautiful and natural experiences that money can buy.” - Steve Martin

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Something like this, perhaps?

    Code:
    <script type = "text/javascript">
    
    var letters =["A","B","C", "D", "E", "F", "G", "H"];
    var numbers = [ "1", "4", "12", "6", "8", "17", "9", "2"]
    var result = [];
    
    var word = "HABFACDEFGH";
    var wordsplit = word.split("");
    
    for (var i=0; i < wordsplit.length; i++) {
    for (var j = 0; j <letters.length; j++) {
    if (wordsplit[i] == letters[j]) {
    result[i] = numbers[j];
    }
    }
    }
    
    var output = result.join (" ");
    alert (output);
    
    </script>

    “Sex is one of the most wholesome, beautiful and natural experiences that money can buy.” - Steve Martin
    Yes yes, like this, have only one problem, that the value is in String, and after i need to add all number, so i used parseInt, to come back all value in number!! But yes, my Idea is this

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by triko View Post
    Yes yes, like this, have only one problem, that the value is in String, and after i need to add all number, so i used parseInt, to come back all value in number!! But yes, my Idea is this
    Don't use parseint() - use Number()

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Don't use parseint() - use Number()
    Philip have a problem!!!! Please see:
    Code:
      var myLetters = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
      var numbersOdd = ["1","0","5","7","9","13","15","17","19","21","1","0","5","7","9","13","15","17","19","21","2","4","18","20","11","3","6","8","12","14","16","10","22","25","24","23"]
      var numbersEven = ["0","1","2","3","4","5","6","7","8","9","0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
      var result = [];
      var wordOdd;
    //txTotal, is the function that contenent my string of 15 (word and number ex: brtnce43r0e890s)
      wordOdd = txTotal.charAt(0) + txTotal.charAt(2) + txTotal.charAt(4) + txTotal.charAt(6) + txTotal.charAt(8) + txTotal.charAt(10) + txTotal.charAt(12) + txTotal.charAt(14);
      var wordOddSplit = wordOdd.split("");
      for (i = 0; i < wordOddSplit.length; i++) // Yhe debugger sign hre the error!!! Why??? 
      {
      for (k = 0; k < myLetters.length; k++)
      {
      if (wordOddSplit[i] == myLetters[k])
      {
      result[i] = numbersOdd[k];
      }
      }
      }
      var output = result.join (" ");
      alert (output);
    Last edited by triko; 11-20-2012 at 11:12 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    var wordOdd;

    You cannot split an empty string!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    var wordOdd;

    You cannot split an empty string!
    O yes yes
    After split, it's possibile add all value that I have, and insert into other variable?

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by triko View Post
    O yes yes
    After split, it's possibile add all value that I have, and insert into other variable?
    Not a clue what you mean.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Not a clue what you mean.
    Yes sorry!
    I have the final var resuls = 1 13 7 5 9 10 4,
    Now it's possible add al value? 1+13+7+5+9+10+4 and obtain the result? = 49, in other var?

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by triko View Post
    Yes sorry!
    I have the final var resuls = 1 13 7 5 9 10 4,
    Now it's possible add al value? 1+13+7+5+9+10+4 and obtain the result? = 49, in other var?
    Yes, of course it is possible. But I think you should make an attempt to do this yourself. It is very easy. It can be done in one line of code.

    You need to convert each element of the result array to a number (it is a string at present because you have placed your numbers in quotes thus "13") and add them all together.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Yes, of course it is possible. But I think you should make an attempt to do this yourself. It is very easy. It can be done in one line of code.

    You need to convert each element of the result array to a number (it is a string at present because you have placed your numbers in quotes thus "13") and add them all together.

    Now don't work the first part... O god, that disaster!!!
    Test it, with a var name = "aaabbb93r02e970", the result return is 1 1 1 0 0 0 21 7 11 1 5 9 21 17 1. At me return only 5 number random!
    Code:
    function calculatePinControl (txTotal)
                { 
                var myLetters = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
                var numbersOdd = ["1","0","5","7","9","13","15","17","19","21","1","0","5","7","9","13","15","17","19","21","2","4","18","20","11","3","6","8","12","14","16","10","22","25","24","23"]
                var numbersEven = ["0","1","2","3","4","5","6","7","8","9","0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
                var result = [];
                var wordOdd = "";
                //txTotal, is the function that contenent my string of 15 (word and number ex: brtnce43r0e890s)
                wordOdd = txTotal.charAt(0) + txTotal.charAt(2) + txTotal.charAt(4) + txTotal.charAt(6) + txTotal.charAt(8) + txTotal.charAt(10) + txTotal.charAt(12) + txTotal.charAt(14);
                var wordOddSplit = wordOdd.split("");
                for (i = 0; i < wordOddSplit.length; i++)
                {
                    for (k = 0; k < myLetters.length; k++)
                    {
                        if (wordOddSplit[i] == myLetters[k])
                        {
                            result[i] = numbersOdd[k];
                        }
                    }
                }
                var output = result.join (" ");
                alert (output);
                }
    Sorry I read and test again and I'm understanding error!!!
    Now I find a solution for convert the split in number
    Last edited by triko; 11-21-2012 at 10:33 PM.

  • #12
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Yes, of course it is possible. But I think you should make an attempt to do this yourself. It is very easy. It can be done in one line of code.

    You need to convert each element of the result array to a number (it is a string at present because you have placed your numbers in quotes thus "13") and add them all together.
    I don't find a solution! I test this!!!
    Code:
                var letters = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
                var numbers/*Odd*/ = ["1","0","5","7","9","13","15","17","19","21","1","0","5","7","9","13","15","17","19","21","2","4","18","20","11","3","6","8","12","14","16","10","22","25","24","23"]
                var numbersEven = ["0","1","2","3","4","5","6","7","8","9","0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
                var result = [];
                var wordOdd = "";
                //txTotal, is the function that contenent my string of 15 (word and number ex: brtnce43r0e890s)
                var word = "btc9r290";
                var wordsplit = word.split("");
                for (var i=0; i < wordsplit.length; i++)
                {
                    for (var j = 0; j <letters.length; j++)
                    {
                        if (wordsplit[i] == letters[j])
                        {
                            result[i] = numbers[j];
                        }
                    }
                }
                var output = result.join (" ");// var output = result;
                var out = output.parseInt(output);//var out = output.parseInt();//var out = output.number(result);//var out = output.number(output);// var out = number(output);// (What am I doing) xD
    I don't have idea for oyu line easy easy!!!
                var ciao;
                alert (output);
                return output;
                }

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    var total = 0;
    
    for (var j = 0; j <letters.length; j++)   {
    if (wordsplit[i] == letters[j])  {
    result[i] = numbers[j];
    total += (number[j]*1);  // make number[j] a number
    }
    }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    var total = 0;
    
    for (var j = 0; j <letters.length; j++)   {
    if (wordsplit[i] == letters[j])  {
    result[i] = numbers[j];
    total += (number[j]*1);  // make number[j] a number
    }
    }
    Is this your solution add at my problem but the result that return it's the same string and don't number added themselves
    Code:
                function calculatePinControl (txTotal)
                { 
                var letters = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
                var numbers= ["1","0","5","7","9","13","15","17","19","21","1","0","5","7","9","13","15","17","19","21","2","4","18","20","11","3","6","8","12","14","16","10","22","25","24","23"]
                var result = [];
                var wordOdd = "";
                //txTotal, is the function that contenent my string of 15 (word and number ex: brtnce43r0e890s)
                word = "rsslrt93r02e970"
                var wordsplit = word.split("");
                var total = 0;
                for (var i=0; i < wordsplit.length; i++)
                {
                    for (var j = 0; j <letters.length; j++)
                    {
                        if (wordsplit[i] == letters[j])
                        {
                            result[i] = numbers[j];
                            total += (numbers[j]*1);
                        }
                    }
                }
                var output = result.join (" ");
                alert (output);
                return output;
                }

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by triko View Post
    Is this your solution add at my problem but the result that return it's the same string and don't number added themselves
    Well, it adds them up for me, but you do not display the total anywhere.

    alert (output);
    alert (total); // 148

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    triko (11-23-2012)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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