Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2009, 11:19 PM   PM User | #1
Red_Dragon
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red_Dragon is an unknown quantity at this point
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.
Red_Dragon is offline   Reply With Quote
Old 12-08-2009, 12:13 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-08-2009, 01:28 PM   PM User | #3
Red_Dragon
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red_Dragon is an unknown quantity at this point
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
Red_Dragon is offline   Reply With Quote
Old 12-08-2009, 06:38 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-08-2009, 07:13 PM   PM User | #5
Red_Dragon
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red_Dragon is an unknown quantity at this point
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?
Red_Dragon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:37 PM.


Advertisement
Log in to turn off these ads.