In a javascript for loop, I need to substitute the underscore bounded a with Vowels[i] and the quote bounded ā with MacronVowels[i] in:
flosType_a_withMacron()
putMacronedLetter("ā")
I am trying to condense a series of five functions with vowels a,b,c,d,e and ā,ē,ī,ō,ū by creating 2 arrays (see code) and replacing (in the for loop) the function with aforesaid array elements. Obviously this will only save me a few lines of code but I relatively new to coding and just want to know how to do this for learning purposes.
Here are the five functions as they current exist and work correctly. What I want to do in wrap them into one in a for loop:
EXISTING CODE:
function type_a_withMacron() {
putMacronedLetter("ā");
moveCursorToEnd(element);
}
function type_e_withMacron() {
putMacronedLetter("ē");
moveCursorToEnd(element);
}
function type_i_withMacron() {
putMacronedLetter("ī");
moveCursorToEnd(element);
}
function type_o_withMacron() {
putMacronedLetter("ō");
moveCursorToEnd(element);
}
function type_u_withMacron() {
putMacronedLetter("ū");
moveCursorToEnd(element);
}
PROPOSED CODE: (what is proper formatting here, this is obv wrong, just asking)
const flos_Vowels = ["a","b","c","d","e"];
const flos_MacronVowels = ["ā","ē","ī","ō","ū"];
for(let i = 0;i < 5;i++ )
{
function flosType_flos_[Vowels[i]]_withMacron()
{
putMacronedLetter("[flos_MacronVowels[i]]");
moveCursorToEnd(element);
}
}