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?
Aucun commentaire:
Enregistrer un commentaire