5 Replies - 637 Views - Last Post: 02 December 2013 - 03:01 PM

#1 wkinne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 13

searching a javascript array

Posted 02 December 2013 - 12:53 PM

I am having a bit of trouble with this program. The variable is already set and is on a different js file. I am able to run the program until I get to the search function. I can't figure out why the search function will not run. Any help would be great. This is for school so thats why all the comments are there.


Thank you

// Create privatized scope using a self-executing function
(function() {
	console.log("hello");
	// Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's)
	var resultsDIV = document.getElementById("results"),
		searchInput = document.forms[0].search,
		currentSearch = '';


	// Validates search query
    var validate = function (query) {
        console.log("validate");
        // Trim whitespace from start and end of search query
            query = query.trim();
        // Check search length, must have 3 characters
        if (query.length < 3) {
            alert("Your search query is too small, try again.");
           }else{


        // (DO NOT FIX THE LINE DIRECTLY BELOW)
        searchInput.focus();
        }

    };




	// Finds search matches
    var search = function (query) {
        console.log("In search function");
        // split the user's search query string into an array
        var queryArray = query.split(" ");

        // array to store matched results from database.js
        var results = [];
        // loop through each index of db array
        for (var i = 0, j = db.length; i < j; i++) {
            console.log(i);
            // each db[i] is a single video item, each title ends with a pipe "|"
            // save a lowercase variable of the video title
            var dbTitleEnd = db[i].indexOf('|');
            var dbItems = db[i].toLowerCase().substring(0, dbTitleEnd);

        }
        // loop through the user's search query words
        // save a lowercase variable of the search keyword
        for (var ii = 0, jj = queryArray.length; ii < jj; ii++) {
            var qItem = queryArray[ii].toLowerCase();
        }
            // is the keyword anywhere in the video title?
            // If a match is found, push full db[i] into results array
            var compare = dbItems.indexOf(qItem);
            if (compare !== -1) {
                results = results.push(db[i]);
            }

            results.sort();





        // Check that matches were found, and run output functions





        // Put "No Results" message into page (DO NOT FIX THE HTML VAR NOR THE innerHTML)


        var noMatch = function() {

            var html = '' +
                    '<p>No Results found.</p>' +
                    '<p style="font-size:10px;">Try searching for "Javascript".  Just an idea.</p>'
                ;
            resultsDIV.innerHTML = html;
        }

        // Put matches into page as paragraphs with anchors


        var showMatches = function(results) {


            // THE NEXT 4 LINES ARE CORRECT.
            var html = '<p>Results</p>',
                title,
                url
                ;

            // loop through all the results search() function
            for (var i = 0, j = results.length; i < j; i++) {

                // title of video ends with pipe
                // pull the title's string using index numbers
                var titleEnd = results[i].indexOf('|');
                title = results[i].subString(0, titleEnd);

                // pull the video url after the title
                url = results[i].substring(results[i].indexOf('|') + 1, results[i].length);

                // make the video link - THE NEXT LINE IS CORRECT.
                html += '<p><a href=' + url + '>' + title + '</a></p>';

                resultsDIV.innerHTML = html; //THIS LINE IS CORRECT.
            }

            if (results.length === 0) {
                noMatch();
            } else {
                showMatches(results);
            }
    };

    };








console.log("start of program");
/***** start of program *******/
	// The onsubmit event will be reviewed in upcoming Course Material.
	// THE LINE DIRECTLY BELOW IS CORRECT
	document.forms[0].onsubmit = function(){
		var query = searchInput.value;
        validate(query);
        search();

        

        // return false is needed for most events - this will be reviewed in upcoming course material
        // THE LINE DIRECTLY BELOW IS CORRECT
		return false;
	};

	})();





Is This A Good Question/Topic? 0
  • +

Replies To: searching a javascript array

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: searching a javascript array

Posted 02 December 2013 - 12:54 PM

Moving to 'javascript' from 'other languages'
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: searching a javascript array

Posted 02 December 2013 - 12:58 PM

 title = results[i].subString(0, titleEnd);

substring() is in lowercase: Javascript is case-sensitive.

I don't know if there are other issues, I didn't search beyond this one.

This post has been edited by andrewsw: 02 December 2013 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 wkinne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 13

Re: searching a javascript array

Posted 02 December 2013 - 02:56 PM

View Postandrewsw, on 02 December 2013 - 12:58 PM, said:

 title = results[i].subString(0, titleEnd);

substring() is in lowercase: Javascript is case-sensitive.

I don't know if there are other issues, I didn't search beyond this one.


Thank you for that. Now how can I run the search function succesfully? I am having a really hard time with that.

Thanm you
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: searching a javascript array

Posted 02 December 2013 - 02:58 PM

Quote

I am having a really hard time with that.

*HOW*? How are you having a hard time with it?
Was This Post Helpful? 0
  • +
  • -

#6 wkinne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 13

Re: searching a javascript array

Posted 02 December 2013 - 03:01 PM

View Postmodi123_1, on 02 December 2013 - 02:58 PM, said:

Quote

I am having a really hard time with that.

*HOW*? How are you having a hard time with it?


I don't know when to call the function. And the spots I have put it have not been able to grab any results. Just curious why this is happening?


Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1