Compare two different arrays in Actionscript 3 using For Loop within a For Loop - actionscript-3

basically I need a loop within a loop to compare two different arrays in my actionscript3 lottery game.
I have attempted the loop but I cannot seem to get it to work ...
check_win.addEventListener(MouseEvent.CLICK, f_check_win);
function f_check_win(event:Event):void{
for(index = 0; index < matches[index]; index++ ){
trace(index);
for(index2 = 0; index2 < input_array.length; index2++){
if (match[index2] == input_array[index2]){
choose_change = choose_change+1;
}
}
}
So basically within this code check_win is a button. Once the button is clicked it runs the loop. It is meant to take an instance of matches which contains 6 properties and loop until index is greater than matches. According to my output this is happening but the second loop doesn't appear to do anything. Any help is greatly appreciated.

Based on the code you've provided, it looks like your inner loop should look more like this:
for(index2 = 0; index2 < input_array.length; index2++){
if (matches[index] == input_array[index2]){
choose_change = choose_change+1;
}
}

Related

Using splice() to remove items that matches condition from array

Im trying to via a for-loop remove all the items that matches a condition in the state array. But it seems to only be removing the last items in the array and not the ones that matches. Am I using the .splice() incorrectly? Thanks in advance. Code is:
rmTravel() {
for(var i = 0; i < this.cards.length; i++){
if(this.cards[i].sg_categories.includes("travel")){
this.cards.splice(i, 1);
console.log('Removed following card:', this.cards[i].slug)
}
}
console.log('Cards in cards state: ', this.cards)
}
This is a bit of a classic problem; you're iterating forward and shrinking the array at the same time so you're going to end up skipping over records.
I suggest using Array.prototype.filter() instead
this.cards = this.cards.filter(({ sg_categories }) =>
!sg_categories.includes('travel'))
This will reduce the array to entries who's sg_categories property does not include "travel".

Looping through an array to display a quote

Attempting to loop through this array, stored in a JSON file. I want to be able to check if a quote is saved and then display it depending on whether it is saved or not.
_toggleCheck() {
var checked = !this.state.checked;
this.setState({ checked: checked });
// For loop to run through arrray and update booleans
for(int i = 0; i<quotesArray.length-1; i++;){
quotes.quotesArray[i].isSaved = checked;
}
this.props.onChange && this.props.onChange(this.props.name, checked);
}
In attempting to do this, I have come across this error and can not resolve it. Line 63 is the beginning of the for loop. I dont know how to use a for loop in react native and cant find any tutorials on line.
for(int i = 0; i<quotesArray.length-1; i++;){
^ remove this semicolon
By the way, are you sure you want i < quotesArray.length - 1 instead of i < quotesArray.length? You're skipping the last element when you add -1.

Webmatrix Razor Summing numbers in foreach loop

Inside a foreach loop i have a statement
#(var = var1 + var2 ) ;
the math occurs but the results var gets displayed on the page after each execution. I don't want that to happen just do the math so I can display results at the bottom of the table.
thanks
You need to remove the # symbol as when you're in your for each loop, you're already in a code block.
Your code should look a little like this:
#foreach (var i in a)
{
var x = i + i;
}

Razor: adding dynamic values together

I have a set of values that are generated dynamically with a foreach loop, how can I add these into one value?
For instance, say I have a site where each node has a number associated with it. How can I add all these numbers together? So far I've figured it'd be something similar to the following, where the value of 'node.aNumberValue' is added to the next one, and so on:
#foreach (var x in nodes){
var total = node.aNumberValue + node.aNumberValue (etc...);
<p>#total</p>
}
This is what you want, I think:
int total = 0;
#foreach (var x in nodes)
{
total += x.aNumberValue;
}
<p>#total</p>
Or even better, just:
<p>#nodes.Sum(x => x.aNumberValue)</p>

Difficulty outputting an array of indexes from an existing array consisting of strings

So I am trying to create a function that searches through an array based on a searchTerm. If the elements within the array have the searchTerm in it, it should output ALL of indexes inside of MyArray[];.
I hope I have explained clearly, thanks in advance.
Here's a corrected version:
var colours = ["I like the colour red", "I hate the colour yellow", "I love the colour blue"];
function myFunction(colours, searchTerm) {
var myArray = [];
searchTerm = searchTerm.toLowerCase();
for (var i = 0; i < colours.length; i++) {
if (colours[i].toLowerCase().indexOf(searchTerm) >= 0) {
myArray.push(i);
}
}
return myArray;
}
alert(myFunction(colours,"colour")) //Should return indexes 0,1,2 in myArray
And a working demo here: http://jsfiddle.net/jfriend00/GDM9R/.
I had to fix a lot of issues:
You weren't adding results to myArray properly.
You weren't adding the index to myArray.
You weren't testing the results of .indexOf() properly (it returns -1 when no match).
You were iterating over the length of the search phrase, not the number of items in the array.
You didn't declare i as a local variable so it was an implicit global variable.
myArray = colours[i] does not append to the array.
myArray.push(a);