Webmatrix Razor Summing numbers in foreach loop - razor

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;
}

Related

Part of the display will not show

I have some method in which I want to display a sudoku grid, nothing fancy (it prints lines to the console). Here is the method:
public static void DisplaySudoku(){
System.out.println("=====================================");
for(int i = 0 ; i < 9 ; i++){
System.out.print("|");
for(int j = 0 ; j < 9 ; j++){
System.out.print(" " + String.valueOf(linesArray[i][j].charAt(2)) + " |" );
}
System.out.print("\r");
}
System.out.println("=====================================");
}
The array linesArray[][] is built somewhere else, and is actually working, stored in a member of the class (I have tested it, displaying values within the main method without any problem). But the output when compiling and running gives me only the top and bottom lines:
=====================================
=====================================
I wonder why it seems to skip everything inside the loops?
Got it, the \r returns to the beginning of the line without actually changing to a new line. Will try to work it out some other way.

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>

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

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;
}
}

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);

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8