-1

Lately I have been trying to create a webpage with a search feature. My way of implementing this, while not the fastest or most elegant, should work in theory. All it does is split the search term into a list, the delimiter being a space, and then splits the keywords (in dictionary format, with the value being a download link, and with the key being the "keywords" I was referring to) and finally, it has an outer loop looping through the keys (being split each iteration into a list), and an inner loop looping through the words input through the input field. If a word in the search field matches one keyword of the key words list, then that key from the dictionary gets a score of +1.

This should sort the keys into order of best result to worst, and then the code can continue on to process all this information and display links to the downloadable files (the point of the webpage is to supply downloads to old software [of which I have collected over the years] etc.). However, when I run the program, whenever the alert(ranking.length) function is called, all I get is undefined in the output window.

Here is the code. (The search() function is called whenever the search button is pressed):

var kw_href = {
            "windows":["windows3.1.7z"],
            "ms dos 6.22":["ms-dos 6.22.7z"]
        }
        function search(){
            var element = document.getElementById("search_area");
            var search_term = element.value.toLowerCase();
            
            var s_tags = search_term.split(" ");
            var keys = Object.keys(kw_href);

            ranking = {
                "windows":0,
                "ms dos 6.22":0
            };

            for (i = 0; i < keys.length; i++){
                keywords_arr = keys[i].split(" ");
                for (x = 0; x < s_tags.length; x++){
                    if (keywords_arr.includes(s_tags[x])){
                        ranking[keys[i]] = ranking[keys[i]] + 1;
                    }
                }
            }
            // now we have a results list with the best results. Lets sort them into order.
            alert(ranking.length);
        }

Edit

alert(ranking.length) line is for debugging purposes only, and I was not specifically trying to find the length.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    `ranking` is an Object, so it does not have a `length` property – blex Jul 06 '20 at 17:50
  • No, since I am not trying to find the length - it was a test to determine if the sorting algorithm had worked. – sciencepiofficial Jul 06 '20 at 17:58
  • @sciencepiofficial What value were you expecting to get? – John Montgomery Jul 06 '20 at 18:00
  • Hopefully 2 since it was supposed to add the two keywords to the list with a score. – sciencepiofficial Jul 06 '20 at 18:01
  • So you wanted to get the number of keys in the object? In that case the duplicate is correct. – John Montgomery Jul 06 '20 at 18:02
  • No, that was not the purpose of this question. As mentioned in the edit, I was not trying to outright find the length. It was just a debug test and because of the `undefined` result, I thought I had done something wrong. – sciencepiofficial Jul 06 '20 at 18:03
  • The answer you just accepted says the same thing as the duplicate. I'm still confused what you're going for here. – John Montgomery Jul 06 '20 at 18:05
  • Basically, to test if the code works, I was checking if the dictionary's length had changed. I was not trying to get a result. It was a check, while the post you provided was dedicated to solving this problem. I was simply debugging. Hope you can understand :) – sciencepiofficial Jul 06 '20 at 18:08
  • I really don't. The question you asked was why that value was undefined. The duplicate explains how to make it not undefined. So it answers your question. Whether that's important to the overall goal of your program is irrelevant. – John Montgomery Jul 06 '20 at 18:12

2 Answers2

2

ranking is a generic object, not an array, so it won't have a computed length property.

If you want to count the number of properties in it, convert it to an array with Object.keys(ranking).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

ranking should be array of object like
ranking =[{"windows":0,"ms dos 6.22":0},{"windows":1,"ms dos 6.22":10}]
Then length ranking.length will work

Sohail Shahzad
  • 319
  • 1
  • 3
  • 12