How to minimize these if statements?

Viewed 95

Arabic letters have numbers assigned to them. I am trying to calculate the number for a given word as below. The number for each letter is added to calculate the number for the word. I am checking each letter of the word by if statements, i. e., if the letter from the word is equal to the given letter it pushes its value to the array. In the end I add all the values of the array to give the number for the whole word.

var x = 'افتخار';

y = [];


for (let i = 0; i < x.length; i++) {

    if (x[i] == ' ') {
        y.push(0);
    }
    if (x[i] == 'ا' || x[i] == 'آ' || x[i] == 'ء') {
        y.push(1);
    }
    if (x[i] == 'ب' || x[i] == 'پ') {
        y.push(2);
    }
    if (x[i] == 'ج' || x[i] == 'چ') {
        y.push(3);
    }
    if (x[i] == 'د' || x[i] == 'ڈ') {
        y.push(4);
    }
    if (x[i] == 'ه' || x[i] == 'ھ') {
        y.push(5);
    }
    if (x[i] == 'و') {
        y.push(6);
    }
    if (x[i] == 'ز' || x[i] == 'ژ') {
        y.push(7);
    }
    if (x[i] == 'ح') {
        y.push(8);
    }
    if (x[i] == 'ط') {
        y.push(9);
    }
    if (x[i] == 'ی') {
        y.push(10);
    }
    if (x[i] == 'ک') {
        y.push(20);
    }
    if (x[i] == 'ل') {
        y.push(30);
    }
    if (x[i] == 'م') {
        y.push(40);
    }
    if (x[i] == 'ن') {
        y.push(50);
    }
    if (x[i] == 'س') {
        y.push(60);
    }
    if (x[i] == 'ع') {
        y.push(70);
    }
    if (x[i] == 'ف') {
        y.push(80);
    }
    if (x[i] == 'ص') {
        y.push(90);
    }
    if (x[i] == 'ق') {
        y.push(100);
    }
    if (x[i] == 'ر') {
        y.push(200);
    }
    if (x[i] == 'ش') {
        y.push(300);
    }
    if (x[i] == 'ت' || x[i] == 'ٹ') {
        y.push(400);
    }
    if (x[i] == 'ث') {
        y.push(500);
    }
    if (x[i] == 'خ') {
        y.push(600);
    }
    if (x[i] == 'ذ') {
        y.push(700);
    }
    if (x[i] == 'ض') {
        y.push(800);
    }
    if (x[i] == 'ظ') {
        y.push(900);
    }
    if (x[i] == 'غ') {
        y.push(1000);
    }
}
console.log(y.reduce((a, b) => a + b, 0));

Is there a better way to write this minimizing the number of if statements used?

2 Answers

If you make a string formatted with the number to push followed by the characters associated with them, you can then create a Map indexed by the character whose value is the number. Then just .get from the Map.

const str = `
0  
1 اآء
2 بپ
3 جچ
`; // etc
const map = new Map();
for (const [, num, chars] of str.matchAll(/^(\d+) (.+)/gm)) {
  for (const char of [...chars]) {
    map.set(char, Number(num));
  }
}

// do the above only once, then iterate through strings when needed:

const makeArrFromStr = str => [...str].map(char => map.get(char));
makeArrFromStr('افتخار')

Using a string initially instead of an object will make things a whole lot more concise when there are lots of different characters.

For a small English example:

const str = `
0  
1 a
2 b
3 cd
`; // etc
const map = new Map();
for (const [, num, chars] of str.matchAll(/^(\d+) (.+)/gm)) {
  for (const char of [...chars]) {
    map.set(char, Number(num));
  }
}

// do the above only once, then iterate through strings when needed:

const makeArrFromStr = str => [...str].map(char => map.get(char));
console.log(makeArrFromStr('ab cd'));

whatever = {
  " ": 0,
  "ا": Number(1), // I couldn't write 1 because some Arabic black magic block me
  ...,
}

for (let i = 0; i < x.length; i++) {
    if (x[i] in whatever) {
        y.push(x[i])
    }
}
Related