Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 02:28 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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++)
triko is offline   Reply With Quote
Old 11-20-2012, 03:45 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-20-2012, 07:14 PM   PM User | #3
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 11-20-2012, 07:25 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-20-2012, 11:10 PM   PM User | #5
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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..
triko is offline   Reply With Quote
Old 11-21-2012, 07:32 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-21-2012, 03:21 PM   PM User | #7
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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?
triko is offline   Reply With Quote
Old 11-21-2012, 03:25 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-21-2012, 05:29 PM   PM User | #9
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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?
triko is offline   Reply With Quote
Old 11-21-2012, 05:39 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-21-2012, 06:31 PM   PM User | #11
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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..
triko is offline   Reply With Quote
Old 11-21-2012, 11:07 PM   PM User | #12
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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;
            }
triko is offline   Reply With Quote
Old 11-22-2012, 09:16 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-22-2012, 11:08 PM   PM User | #14
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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;
            }
triko is offline   Reply With Quote
Old 11-23-2012, 08:23 AM   PM User | #15
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
triko (11-23-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:25 AM.


Advertisement
Log in to turn off these ads.