Jump to content

JavaScript - Compare two Arrays?

Guest
Go to solution Solved by colonel_mortis,

Figured I'd use this thread as it's related to this, rather than create a new one. 

 

@colonel_mortis I'm pleading for your expertise and experience here. I need to create a report discussing how different browsers render JavaScript. That is, how different browsers use JavaScript differently to others... However, i have no idea of some of the difference and it's a rather difficult task IMO. 

 

Could you, or anyone for that matter, literally just provide a few points or ideas on how they use JavaScript differently. All I've got so far is the antithetical differences of the likes of alerts and prompts etc. in different browsers. 

 

Appreciate any help or advice!

You could have a look at comparing the different feature supports of the different browsers - http://kangax.github.io/compat-table/es6/ would be a good place to start. Also, you might find this Stack Overflow post is helpful.

So, I'm kind of at a loose end as to how I would actually go about doing what i want to do... JavaScript is something I'm relatively new to but have enough understanding of it to grasp anything new.

 

Basically, i'm trying to create a rather simple typing test for an interactive website. Only, i need to check how many of the words in the second array (what the user types) are the same as that in the first array... If that even makes sense. 

 

Just as an example - Say this was this text given for the user to type in one minute "Basically, i'm trying to create a rather simple typing test for an interactive website."

 

That would be the first array: var array1 = new Array ("Basically", "i'm", "trying", "to", "create", "a", "rather", "simple", "typing", "test", "for", "an", "interactive", "website");

 

I then need to somehow find out how many words the user types, are the same as the ones given for them to type. I'm not asking for someone to do this for me, just how i would go about doing it would be a MASSIVE help to me.

 

Oh, and what the user types might not be every word given to them to type... 

 

Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

I think this code should do what you want:

var correctWords = ["an", "array", "of", "the", "words", "that", "should", "exist"];var theirWords = ["array", "of", "words", "they", "typed"];var numCorrect = 0;var startingIndex = 0; for (var i = 0; i < theirWords.length; i++){    var index = correctWords.indexOf(theirWords[i], startingIndex);    if(index !== -1) {        //They typed a correct word        numCorrect++;        correctWords.splice(index, 1); //Remove the word from the array        startingIndex = index; //Make it start from the next word in the array    }}

If it doesn't matter what order the words need to be in, you can remove all references to startingIndex.

Edited by colonel_mortis
Fixed mistake

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I think this code should do what you want:

var correctWords = ["an", "array", "of", "the", "words", "that", "should", "exist"];var theirWords = ["array", "of", "words", "they", "typed"];var numCorrect = 0;var startingIndex = 0; for (int i = 0; i < theirWords.length; i++){    var index = correctWords.indexOf(theirWords[i], startingIndex);    if(index !== -1) {        //They typed a correct word        numCorrect++;        correctWords.splice(index, 1); //Remove the word from the array        startingIndex = index; //Make it start from the next word in the array    }}

If it doesn't matter what order the words need to be in, you can remove all references to startingIndex.

 

I put that straight into a HTML file just to see how it worked, only the JavaScript doesn't seem to run in the browser and i can't seem to spot why... 

<html>	 <head>  <title>Test</title> </head>	 <body>	  <script type="text/javascript">	   var correctWords = ["an", "array", "of", "the", "words", "that", "should", "exist"];   var theirWords = ["array", "of", "words", "they", "typed"];   var numCorrect = 0;   var startingIndex = 0;	   for (int i = 0; i < theirWords.length; i++){   var index = correctWords.indexOf(theirWords[i], startingIndex);    if(index !== -1) {	//They typed a correct word	 numCorrect++;	 correctWords.splice(index, 1); //Remove the word from the array	 startingIndex = index; //Make it start from the next word in the array	 }   }		  document.write(numCorrect);				</script>		</body>	</html>
Link to comment
Share on other sites

Link to post
Share on other sites

can't you just do

 

var x = ["hey", "i", "am", "a", "test"]var y = ["hello", "world"]if (x == y) {alert("x and y are the same")} else {alert("x and y are not the same")}
edit: oops, you are trying to compare individual words, i think @colonel_mortis has the solution :)
Link to comment
Share on other sites

Link to post
Share on other sites

I put that straight into a HTML file just to see how it worked, only the JavaScript doesn't seem to run in the browser and i can't seem to spot why... 

<html>	 <head>  <title>Test</title> </head>	 <body>	  <script type="text/javascript">	   var correctWords = ["an", "array", "of", "the", "words", "that", "should", "exist"];   var theirWords = ["array", "of", "words", "they", "typed"];   var numCorrect = 0;   var startingIndex = 0;	   for (int i = 0; i < theirWords.length; i++){   var index = correctWords.indexOf(theirWords[i], startingIndex);    if(index !== -1) {	//They typed a correct word	 numCorrect++;	 correctWords.splice(index, 1); //Remove the word from the array	 startingIndex = index; //Make it start from the next word in the array	 }   }		  document.write(numCorrect);				</script>		</body>	</html>

I've been using too many different programming languages. On line 6 of my code, I wrote int when I should have put var. I've updated my post above, and it works when pasted into the dev console now.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I've been using too many different programming languages. On line 6 of my code, I wrote int when I should have put var. I've updated my post above, and it works when pasted into the dev console now.

 

Ahh i was wondering what int was when i first looked over it. Does exactly what i need it to do now. Thank you so much for your help!

Link to comment
Share on other sites

Link to post
Share on other sites

@colonel_mortis First, sorry for bothering you.

 

 

I've managed to get it to count the amount of same words in two arrays and select a random string of words so the text given to the user is not always the same. However, i need to assign the value of a HTML text box the variable containing the randomly chosen string of text...

<html>  <head>    <script type="text/Javascript">      var things = ['Rock yeah but the cat', 'Paper and i know', 'Scissor but no yes', 'tale how ab      out you food'];      var thing = things[Math.floor(Math.random()*things.length)];      alert(thing);    </script>  </head>	  <body>	    <input type="text" id="txtBox1">	  </body>	</html>

That's to randomly chose one of the strings in the array things, but how would i give txtBox1 the value of variable thing. Promise this will be the last time i bother you  ;)

 

EDIT: Never mind, document.GetElementById wasn't working as i had the script above the form element in the HTML. All's good now :) !

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

Figured I'd use this thread as it's related to this, rather than create a new one. 

 

@colonel_mortis I'm pleading for your expertise and experience here. I need to create a report discussing how different browsers render JavaScript. That is, how different browsers use JavaScript differently to others... However, i have no idea of some of the difference and it's a rather difficult task IMO. 

 

Could you, or anyone for that matter, literally just provide a few points or ideas on how they use JavaScript differently. All I've got so far is the antithetical differences of the likes of alerts and prompts etc. in different browsers. 

 

Appreciate any help or advice! 

Link to comment
Share on other sites

Link to post
Share on other sites

Figured I'd use this thread as it's related to this, rather than create a new one. 

 

@colonel_mortis I'm pleading for your expertise and experience here. I need to create a report discussing how different browsers render JavaScript. That is, how different browsers use JavaScript differently to others... However, i have no idea of some of the difference and it's a rather difficult task IMO. 

 

Could you, or anyone for that matter, literally just provide a few points or ideas on how they use JavaScript differently. All I've got so far is the antithetical differences of the likes of alerts and prompts etc. in different browsers. 

 

Appreciate any help or advice!

You could have a look at comparing the different feature supports of the different browsers - http://kangax.github.io/compat-table/es6/ would be a good place to start. Also, you might find this Stack Overflow post is helpful.

HTTP/2 203

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

×