インドの通貨番号をパイサをサポートする単語に変換するJavaScript関数
最大2桁の精度の浮動小数点数を受け取るJavaScript関数を作成する必要があります。
関数は、その数値をインドの現在のテキストに変換する必要があります。
例-
入力番号が-
の場合const num = 12500
その場合、出力は-
になります。const output = 'Twelve Thousand Five Hundred';
例
以下はコードです-
const num = 12500;
const wordify = (num) => {
const single = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];
const double = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
const tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
const formatTenth = (digit, prev) => {
return 0 == digit ? "" : " " + (1 == digit ? double[prev] : tens[digit])
};
const formatOther = (digit, next, denom) => {
return (0 != digit && 1 != next ? " " + single[digit] : "") + (0 != next || digit > 0 ? " " + denom : "")
};
let res = "";
let index = 0;
let digit = 0;
let next = 0;
let words = [];
if (num += "", isNaN(parseInt(num))){
res = "";
}
else if (parseInt(num) > 0 && num.length <= 10) {
for (index = num.length - 1; index >= 0; index--) switch (digit = num[index] - 0, next = index > 0 ? num[index - 1] - 0 : 0, num.length - index - 1) {
case 0:
words.push(formatOther(digit, next, ""));
break;
case 1:
words.push(formatTenth(digit, num[index + 1]));
break;
case 2:
words.push(0 != digit ? " " + single[digit] + " Hundred" + (0 != num[index + 1] && 0 != num[index + 2] ? " and" : "") : "");
break;
case 3:
words.push(formatOther(digit, next, "Thousand"));
break;
case 4:
words.push(formatTenth(digit, num[index + 1]));
break;
case 5:
words.push(formatOther(digit, next, "Lakh"));
break;
case 6:
words.push(formatTenth(digit, num[index + 1]));
break;
case 7:
words.push(formatOther(digit, next, "Crore"));
break;
case 8:
words.push(formatTenth(digit, num[index + 1]));
break;
case 9:
words.push(0 != digit ? " " + single[digit] + " Hundred" + (0 != num[index + 1] || 0 != num[index + 2] ? " and" : " Crore") : "")
};
res = words.reverse().join("")
} else res = "";
return res
};
console.log(wordify(num)); 出力
以下はコンソールでの出力です-
Twelve Thousand Five Hundred
-
JavaScriptで数値が含まれている文字列を検証する
問題 文字列strを受け取るJavaScript関数を作成する必要があります。この関数は、文字列内のアルファベットを、その前の数字に基づいて検証する必要があります。 文字列を数字で分割してから、数字を次の部分文字列の文字数と比較する必要があります。それらがすべて一致する場合、文字列は有効であり、trueを返す必要があり、そうでない場合はfalseを返す必要があります。 例- 5hello4from2me trueを返す必要があります 数字で割ると、文字列は「hello」、「from」、「me」になり、これらの文字列はすべて、前の数字と同じ長さになるためです 例 以下はコードです-
-
数字を単語に変換するCプログラム
数字dがあるとすると、それを単語に変換する必要があります。したがって、d =5の場合、出力は「5」になります。 0と9の範囲を超えるdを指定すると、適切な出力が返されます。 したがって、入力がd =6の場合、出力は「6」になります。 これを解決するには、次の手順に従います- 関数solve()を定義します。これにはdが必要です。 d9の場合、次のようになります。 return( 0〜9の範囲を超えています) それ以外の場合、dが0と同じ場合、次のようになります。 return( Zero) それ以外の場合、dが1と同じ場合、次のようになります。 return( One)