Search code examples
javascriptarraysstringnode-red

Problems with searching arrays


I wrote my code to search string for keywords and extracting needed data, but I have problems when I'm trying to search with keywords in arrays named: sort, title and artist. When I'm doing it I get an error about potential infinite loop.

var type = ['track','tracks','song','songs','album','albums'];
var artist = ['created by', 'made by'];
var genre = ['genre'];
var limit = ['set limit'];
var title = ['title','name'];
var sort = ['sort by', 'classificate by', 'separate by'];
var sortBy = ['popularity'];

// function changeWordsToNumbers(words) {
//         if (msg.numbers[words])
//             return msg.numbers[words];
// }

function searchForIndex(instruction, keywords) {
  for (i = 0; i < keywords.length; i++) {
    let search = instruction.search(keywords[i]);
    if (search)
      return search;
  }
  return false;
}


function getSearchResult(wanted) {
    var searchResult = {
            artist : searchForIndex(wanted, artist),
            genre : searchForIndex(wanted, genre),
            limit : searchForIndex(wanted, limit),
            type : searchForIndex(wanted, type),
            title : searchForIndex(wanted, title),
            sort : searchForIndex(wanted, sort),
            sortBy : searchForIndex(wanted, sortBy)
        };
    return searchResult;
}

function removeJunkKeyword(searchResult,instruction) {
    for(var property in searchResult) {
        if(searchResult.hasOwnProperty(property)) {
            if(searchResult[property] != - 1) {
                    instruction = instruction.slice(0, searchResult[property] - 1); 
            }
        }
    }
    return instruction;
}

function checkExist(searchResult) {
    for(var property in searchResult) {
        if(searchResult.hasOwnProperty(property)) {
            if(searchResult[property] != -1)
                return false;
        }
    }
    return true;
}

function findAndCleanQuery(instruction, keywords) {
        var exist = instruction.search(keywords);
        var result = instruction.slice(exist + keywords.length + 1, instruction.length);
        var searchResult = getSearchResult(result);
        if (exist != -1) {
            if (checkExist(searchResult)) {
                return result;
            } else {
                result = removeJunkKeyword(searchResult,result);
                return result;
            }    
        }
    return false;
}

function searchFor(instruction, keywords) {
  for (i = 0; i < keywords.length; i++) {
    let result = findAndCleanQuery(instruction,keywords[i]);
    if (result) 
      return result;
  }
  return false;
}

function searchForType(instruction) {
    for (i = 0; i < type.length; i++) {
        let search = instruction.search(type[i])
        if(search)
            return type[i];
    }
    return false;

}

function searchForKeywords(instruction) {
    msg.artist = searchFor(instruction, artist);
    msg.type = searchForType(instruction, type);
    msg.genre = searchFor(instruction, genre);
    msg.limit = searchFor(instruction, limit);
    msg.title = searchFor(instruction, title);
    msg.sort = searchFor(instruction, sortreg);
}
var msg = {}
msg.instruction = 'Search for me';
searchForKeywords(msg.instruction);
console.log(msg.artist);
console.log(msg.type);
console.log(msg.genre);
console.log(msg.limit);
console.log(msg.title);
console.log(msg.sort);

Link for code: https://repl.it/J4Mc/9

PS. The object msg is used by node-red to communicate between nodes.


Solution

  • The issue is that you're doing this on several of your loops:

    for (i = 0; i < keywords.length; i++) {
    

    ...where you should be doing this instead:

    for (let i = 0; i < keywords.length; i++) {
    

    Without using let, the variable i is effectively global. Each time you went into a new loop it got reset to 0, so it was never able to increase, which created the infinite loop.


    As a side note, you'll also notice sortreg is undefined when it's used on line 98.