Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
javascript replace letters from array 1 with number from array 2 and calculate the sum of these numbers

I have two array

array number 1:

let search  = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];

array number 2:

let replace = ['1', '2', '3', '4', '5','6', '7', '8', '9', '10','20', '30', '40', '50', '60','70', '80', '90', '100', '200','300', '400', '500', '600', '700','800', '900', '1000'];

and this is an input :

let word = "محمد";

I want in javascript search the letters of the variable word and replace them with the equivalent of number in the replace array .The replace array and the search array have the same numbers of letters and number.

The output will be:

م 40
ح 8
م 40
د 4

total = 92

let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];
if (word === "") {
  document.write('the field is empty');
} else {
  w = word.split("").filter(function(ele) {
    return ele.replace(search, replace);
  }).map(function(ele) {
    return ele + ele;
  });
}
console.log(w);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Assuming you meant the expected result was 92, you can do this by forst splitting the input, then finding the index of the item in find and aggregating the result using reduce finding the value from replace

let search  = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5','6', '7', '8', '9', '10','20', '30', '40', '50', '60','70', '80', '90', '100', '200','300', '400', '500', '600', '700','800', '900', '1000'];

let word = "محمد";

let result = word.split("")
                .map(x => search.indexOf(x))
                .reduce( (acc,i) => acc + parseInt(replace[i],10),0);
console.log(result);

You could impove this by storing integers in your replace array which would negate the need for parseInt

about 4 years ago · Juan Pablo Isaza Relatório

0

I would suggest creating a map for the two arrays:

let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];

let map = Object.fromEntries(search.map((letter, i) => [letter, replace[i]]));

let w = Array.from(word, letter => map[letter]).filter(Boolean);
console.log(w);
// If you want to have a concatenated string:
console.log(w.join(","));
// Or a sum:
console.log(w.reduce((a, b) => a + +b, 0));

Note: If your intention is to sum, or make other numerical calculations, I would suggest to make the replace array an array of numbers, not strings.

about 4 years ago · Juan Pablo Isaza Relatório

0

Logic

  • Split the word into letters
  • Find the index of each word from search.
  • Find the value of the above index from replace array.
  • Add this in a loop

Working Fiddle

let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];
let sum = 0;
word.split("").forEach(letter => {
  const index = replace[search.indexOf(letter)];
  console.log(`${index} ${letter}`)
  sum += +index;
});
console.log(`total = ${sum}`)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda