Jump to content

Need Help Understanding associative Array

Sergio45
Go to solution Solved by vorticalbox,
5 minutes ago, Sergio45 said:

Yes, I want to see if blitzen was in the array,but after reading the explanation I think I don't want to go down that path.

Well- The assignment is going to be asking me to put the names of the reindeer in an array and store a count in a array and the image file in an array. So when the user types a name of the reindeer and types exit to stop the count. It shows the results of what was enter with the image associated with the character (x) many times. I was aiming to use an array like

 

var reindeer["rudolph"]= name,image,count(?)

and so on for each of the reindeer. 

 

But What I think was confusing me was the structure of each array.Plus which one would work best in the case of an  Associative or Multi-dimensional array.

 

if you change the  name in my code to a reindeer then you could have all the info where I put names.

 

reindeer = {
"blitzen":['image.jpg', 1],
"comet":['com.jpg', 6]
};

that could work

I am trying to get a better understanding on how Associative arrays work before I tackle my assignment. I made a function that created the reindeer name array and name array. I have the prompt asking for a name, in this case it's going to be blitzen to test it out. I just need to know what I am doing wrong. 

Thanks again in advanced for the help.

var $ = function(id) {return document.getElementById(id); }//end $

window.onload = function () 
{

	//Provide answer for Question 1 here
	
	//////////////////////
	associatve();

	
}


function associatve ()
{
	var reindeer =[];
	
	reindeer["name"]="blitzen","comet","cupid","dancer","dasher","donner","prancer",
	"rudolph","vixen","bumble";
	
	var blitzen = reindeer["name"]=[0];//should equal blitzen
	
	var comet = reindeer["name"]=[1]; //should equal comet
	
	var cupid = reindeer["name"]=[2]; //should equal cupid
	
	var dancer = reindeer["name"]=[3]; //should equal dancer
	
	var dasher = reindeer["name"]=[4]; //should equal dasher
	
	var donner = reindeer["name"]=[5]; //should equal donner
	
	var prancer = reindeer["name"]=[6]; //should equal prancer
	
	var rudolph = reindeer["name"]=[7]; //should equal rudolph
	
	var vixen = reindeer["name"]=[8]; //should equal vixen
	
	var bumble = reindeer["name"]=[9]; //should equal bumble
	
	var user=prompt("enter a name");
	var inputFromUser = user;
	
	if(inputFromUser==reindeer["name"][0]) 
	//or should it be this
	//if(inputFromUser==blitzen)
	{
	  alert("you got it");
	}
	else
	{
	    alert("Try again");
	}
	
	
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

The thing to understand is that an associative array is just a normal array. Or, more accurately, the reverse, every array is associative. Think about how you access something in a normal array:

var array = [ "blitzen", "comet" ]; // Creates an array

// These statements do the same, they add to the array
array[] = "cupid";
array.push("dasher");

console.log(array[0]); // Prints out "blitzen"

The thing that you have to understand, is that when you create that array, you are really doing this:

var array = [ "blitzen", "comet" ]; // Creates an array

// Does the same
array[] = "cupid";
array.push("dasher");

console.log(array); // Prints out the following:
// [
//	0: "blitzen", // You're printing this out below
//	1: "comet",
//	2: "cupid",
//	3: "dasher"
//]
console.log(array[0]); // Prints out "blitzen"

The "associative array" you are talking about is also known as a hash map, and is known in JavaScript as a key-value object. The above could be represented like this with different keys:

var object = {
	"foo": "blitzen",
	"bar": "comet"
}; // Creates an array

// Does the same
object["baz"] = "cupid";

console.log(object); // Prints out the following:
// {
//	foo: "blitzen", // You're printing this out below
//	bar: "comet",
//	baz: "cupid"
// }
console.log(object["foo"]); // Prints out "blitzen"

In you code, I'm not quite sure what you're trying to do. Are you trying to just have an array of reindeer, like this:

[
	"blitzen",
	"comet",
	"cupid",
	"dasher"
]

Or is it something else?

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

reindeer = {
"name":['blitzen', 'comet']
};
console.log(reindeer["name"][0])
console.log(reindeer["name"][1])

output

Quote

blitzen

comet

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, dannytech357 said:

 


[
	"blitzen",
	"comet",
	"cupid",
	"dasher"
]

Or is it something else?

What I am trying to do is put the names of the reindeer in the reindeer["name"] array and compare it to what the user types in. So if the user types blitzen i want to compare it the reindeer["name"][0] array. If that is possible?

Link to comment
Share on other sites

Link to post
Share on other sites

So you want to see if blitzen is in the array? Or do you want to check whether it's equal to a specific element?

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Sergio45 said:

What I am trying to do is put the names of the reindeer in the reindeer["name"] array and compare it to what the user types in. So if the user types blitzen i want to compare it the reindeer["name"][0] array. If that is possible?

 

The code I posted will let you do that my question is why have an array at all if all you are looking for is one answer?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, dannytech357 said:

So you want to see if blitzen is in the array? Or do you want to check whether it's equal to a specific element?

Yes, I want to see if blitzen was in the array,but after reading the explanation I think I don't want to go down that path.

1 hour ago, vorticalbox said:

The code I posted will let you do that my question is why have an array at all if all you are looking for is one answer?

Well- The assignment is going to be asking me to put the names of the reindeer in an array and store a count in a array and the image file in an array. So when the user types a name of the reindeer and types exit to stop the count. It shows the results of what was enter with the image associated with the character (x) many times. I was aiming to use an array like

 

var reindeer["rudolph"]= name,image,count(?)

and so on for each of the reindeer. 

 

But What I think was confusing me was the structure of each array.Plus which one would work best in the case of an  Associative or Multi-dimensional array.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Sergio45 said:

Yes, I want to see if blitzen was in the array,but after reading the explanation I think I don't want to go down that path.

Well- The assignment is going to be asking me to put the names of the reindeer in an array and store a count in a array and the image file in an array. So when the user types a name of the reindeer and types exit to stop the count. It shows the results of what was enter with the image associated with the character (x) many times. I was aiming to use an array like

 

var reindeer["rudolph"]= name,image,count(?)

and so on for each of the reindeer. 

 

But What I think was confusing me was the structure of each array.Plus which one would work best in the case of an  Associative or Multi-dimensional array.

 

if you change the  name in my code to a reindeer then you could have all the info where I put names.

 

reindeer = {
"blitzen":['image.jpg', 1],
"comet":['com.jpg', 6]
};

that could work

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×