-2

I have a two function, that using a new format. I would like to change this code, but I need that this function did exactly the same.

first function:

let sts = $(".st--footer-column");

for (let i of sts) {
  while (i.childElementCount) {
    i.parentNode.appendChild(i.firstElementChild)
  }
  i.parentNode.removeChild(i)
}

second function:

let groups = $(".st-footer--navigation");

let child = groups.find('.st--footer-column');

if (!child.length) {
  for (let i of groups) {
    let childrens = i.children;
    let stFooterColumn = document.createElement("div");

    // add the class
    stFooterColumn.classList.add("st--footer-column");

    // add each child to the new div
    while (childrens.length) {
      stFooterColumn.appendChild(childrens[0])
    }

    // append the div to previews to group
    i.appendChild(stFooterColumn)
  }
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
JsStorm2
  • 75
  • 6
  • Why? And have you tried simply replacing it with `var`? –  Aug 27 '18 at 08:02
  • https://babeljs.io/ might be helpfull – scraaappy Aug 27 '18 at 08:03
  • What was the problem when you tried to replace `let` with `var`? Why do you think that it is a problem in the given code? – t.niese Aug 27 '18 at 08:03
  • here is a basic definition of [what let is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) and here are the [differences between let and var](https://stackoverflow.com/a/11444416/2417602) – vikscool Aug 27 '18 at 08:04
  • 6
    Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Jon Skeet's [question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/) is also useful. What is the goal of the change? What isn't working about your new version? What makes you focus on `let` specifically? (You also have `for-of`, which was introduced in the same update -- ES2015 -- as `let`.) – T.J. Crowder Aug 27 '18 at 08:05
  • 4
    (Just FYI: In English, "children" is already plural, so you don't use "childrens". [There's an unrelated contraction, "children's", but that's a possessive -- "the children's room".]) – T.J. Crowder Aug 27 '18 at 08:06

1 Answers1

-1

I would leave them as is, but if you want to change them, you can literally just replace let and var and the functionality will be unchanged, specifically because you are saying they are functions.

var is limited to the function's scope, so it won't affect anything outside of the function.

let is block scoped, but in the examples you've given replacing with var will not affect the rest of the function.

function firstFunction() {
    var sts = $(".st--footer-column");

    for (var i of sts) {
        while (i.childElementCount) {
            i.parentNode.appendChild(i.firstElementChild);
        }
        i.parentNode.removeChild(i);
    }
}

function secondFunction() {
    var groups = $(".st-footer--navigation");

    var child = groups.find(".st--footer-column");

    if (!child.length) {
        for (var i of groups) {
            var childrens = i.children;
            var stFooterColumn = document.createElement("div");

            // add the class
            stFooterColumn.classList.add("st--footer-column");

            // add each child to the new div
            while (childrens.length) {
                stFooterColumn.appendChild(childrens[0]);
            }

            // append the div to previews to group
            i.appendChild(stFooterColumn);
        }
    }
}
ed'
  • 1,815
  • 16
  • 30