I have created a function which converts numbers into text format and successfully converted up to 6 digit numbers. However, I want to know if I can make any modifications to make the code shorter.
$(document).ready(function() {
var onesVal = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
var twoVal = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Ninteen'];
var tensVal = ["", "", 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninty'];
var powersVal = ['Hundred', 'Thousand', 'Lakh'];
$('input').on('input propertychange', function() {
if ($(this).val() == '') {
$('p').text('');
} else {
//var textVal = $(this).val();
var textVal = parseInt($(this).val());
if (textVal < 10) {
$('p').text(onesVal[textVal]);
} else if (textVal < 100) {
var getTwoVal = forTwoValue(textVal);
$('p').text(getTwoVal);
} else if (textVal < 1000) {
var getThreeVal = forThreeValue(textVal);
$('p').text(getThreeVal);
} else if (textVal < 10000) {
var getFourVal = forFourValue(textVal);
$('p').text(getFourVal);
} else if (textVal < 100000) {
var getFiveVal = forFiveValue(textVal);
$('p').text(getFiveVal);
} else if (textVal < 1000000) {
var getSixVal = forSixValue(textVal);
$('p').text(getSixVal);
};
};
});
function forTwoValue(value) {
value = value.toString();
if (value < 20) {
return twoVal[value[1]];
} else if (value % 10 == 0) {
return tensVal[value[0]];
} else {
return tensVal[value[0]] + ' ' + onesVal[value[1]];
};
};
function forThreeValue(value) {
value = value.toString();
if (value % 100 == 0) {
return onesVal[value[0]] + ' ' + powersVal[0];
} else {
var hundOne = Math.floor(value / 100);
var hundTwo = (value % 100).toString();
if (hundTwo < 10) {
return onesVal[hundOne] + ' ' + powersVal[0] + ' ' + onesVal[hundTwo];
} else if (hundTwo < 100) {
var getTwoVal = forTwoValue(hundTwo);
return onesVal[hundOne] + ' ' + powersVal[0] + ' ' + getTwoVal;
};
};
};
function forFourValue(value) {
value = value.toString();
if (value % 1000 == 0) {
return onesVal[value[0]] + ' ' + powersVal[1];
} else {
var thouOne = Math.floor(value / 1000);
var thouTwo = (value % 1000).toString();
if (thouTwo < 10) {
return onesVal[thouOne] + ' ' + powersVal[1] + ' ' + onesVal[thouTwo];
} else if (thouTwo < 100) {
var getTwoVal = forTwoValue(thouTwo);
return onesVal[thouOne] + ' ' + powersVal[0] + ' ' + getTwoVal;
} else if (thouTwo < 1000) {
var getThreeVal = forThreeValue(thouTwo);
return onesVal[thouOne] + ' ' + powersVal[1] + ' ' + getThreeVal;
};
};
};
function forFiveValue(value) {
value = value.toString();
var tenThouOne = Math.floor(value / 1000).toString();
var tenThouTwo = (value % 1000).toString();
var getTwoVal = forTwoValue(tenThouOne);
if (tenThouTwo == 0) {
return getTwoVal + ' ' + powersVal[1];
} else if (tenThouTwo < 10) {
return getTwoVal + ' ' + powersVal[1] + ' ' + onesVal[tenThouTwo];
} else if (tenThouTwo < 100) {
var getTwoVal2 = forTwoValue(tenThouTwo);
return getTwoVal + ' ' + powersVal[1] + ' ' + getTwoVal2;
} else if (tenThouTwo < 1000) {
var getThreeVal = forThreeValue(tenThouTwo);
return getTwoVal + ' ' + powersVal[1] + ' ' + getThreeVal;
};
};
function forSixValue(value) {
value = value.toString();
if (value % 100000 == 0) {
return onesVal[value[0]] + ' ' + powersVal[2];
} else {
var lakhOne = Math.floor(value / 100000).toString();
var lakhTwo = (value % 100000).toString();
if (lakhTwo == 0) {
return onesVal[lakhOne] + ' ' + powersVal[2];
} else if (lakhTwo < 10) {
return onesVal[lakhOne] + ' ' + powersVal[2] + ' ' + onesVal[lakhTwo];
} else if (lakhTwo < 100) {
var getTwoVal = forTwoValue(lakhTwo);
return onesVal[lakhOne] + ' ' + powersVal[2] + ' ' + getTwoVal;
} else if (lakhTwo < 1000) {
var getThreeVal = forThreeValue(lakhTwo);
return onesVal[lakhOne] + ' ' + powersVal[2] + ' ' + getThreeVal;
} else if (lakhTwo < 10000) {
var getFourVal = forFourValue(lakhTwo);
return onesVal[lakhOne] + ' ' + powersVal[2] + ' ' + getFourVal;
} else if (lakhTwo < 100000) {
var getFiveVal = forFiveValue(lakhTwo);
return onesVal[lakhOne] + ' ' + powersVal[2] + ' ' + getFiveVal;
};
};
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Enter Number : <input type="text" maxlength="6">
<p id="demo"></p>