...

View Full Version : How to replace an letters?



triko
11-20-2012, 03:28 PM
Hi all!!!!
By the title I would replace the letters, but without use the method replace :D
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!!


(This is example that I have used, but it's a Fail!)
var myWord = ["1","4","12",.....];
for (myWord = 0; myWord < value.length; i++)

Philip M
11-20-2012, 04:45 PM
Something like this, perhaps?


<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

triko
11-20-2012, 08:14 PM
Something like this, perhaps?


<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 :D

Philip M
11-20-2012, 08:25 PM
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 :D

Don't use parseint() - use Number()

triko
11-21-2012, 12:10 AM
Don't use parseint() - use Number()

Philip have a problem!!!! Please see:


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);

Philip M
11-21-2012, 08:32 AM
var wordOdd;

You cannot split an empty string!

triko
11-21-2012, 04:21 PM
var wordOdd;

You cannot split an empty string!

O yes yes :thumbsup:
After split, it's possibile add all value that I have, and insert into other variable?

Philip M
11-21-2012, 04:25 PM
O yes yes :thumbsup:
After split, it's possibile add all value that I have, and insert into other variable?

Not a clue what you mean. :confused:

triko
11-21-2012, 06:29 PM
Not a clue what you mean. :confused:

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? :D

Philip M
11-21-2012, 06:39 PM
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? :D

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.

triko
11-21-2012, 07:31 PM
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!


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!!! :D
Now I find a solution for convert the split in number :D

triko
11-22-2012, 12:07 AM
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!!!


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;
}

Philip M
11-22-2012, 10:16 PM
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
}
}

triko
11-23-2012, 12:08 AM
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 :D


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;
}

Philip M
11-23-2012, 09:23 AM
Is this your solution add at my problem but the result that return it's the same string and don't number added themselves :D



Well, it adds them up for me, but you do not display the total anywhere.

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

triko
11-23-2012, 01:32 PM
Ops..!!! Ok thanks Philip, now have my last question that is now posted!!!
You have helped me more and more and follow in this problem! thanks so much!!!! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum