I have three ints that change as the user uses the app. i'm trying to set up a function that adds them together passively in the background and outputs a to another int that i've created. so i can then use it in an if else statement down the line to check if it is greater or less than another int. I can set it to work when the user presss a button, but is there a way to do it passivly? here is my function:
int calculateLeftTotalVP(){return movpLeftCounter + totalTOLeftCounter + paintedLeftCounter = totalVPLeft;}
I'm getting the error: Missing selector such as '.identifier' or '[0]'. Try adding a selector.
under the 3 ints i'm trying to add together.
what would be the best way to go about this please? cheers
I think you want like this
int calculateLeftTotalVP() {
return totalVPLeft =
movpLeftCounter + totalTOLeftCounter + paintedLeftCounter;
}
I would prefer passing variable and then return the result from it.
int calculateLeftTotalVP({
required int movpLeftCounter,
required int totalTOLeftCounter,
required int paintedLeftCounter,
}) {
return movpLeftCounter + totalTOLeftCounter + paintedLeftCounter;
}
totalVPLeft = calculateLeftTotalVP(movpLeftCounter: ,paintedLeftCounter: ,totalTOLeftCounter: );
Related
using UnityEngine;
using System.Collections;
public class NewMonoBehaviour1 : MonoBehaviour
{
void ConcatExample(int[] intArray)
{
string line = intArray[0].ToString(); // the line is the var of the first in array
for(i =1;i <intArray.Length; i++) // the length is unknown ?
{
line += ", " + intArray[i].ToString(); //
}
return line;
//each time allocate new in original place
}
}
How can this function work ? the length of array is unknown , so how the for loop works ?Besides, this is void function but shouldn't return anythings right ,or is there any exceptional case ,finally,according to the unity manual, it is said that the function will keep producing a string but with new contents in the same place , resulting in consuming large memory space .Why ?thx
What makes you think that the Length should be unknown? It is a property that any array simply has
Gets the total number of elements in all the dimensions of the Array.
Of course it is not unknown the moment you call your method with an according parameter!
The return line; will not even compile since as you say the method is of type void so it can not return anything. It should probably be private string ConcatExample
Then what the unity manual (don't know where exactly you read this) means lies in
line += ", " + intArray[i].ToString();
under the hood every string in c# is an immutable char[]. So everytime you do a string concatenation via stringC = stringA + stringB what happens under the hood is basically something similar to
char[] stringC = new char[stringA.Length + stringB.Length];
for(var iA = 0; iA < stringA.Length; iA++)
{
stringC[i] = stringA[i];
}
for(var iB = 0; iB < stringB.Length; iB++)
{
stringC[iB + stringA.Length] = stringB[iB];
}
so whenever dealing with loops especially with large data it is strongly recommended to rather use a StringBuilder like
private string ConcatExample(int[] intArray)
{
var builder = new StringBuilder(intArray[0]);
for(i =1; i < intArray.Length; i++)
{
builder.Append(", ").Append(intArray[i].ToString());
}
return builder.ToString();
}
The length of the array will be the length of the array of ints you pass into the function as an argument.
say you pass it
Int[] ints = {1,2,3}
ConcatExample(ints); //the length of the array is now 3
add a debug.log() function to the ConcatExample method
void ConcatExample(int[] intArray)
{
string line = intArray[0].ToString();
for (int i = 1; i < intArray.Length; i++)
{
line += ", " + intArray[i].ToString(); //
Debug.Log(line);
}
}
debug.log would produce the following in the console
1, 2
1, 2, 3
and finally the return line; at the end would just result in an error because yes you are correct void returns nothing
This function CANNOT work, unless it gets the data it expects. A NULL passed to this function, for example, would generate a runtime null-reference exception. Passing a valid integer array, of length zero would generate an invalid index error on the first line.
You are correct, the function returns nothing, and appears pointless. In fact, I would have expected return line; to generate a complier error.
The string type appears "dynamic" meaning, it will indeed allocate more and more memory as needed. Technically, it is actually the string "+" operator, (a function that takes two strings as parameters) that is allocating this space. This function returns a new string, of the appropriate size. The garbage collector will DEallocate "old" strings when they are no longer referenced by any variables.
I created the following function to return two random integer values:
List<int> randomGenerator() {
return [(Random().nextInt(6) + 1),(Random().nextInt(6) + 1)];
}
And want to set to two program variables (left and right dice).
So tried by executing:
[rightDiceNumber,leftDiceNumber] = randomGenerator();
But it didn't work.
you can get your return value like this
List randomValues;
randomValues=randomGenerator();
print(randomValues[0]);
print(randomValues[1]);
I want to wait till first element gets updated to rate same as monthlyRate variable and second element gets updated to rate same as annualRate.
With below code I get:
Failed: Wait timed out after 9008ms
public waitForSubscriptionRates = (rateselector: string, monthlyRate: string, annualRate: string) => {
browser.wait(function(){
element.all(by.css(rateselector)).filter(function(elem, index) {
return elem.getText().then(function(text) {
console.log('text=' + text);
console.log('monthlyRate=' + monthlyRate);
console.log('annualrate=' + annualRate);
return (text === monthlyRate || text === annualRate);
});
}).first().click();
},9000);
}
I'm not sure do I understand your code well.
I assume that there are two selectors - first may contain text of monthlyRate and second annualRate. They can not be mixed, isn't it?
I don't get what you want to click.
Anyway, you can chain ExpectedConditions with or keyword.
See the example in the documentation: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.or
I'm using getElementsByTagName to return all the select lists on a page - is it possible to then filter these based upon an option value, ie of the first or second item in the list?
The reason is that for reasons I won't go into here there are a block of select lists with number values (1,2,3,4,5 etc) and others which have text values (Blue and Black, Red and Black etc) and I only want the scripting I have to run on the ones with numerical values. I can't add a class to them which would more easily let me do this however I can be certain that the first option value in the list will be "1".
Therefore is there a way to filter the returned list of selects on the page by only those whose first option value is "1"?
I am pretty sure that there is a better solution, but for the moment you can try something like:
var allSelect = document.getElementsByTagName("select");
var result = filterBy(allSelect, 0/*0 == The first option*/, "1"/* 1 == the value of the first option*/);
function filterBy(allSelect, index, theValue) {
var result = [];
for (var i = 0; i < allSelect.length; i++) {
if(allSelect[i].options[index].value == theValue ) {
result.push(allSelect[i]);
}
}
return result;
}
I managed to get this working by wrapping a simple IF statement around the action to be performed (in this case, disabling options) as follows:
inputs = document.getElementsByTagName('select');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].options[1].text == 1) {
// perform action required
}
}
No doubt there is a slicker or more economic way to do this but the main thing is it works for me.
I am totally new to Instruments>Automation. Trying to test the internal app using Automation in Instruments.
Here is my problem:
Our app has the UI cells generated on the fly. There is no way to predict how many cells will be created and what name they will have. But, all of them will contain a certain string (like "Courses"). The question is - How, using Automation, find out if particular cell contain that string in its name?
You are able to get total cells count simply using "length" property.
var cellsCount = <YourUIATableViewObject>.cells().length;
UIALogger.logMessage("total cells count = " + cellCount);
After that you will be able to get cell properties and operate with them:
for (var i = 0; i < cellsCount; i ++)
{
var cellValue = <YourUIATableViewObject>.cells()[i].value();
var cellName = <YourUIATableViewObject>.cells()[i].name();
UIALogger.logMessage("Cell #"+i+" properties: cellValue ="+cellValue+"; cellName ="+cellName);
//Try to use match() or search() functions to find what you need.
if ( cellName.search("Courses") != -1 )
//if (cellValue.search("Courses") != -1 )
{
UIAlogger.logMessage("Cell #"+i+" contains 'Courses'");
}
else
{
UIAlogger.logMessage("Cell #"+i+" does not contain 'Courses'");
}
}
This JavaScript tutorial will help you: