Created
August 8, 2020 04:17
-
-
Save Dobby233Liu/878e2ce2dc0eb60b63a4d1b8d313c37b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateStringByTemplate(arr){ | |
// FIXME: This is a temporary solution. It may cause overflows. | |
// If you have an excellent way to do this, tell me. | |
main = "" | |
for (i of arr) { | |
z = "" | |
if (typeof i != "string") { // is i not a string? | |
if (Array.isArray(i)) | |
z = i // assign a placeholder | |
else | |
throw new TypeError("indexed element is neither a string nor an array, got a(n) " + typeof i + "instead") | |
while (Array.isArray(z)) { // if z is (still) a array, index to the end using a while loop (FIXME) | |
z = z[Math.floor(Math.random() * z.length)] // get another (replacement) element | |
if (typeof z != "string" && !Array.isArray(z)) // do it have a vaild type? | |
throw new TypeError("randomly selected element in array z is neither a string nor an array, got a(n) " + typeof i + "instead") | |
} | |
} | |
else | |
z = i // just assign it to z | |
main += z | |
} | |
return main | |
} |
Author
Dobby233Liu
commented
Aug 8, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment