My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Can you please help me with this Javascript Code? Please check the comment -> // i didn't understand what going on here

Veera Venkata Avinash's photo
Veera Venkata Avinash
·Nov 10, 2017
var getWords = function(str) {
  var lastIndex = str.length - 1;

  if(str[0] === " ") {
    str = str.substring(1);
  }

  if(str[lastIndex] === " ") {
    str = str.substring(0, lastIndex);
  }

  return str.split(" ");
}

var data = ["avinash","supte"]
var dataLength = data.length;
var  index= {};
for(var i=0;i<dataLength;i++){
        var entry = data[i];
        if(entry === undefined){
            continue;
        }
        var str = getWords(entry);
        for(var j=0;j<str.length;j++){
            var item = str[j];
            var itemLength = item.length - 1;
            var node = index;
              for(var n = 0; n < itemLength; n++) {
                var char = item[n];
                var newNode = node[char];
                newNode = newNode === undefined ? {} : newNode;
                node[char] = newNode; // **
                node = newNode // i didn't understand what going on here
              }
            console.log(index)
        }
}