Advertisement
Guest User

Order the Names

a guest
May 26th, 2019
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let addButton = document.getElementsByTagName("button")[0];
  3.     let inputTextElement = document.getElementsByTagName("input")[0];
  4.  
  5.     let orderListElement = document.getElementsByTagName("ol")[0];
  6.     let listItemElements = orderListElement.getElementsByTagName("li");
  7.  
  8.     addButton.addEventListener("click", function () {
  9.         let inputName = inputTextElement.value;
  10.  
  11.         if (inputName) {
  12.             let currentName = "";
  13.             currentName += inputName[0].toUpperCase();
  14.  
  15.             for (let i = 1; i < inputName.length; i++) {
  16.                 currentName += inputName[i].toLowerCase();
  17.             }
  18.  
  19.             let index = currentName.charCodeAt(0) - 65;
  20.  
  21.             if (listItemElements[index].textContent.length === 0) {
  22.                 listItemElements[index].textContent += currentName;
  23.             } else {
  24.                 listItemElements[index].textContent += ", " + currentName;
  25.             }
  26.  
  27.             inputTextElement.value = null;
  28.         }
  29.     })
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement