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

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)
        }
}
Write your answer…

3 answers

Let me add step by step comments in your code, to explain what is going on at the step you're having trouble understanding!

var data = ["avinash", "supte"]
var dataLength = data.length;
var  index = {};
for (var i = 0; i < dataLength; i++){
    var entry = data[i]; // entry will be "avinash"
    if (entry === undefined) {
        continue;
    }
    var str = getWords(entry); // str will be ["avinash"]
    for (var j = 0; j < str.length; j++) {
        var item = str[j]; // item will be "avinash"
        var itemLength = item.length - 1;
        var node = index;
        for (var n = 0; n < itemLength; n++) {
            // char will be "a" in the 1st iteration
            var char = item[n];
            var newNode = node[char];
            newNode = newNode === undefined ? {} : newNode;

            node[char] = newNode;
            // At this point node will be { a: {} }

            node = newNode;
            // What's happening in the above step is...
            // node is being set to the obj. represented by...
            // the key "a" in the index object, which is { a: {} }

            // Think of it this way, when you do:
            // node[char] = newNode ... you're essentially saying
            // node.a = {} which will make node equal to { a: {} }
            // ...and finally when you do:
            // node = newNode, you're saying
            // node = node.a

            // This process of assigning inner most nested obj.
            // trickles down the loop!


            // So, at the end of the second iteration index will be...
            // { a: { v: {} } } ... and when you do
            // node = newNode at the end of the second iteration
            // node will be set to the obj. represented by...
            // the key "v" in the index object, which is { a: { v: {} } }
        }
        console.log(index);
    }
}

Hope that makes sense, if not don't hesitate to let me know! :)

Having said that, I would say the above code can be refactored to make its intent, and readability much more clearer! I was able to quickly understand it, only because I understood what you were trying to do thanks to one of your earlier questions!

Reply to this…

Share your programming knowledge and learn from the best developers on Hashnode

Get started

In addition, let me refactor this a bit:

function getWords(str = '') {
  return str.trim().split(' ')
}

function createSubobjects(str = '') {
  if (str.length > 1) {
    return {
      [str[0]]: createSubobjects(str.slice(1))
    }
  }
  return str
}

const data = ["avinash", "supte"]

const index = data.reduce((node, entry) => {
  if (entry) {
    getWords(entry)
      .forEach(word => {
        Object.assign(node, createSubobjects(word))
      })
  }
  return node
}, {})

console.log(index)

This is a bit shorter and more readable.

Reply to this…

We are looking for the roblox generator robux best game free robux money big fan for the play online.

Reply to this…