So I have a string that I want to draw onto a HTML5 Canvas using javascript. I have got separate functions that will draw each character of the alphabet.
I have used a for loop to iterate through the characters in the string.
let theText = "String"
for (let i = 0; i < theText.length; i++) {
theText[i];
}
I also have functions for each character in the alphabet
function draw_A() {
//code that draws A
}
... a through to x
function draw_X() {
//code that draws X
}
Currently my solution to drawing the characters is
for (let i = 0; i < theText.length; i++) {
c = theText[i];
if (c == "A") {
draw_A();
}
... a through to x
else if (c == "X") {
draw_X();
}
}
I know this method is inefficient and I have also tried using case, however, that is just a simplified version of what I'm doing already.
I know that if you want to insert a variable into a string in javascript you can use the ${} characters inside of `` to do this. The below code will insert the variable 'username' into a string.
let myString = `Hello ${username}, welcome to this script`;
Is there a similar way to do this with functions. I don't want to have to write 26 if or case statements to carry out something that I know should be much simplier