patternjavascriptModerate
Arabic to Greek numeral converter, 1 - 10
Viewed 0 times
arabicgreekconverternumeral
Problem
One of the things that's straight-forward in JavaScript is Unicode support. If you want to return
I want to build a translator between Arabic and Greek numerals. Since my projects tend to take forever to complete, I decided to post what I have instead of waiting till it's finished. The code currently supports 1 - 10 and translates them to the Unicode Greek equivalent.
I wrote it the most straight-forward way I could think of. It's not necessarily the best way.
Usage:
Output:
Run it at repl.it
I'm aware
I'm especially looking for a review on the usual suspects: naming, idiomatic style and modularity. All other advice is welcome as well.
ϝ (lowercase digamma, not a simple f), you simply tell it to do so. No look-up, conversion or unreadable Unicode codes required.I want to build a translator between Arabic and Greek numerals. Since my projects tend to take forever to complete, I decided to post what I have instead of waiting till it's finished. The code currently supports 1 - 10 and translates them to the Unicode Greek equivalent.
I wrote it the most straight-forward way I could think of. It's not necessarily the best way.
function arabicToGreek(input) {
'use strict';
switch (input) {
case 1:
return "α";
case 2:
return "β";
case 3:
return "γ";
case 4:
return "δ";
case 5:
return "ε";
case 6:
return "ϝ";
case 7:
return "ζ";
case 8:
return "η";
case 9:
return "θ";
case 10:
return "ι";
}
return "";
}Usage:
arabicToGreek(4);
arabicToGreek(10);Output:
δ
ιRun it at repl.it
I'm aware
ϝ (6) should possibly be written as ϛ. I'm also aware I may or may not need to include diacritics.I'm especially looking for a review on the usual suspects: naming, idiomatic style and modularity. All other advice is welcome as well.
Solution
Reviewing your current structure: your
It's better to use an object here, as later you can extend it without having to spend pointless LoCs on
Then, add
This also means you can store the greek characters and potentially other languages in an external JSON document.
return ""; could be improved: it should / could be inside the switch statement as a default case.It's better to use an object here, as later you can extend it without having to spend pointless LoCs on
cases and breaking.function arabicToGreek(input) {
'use strict';
var greek = {
1: "α",
2: "β",
3: "γ",
4: "δ",
5: "ε",
6: "ϝ",
7: "ζ",
8: "η",
9: "θ",
10: "ι"
};
//
}Then, add
greek.hasOwnProperty to check that your string is a valid object property, in which case that it isn't, you can throw an error.if (!greek.hasOwnProperty(input)){
throw new Error("input not a valid integer representing a character");
}This also means you can store the greek characters and potentially other languages in an external JSON document.
Code Snippets
function arabicToGreek(input) {
'use strict';
var greek = {
1: "α",
2: "β",
3: "γ",
4: "δ",
5: "ε",
6: "ϝ",
7: "ζ",
8: "η",
9: "θ",
10: "ι"
};
//
}if (!greek.hasOwnProperty(input)){
throw new Error("input not a valid integer representing a character");
}Context
StackExchange Code Review Q#116875, answer score: 13
Revisions (0)
No revisions yet.