Untitled
سوال 3unknown
javascript
2 years ago
2.7 kB
1
Indexable
Never
// Define an array of Persian digits const persianDigits = [ 'صفر', 'یک', 'دو', 'سه', 'چهار', 'پنج', 'شش', 'هفت', 'هشت', 'نه', ]; // Define an array of Persian tens const persianTens = [ 'ده', 'بیست', 'سی', 'چهل', 'پنجاه', 'شصت', 'هفتاد', 'هشتاد', 'نود', ]; // Define an array of Persian hundreds const persianHundreds = [ 'صد', 'دویست', 'سیصد', 'چهارصد', 'پانصد', 'ششصد', 'هفتصد', 'هشتصد', 'نهصد', ]; // Define a function to convert a single digit to Persian function digitToPersian(digit) { return persianDigits[digit]; } // Define a function to convert a two-digit number to Persian function twoDigitsToPersian(number) { // If the number is less than 10, use the digitToPersian function if (number < 10) { return digitToPersian(number); } // If the number is a multiple of 10, use the persianTens array else if (number % 10 == 0) { return persianTens[number / 10 - 1]; } // Otherwise, use the persianTens and digitToPersian functions with a و in between else { let ten = Math.floor(number / 10); let one = number % 10; return persianTens[ten - 1] + ' و ' + digitToPersian(one); } } // Define a function to convert a three-digit number to Persian function threeDigitsToPersian(number) { // If the number is less than 100, use the twoDigitsToPersian function if (number < 100) { return twoDigitsToPersian(number); } // If the number is a multiple of 100, use the persianHundreds array else if (number % 100 == 0) { return persianHundreds[number / 100 - 1]; } // Otherwise, use the persianHundreds and twoDigitsToPersia functions with a و in between else { let hundred = Math.floor(number / 100); let rest = number % 100; return persianHundreds[hundred - 1] + ' و ' + twoDigitsToPersian(rest); } } // Define a function to convert any four-digit number to Persian function fourDigitsToPersia(number) { // If the number is less than or equal to zero or greater than or equal to ten thousand ,return an error message if (number <= 0 || number >= 10000) { return 'Invalid input. Please enter a positive four-digit number.'; } // If the number is less than one thousand ,use the threeDigitsToPersia function else if (number < 1000) { return threeDigitsToPersian(number); } // If the number is equal to one thousand ,return هزار else if (number == 1000) { return ' هزار '; } } console.log(fourDigitsToPersia(100));