Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Array and Functions help

    Forgive me if the code is rather bad, I am new to Javascript.

    Now what I'm aiming to do is after I have entered the contents of the Array's I then want to query the newly created array/database, based on the track number, Via a seprate function.

    However all the values I entered, always come up empty.

    No matter how many solutions I tried I can't get the query to work. So I've decided to come to you guys/gals for help.

    Code:
    mainMenu()
    
    function mainMenu()
    {
    	menu = 0;
    	menu  = eval(prompt("Select an Operation to Continue\n ====================\n 1 - Create new Database\n 2 - Query Database\n 3 - Show Report\n Any other to exit"));
    	switch (menu)
    	{
    		case 1:
    		insert(trackNumber)
    		break;
    
    		case 2:
    		query(trackNumber)
    		break;
    	
    		case 3:
    		break;
    	}
    }
    
    // Enter the track number
    function trackNumber()
    {
    	var theNumber = eval(prompt("Enter Track Number"));
    	if ((theNumber >= 0) && (theNumber < 100))
    	{
    		return theNumber; // Stores the track number for insert(trackNumber)
    	}
    }
    
    // This function will allow the user via the (trackNumber)
    // parameter to insert the contents of the database
    function insert(trackNumber)
    {
    	// Create new Array's
    	trackNum = new Array(5)
    	trackName = new Array(5)
    	trackArtist = new Array(5)
    	
    	// For each Track we want to hold the trackNumber(), 
    	// the track name and the track's artist.
    	for(var i = 0; i < trackNum.length; i++)
    	{
    		trackNum[i] = trackNumber()
    		trackName[i]= prompt("Enter Track Name: " + i)
    		trackArtist[i] = prompt("Enter Track Artist: " + i)
    	}
    }
    
    // This Function will allow the user to query the database, based on track number.
    function query(trackNumber)
    {
    	var trackSearch = trackNumber()
    	var j = 0;
    
    	for(j in query)
    	{
    		if (query[j] == trackSearch)
    		{
    			alert("Track Number: " + trackSearch + "\nTrack Name: " + trackName[j] + "\nTrack Artist " + trackArtist[j]);
    			break;
    		} 
    		else if (query[j] != trackSearch)
    		{
    			alert("Track Number: Empty" +  "\nTrack Name: Empty" + "\nTrack Artist: Empty");
    			break;
    		} 
    	}
    }
    If someone could please help me by posting some relevant code that would help accomplish this, I would greatly appreciate it, as so far I have been scratching my head puzzled, at how I could solve this.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    In this code:
    Code:
    	switch (menu)
    	{
    		case 1:
    		insert(trackNumber)
    		break;
    
    		case 2:
    		query(trackNumber)
    		break;
    what is the point in passing trackNumber as an argument???

    I see no reason to do that.

    And in this code:
    Code:
    function query(trackNumber)
    {
    	var trackSearch = trackNumber()
    	var j = 0;
    
    	for(j in query)
    	{
    First, "query" is a function. Then you turn around and use it as a collection.

    HUH??? Not possible.

    Hint: When you are querying for a given trackNumber, maybe you should look in the array where you *STORED* the trackNumber values???

    I think that's enough homework help for now.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, looked through some old notes and rewrote the code based round that, hopefully it's an improvement over the previous one.
    No error's have popped up in the console but program still does not run as I want to.
    Runs perfectly fine however, it always comes up "empty"

    Code:
    	//** Main Program **
    
    //create an array
    var trackData = new Array(5)
    
    for(var i = 0; i < trackData.length; i++)
    {
    	trackData[i] = new Array(3);
    	for(var j = 0; j < trackData[i].length; j++)
    	{
    		trackData[i][j] = prompt("Enter data " + j + " for Track " + i + "\n 0 - Number, 1 - Name, 2 - Artist");
    	}
    }
    
    query()
    
    	//** Functions **
    
    //Purpose: To search an array for a given string
    
    function query()
    {
    	var trackSearch = prompt("Enter track number to search for ");
    	var j = 0;
    	for(j in trackData)
    	{
    		if(trackData[j] == trackSearch)
    		{
    			alert(trackInfo)
    			break;
    		}
    		else if (trackData[j] != trackSearch)
    		{
    			alert("Track Number: Empty" +  "\nTrack Name: Empty" + "\nTrack Artist: Empty");
    			break;
    		}
    	}		
    }
    Spot any Errors that may cause this?
    Last edited by Red_Dragon; 12-08-2009 at 01:58 PM. Reason: Rewrote Code

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Ummm...I think you've taken a step backward.
    In human interface, at least.

    I think it's bad to have that generic prompt for the three pieces of data instead of the three explicit prompts you had the first time.

    Yes, using an array of arrays is an improvement. But an array of objects would be even more of one.

    And your search is flawed in the same way it was the first time: You make ONE CHECK in the loop for the searched-for number. If you don't find it, you issue that alert showing null values. No. Wrong. Only if you don't find a match after *ALL* the iterations through the loop should you issue a "not found" message.

    Finally (for now!) look at this line:
    Code:
         alert(trackInfo)
    WHERE does the variable trackInfo come from???

    HINT: No place.

    Also:
    Code:
         if(trackData[j] == trackSearch)
    Okay, what kind of variable/value is trackSearch?? And what kind of value is trackData[j]?

    HINT: They aren't at all the same kinds of thing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    // An Array to store Track details
    var trackArray = new Array(5);
    
    // Initialise the Array to hold Track Objects
    for(var i=0; i < trackArray.length; i++)
    {
    	trackArray[i] = new insert;
    }
    
    // Get details of tracks
    for(var i=0; i < trackArray.length; i++)
    {
    	trackArray[i].number = prompt("Enter Track Number ");
    	trackArray[i].name = prompt("Enter Track Name for Track " + trackArray[i].number);
    	trackArray[i].artist = prompt("Enter Track Artist for Track " + trackArray[i].number);
    }
    
    // ** Functions **
    
    function insert(number, name, artist)
    {
    	this.number = number;
    	this.name = name;
    	this.artist = artist;
    }
    Is this what you mean by Array of Objects?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •