javascript replace letters from array 1 with number from array 2 and calculate the sum of these numbers

Viewed 119

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

7 Answers

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

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.

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}`)

Here is one way to replace the letters with numbers and output the total.

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("").map(function(ele) {
    return ele.replace(ele, replace[search.indexOf(ele)]);
  }).reduce((acc, next)=> acc+parseInt(next), 0)
}
console.log(w);

I think you want to get abjad numbers. so if you want to get 92, you can use blow code.

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 {
  const chars = word.split('')
  var sum = 0
  chars.forEach(x => {
    const index = search.indexOf(x)
    sum += +replace[index]
  })
}
console.log(sum);

mine...

const 
  val = 
    [ 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 ]
, ref = "ابجدهوزحطيكلمنسعفصقرشتثخذظضغ"
  ;
function count (word)
  {
  c = 0
  for(let l of word) c += val[ref.indexOf(l)]
  return c
  }
  
console.log( count( "محمد") ) // 92

In case you want a reusable solution:

const word = 'محمد';
const search = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];
const 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'];;

const sumOfChars = (chars, replacements) => stringValue => {
  return stringValue.split('').reduce(
    (sum, char) => {
      const match = chars.indexOf(char);
      return match > -1 ? sum + Number(replacements[match]) : sum;
    },
    0
  );
}

console.log('charSumOfValues:', sumOfChars(search, replace)(word));

Or in two distinct functions:

const word = 'محمد';
const search = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];
const 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'];;


// --- distinct utilities
const charValues = (chars, replacements) => stringValue => {
  return stringValue.split('').map(char => {
    const match = chars.indexOf(char);
    return match > -1 ? Number(replacements[match]) : 0;
  });
};

const sum = (chars) => chars.reduce((a, b) => a + b, 0);


const getCVs = charValues(search, replace);

console.log('values:', getCVs(word));
console.log('sum:', sum(getCVs(word)));

Related