Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A loop to create the alphabet using JavaScript [duplicate]

I've been working on a small project for myself, and it consists of creating the alphabet. I don't want to hard code each individual letter in markup, but rather use JavaScript to do it for me.

This is how far I've gotten.

for ( i = 0; i < 26; i++ ) {



var li = document.createElement("li");
li.innerHTML = "letter" + i + " ";
li.style.listStyle = "none";
li.style.display = "inline";
document.getElementById("letter-main").appendChild(li);

}

That being said, I'm trying to avoid using jQuery for the time, as I am trying to gain a better understanding of JavaScript.

There's another post that goes over the same Idea, using character codes but with jQuery.

How would I go about this?

like image 786
John Connor Avatar asked Jun 15 '17 17:06

John Connor


People also ask

How do you write a for loop loop in JavaScript?

for/in - loops through the properties of an object. for/of - loops through the values of an iterable object. while - loops through a block of code while a specified condition is true. do/while - also loops through a block of code while a specified condition is true.

Which loop is used for repeating a part of JavaScript?

while Loop, and More. Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false . A loop will continue running until the defined condition returns false .

How do I print the next alphabet in JavaScript?

To get the next letter of the alphabet in JavaScript, we can use the String. fromCharCode static string method and the charCodeAt string instance method.


1 Answers

Answer from Convert integer into its character equivalent in Javascript:

Assuming you want lower case letters:

var chr = String.fromCharCode(97 + n); // where n is 0, 1, 2 ...

97 is the ASCII code for lower case 'a'. If you want uppercase letters, replace 97 with 65 (uppercase 'A'). Note that if n > 25, you will get out of the range of letters.

like image 67
Naman Avatar answered Sep 26 '22 23:09

Naman