Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda