PythJS/Examples
Jump to navigation
Jump to search
Examples
A more complex example, a JavaScript function that converts numbers to words
JavaScript:
function toWords(number, language) {
if (language == "English") {
// From 1 to 12 and zero!
if (number == 0) return("zero");
if (number == 1) return("one");
if (number == 2) return("two");
if (number == 3) return("three");
if (number == 4) return("four");
if (number == 5) return("five");
if (number == 6) return("six");
if (number == 7) return("seven");
if (number == 8) return("eight");
if (number == 9) return("nine");
if (number == 10) return("ten");
if (number == 11) return("eleven");
if (number == 12) return("twelve");
// From 13 to 19
if (number >= 13 & number < 20) {
let ten = number - 10;
var word;
if (ten == 3) word = "thir";
if (ten == 4) word = "four";
if (ten == 5) word = "fif";
if (ten == 6) word = "six";
if (ten == 7) word = "seven";
if (ten == 8) word = "eigh";
if (ten == 9) word = "nine";
return(word + "teen");
}
// From 20 to 99
if (number >= 20 & number < 100) {
let math = Math.floor(number / 10) * 10;
let math2 = number - math;
var word2;
var word3;
if (math == 20) word2 = "twen"
if (math == 30) word2 = "thir";
if (math == 40) word2 = "for";
if (math == 50) word2 = "fif";
if (math == 60) word2 = "six";
if (math == 70) word2 = "seven";
if (math == 80) word2 = "eigh";
if (math == 90) word2 = "nine";
if (math2 == 1) word3 = "-one";
if (math2 == 2) word3 = "-two";
if (math2 == 3) word3 = "-three";
if (math2 == 4) word3 = "-four";
if (math2 == 5) word3 = "-five";
if (math2 == 6) word3 = "-six";
if (math2 == 7) word3 = "-seven";
if (math2 == 8) word3 = "-eight";
if (math2 == 9) word3 = "-nine";
if (math2 != 0) return(word2 + "ty" + word3);
else return(word2 + "ty");
}
// From 100 to 999 and 1000
if (number >= 100 & number <= 999) {
let math3 = Math.floor(number / 100) * 100;
let math4 = number - math3;
let calc = Math.floor(math4 / 10) * 10;
let calc2 = math4 - calc;
var hund;
var word4;
var word5;
var ten1;
var digit2;
if (math3 == 100) hund = "one";
if (math3 == 200) hund = "two";
if (math3 == 300) hund = "three";
if (math3 == 400) hund = "four";
if (math3 == 500) hund = "five";
if (math3 == 600) hund = "six";
if (math3 == 700) hund = "seven";
if (math3 == 800) hund = "eight";
if (math3 == 900) hund = "nine";
if (calc == 20) word4 = "twen";
if (calc == 30) word4 = "thir";
if (calc == 40) word4 = "for";
if (calc == 50) word4 = "fif";
if (calc == 60) word4 = "six";
if (calc == 70) word4 = "seven";
if (calc == 80) word4 = "eigh";
if (calc == 90) word4 = "nine";
if (calc2 == 1) {
word5 = "-one";
digit2 = "one";
}
if (calc2 == 2) {
word5 = "-two";
digit2 = "two";
}
if (calc2 == 3) {
word5 = "-three";
digit2 = "three";
}
if (calc2 == 4) {
word5 = "-four";
digit2 = "four";
}
if (calc2 == 5) {
word5 = "-five";
digit2 = "five";
}
if (calc2 == 6) {
word5 = "-six";
digit2 = "six";
}
if (calc2 == 7) {
word5 = "-seven";
digit2 = "seven";
}
if (calc2 == 8) {
word5 = "-eight";
digit2 = "eight";
}
if (calc2 == 9) {
word5 = "-nine";
digit2 = "nine";
}
if (calc >= 20 & calc <= 99) {
if (calc2 != 0) return(hund + " " + "hundred " + word4 + "ty" + word5);
else return(hund + " " + "hundred " + word4 + "ty");
}
if (calc == 0 & calc2 == 0) return(hund + " " + "hundred");
if (calc == 0 & calc2 != 0) return(hund + " " + "hundred " + digit2);
if (calc == 10 & calc2 == 0) return(hund + " " + "hundred " + "ten");
if (calc == 10 & calc2 == 1) return(hund + " " + "hundred " + "eleven");
if (calc == 10 & calc2 == 2) return(hund + " " + "hundred " + "twelve");
if (calc == 10 & calc2 > 2 & calc2 <= 9) {
if (calc2 == 3) ten1 = "thir";
if (calc2 == 4) ten1 = "four";
if (calc2 == 5) ten1 = "fif";
if (calc2 == 6) ten1 = "six";
if (calc2 == 7) ten1 = "seven";
if (calc2 == 8) ten1 = "eigh";
if (calc2 == 9) ten1 = "nine";
return(hund + " " + "hundred " + ten1 + "teen");
}
}
if (number == 1000) return("one thousand");
}
}
In PythJS:
function toWords(number, language):
if (language == "English"):
// From 1 to 12 and zero!
if (number == 0) return("zero")
if (number == 1) return("one")
if (number == 2) return("two")
if (number == 3) return("three")
if (number == 4) return("four")
if (number == 5) return("five")
if (number == 6) return("six")
if (number == 7) return("seven")
if (number == 8) return("eight")
if (number == 9) return("nine")
if (number == 10) return("ten")
if (number == 11) return("eleven")
if (number == 12) return("twelve")
// From 13 to 19
if (number >= 13 & number < 20):
let ten = number - 10
var word
if (ten == 3) word = "thir"
if (ten == 4) word = "four"
if (ten == 5) word = "fif"
if (ten == 6) word = "six"
if (ten == 7) word = "seven"
if (ten == 8) word = "eigh"
if (ten == 9) word = "nine"
return(word + "teen")
// From 20 to 99
if (number >= 20 & number < 100):
let math = Math.floor(number / 10) * 10
let math2 = number - math
var word2
var word3
if (math == 20) word2 = "twen"
if (math == 30) word2 = "thir"
if (math == 40) word2 = "for"
if (math == 50) word2 = "fif"
if (math == 60) word2 = "six"
if (math == 70) word2 = "seven"
if (math == 80) word2 = "eigh"
if (math == 90) word2 = "nine"
if (math2 == 1) word3 = "-one"
if (math2 == 2) word3 = "-two"
if (math2 == 3) word3 = "-three"
if (math2 == 4) word3 = "-four"
if (math2 == 5) word3 = "-five"
if (math2 == 6) word3 = "-six"
if (math2 == 7) word3 = "-seven"
if (math2 == 8) word3 = "-eight"
if (math2 == 9) word3 = "-nine"
if (math2 != 0) return(word2 + "ty" + word3)
else return(word2 + "ty")
// From 100 to 999 and 1000
if (number >= 100 & number <= 999):
let math3 = Math.floor(number / 100) * 100
let math4 = number - math3
let calc = Math.floor(math4 / 10) * 10
let calc2 = math4 - calc
var hund
var word4
var word5
var ten1
var digit2
if (math3 == 100) hund = "one"
if (math3 == 200) hund = "two"
if (math3 == 300) hund = "three"
if (math3 == 400) hund = "four"
if (math3 == 500) hund = "five"
if (math3 == 600) hund = "six"
if (math3 == 700) hund = "seven"
if (math3 == 800) hund = "eight"
if (math3 == 900) hund = "nine"
if (calc == 20) word4 = "twen"
if (calc == 30) word4 = "thir"
if (calc == 40) word4 = "for"
if (calc == 50) word4 = "fif"
if (calc == 60) word4 = "six"
if (calc == 70) word4 = "seven"
if (calc == 80) word4 = "eigh"
if (calc == 90) word4 = "nine"
if (calc2 == 1):
word5 = "-one"
digit2 = "one"
if (calc2 == 2):
word5 = "-two"
digit2 = "two"
if (calc2 == 3):
word5 = "-three"
digit2 = "three"
if (calc2 == 4):
word5 = "-four"
digit2 = "four"
if (calc2 == 5):
word5 = "-five"
digit2 = "five"
if (calc2 == 6):
word5 = "-six"
digit2 = "six"
if (calc2 == 7):
word5 = "-seven"
digit2 = "seven"
if (calc2 == 8):
word5 = "-eight"
digit2 = "eight"
if (calc2 == 9):
word5 = "-nine"
digit2 = "nine"
if (calc >= 20 & calc <= 99):
if (calc2 != 0) return(hund + " " + "hundred " + word4 + "ty" + word5)
else return(hund + " " + "hundred " + word4 + "ty")
if (calc == 0 & calc2 == 0) return(hund + " " + "hundred")
if (calc == 0 & calc2 != 0) return(hund + " " + "hundred " + digit2)
if (calc == 10 & calc2 == 0) return(hund + " " + "hundred " + "ten")
if (calc == 10 & calc2 == 1) return(hund + " " + "hundred " + "eleven")
if (calc == 10 & calc2 == 2) return(hund + " " + "hundred " + "twelve")
if (calc == 10 & calc2 > 2 & calc2 <= 9):
if (calc2 == 3) ten1 = "thir"
if (calc2 == 4) ten1 = "four"
if (calc2 == 5) ten1 = "fif"
if (calc2 == 6) ten1 = "six"
if (calc2 == 7) ten1 = "seven"
if (calc2 == 8) ten1 = "eigh"
if (calc2 == 9) ten1 = "nine"
return(hund + " " + "hundred " + ten1 + "teen")
if (number == 1000) return("one thousand")