Created
June 23, 2017 12:54
-
-
Save bastienrobert/892059e5d19d87904808f2ded57d8e94 to your computer and use it in GitHub Desktop.
Alphabet generator in Javascript
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 to generate an array w/ alphabet letters | |
function genCharArray(charA, charZ) { | |
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0); | |
for (; i <= j; ++i) { | |
a.push(String.fromCharCode(i)); | |
} | |
return a; | |
} | |
// Function to parse URL and get last element | |
function parseURL(url) { | |
var array = url.split('/'); | |
var lastsegment = array[array.length-1]; | |
return lastsegment; | |
} | |
// Function to set alphabet array in great HTML format | |
function layoutCharArray() { | |
var alphabet = genCharArray('a', 'z'); | |
var element = document.getElementById("alphabet"); | |
var path = "http://localhost" | |
for (i = 0; i < alphabet.length; i++) { | |
nameList = "<li>" + "<a href='" + path + alphabet[i] + "'>" + alphabet[i] + "</a>" + "</li>"; | |
element.innerHTML += nameList; | |
} | |
} | |
// Launch function if element 'alphabet' exists | |
if (document.getElementById("alphabet")){ | |
layoutCharArray(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment