Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to use less for loops #38

Open
LaLeonie opened this issue Jul 19, 2019 · 0 comments
Open

Refactor code to use less for loops #38

LaLeonie opened this issue Jul 19, 2019 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@LaLeonie
Copy link

Forgive me for this rather controversial issue. I love your code... but I think it could be slightly improved by replacing some of your for loops by using more forEach, map or reduce methods.

For example, the code below could be changed to categories.forEach(...)

for(let i = 0; i < categories.length; i++){
let newLine = document.createElement("li");
let parentCrimes = document.querySelector(".categoriesOfCrimes");
parentCrimes.appendChild(newLine);
newLine.setAttribute("class", "crimes");

   newLine.textContent = `${categories[i]}: ${numbers[i]}`;
  }

Here is an example of how we used for Each in a similar scenario:
// Function for creating list items
function listCreation(str) {
var listItem = document.createElement("li");
listItem.textContent = str;
recipeIngredients.appendChild(listItem);
}

// Function to add each ingredient
function addIngredients() {
recipeIngredients.textContent = "";
ingredients.forEach(c => listCreation(c));
}

@LaLeonie LaLeonie added the enhancement New feature or request label Jul 19, 2019
xIrusux added a commit that referenced this issue Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants