How do you craft a switch statement in as3 to make the case apply to an entire range of numbers?
if (mcPaddle.visible == true)
{
switch (score)
{
case 10://10 to 100
myColor.color = 0x111111;
break;
case 110://110 to 1000
//etc etc
break;
}
}
I've tried multiple ways to make the case apply for all numbers between 10-100, and 110-1000, but can't seem to find a way to do it, and I can't find the proper syntax for such a thing in as3.
You can use a switch block :
var score:Number = 123;
switch(true){
case score > 120 && score < 125 :
trace('score > 120 && score < 125');
break;
case score > 100 && score < 140 :
trace('score > 100 && score < 140');
break;
case score == 123 :
trace('score == 123');
break;
}
//score > 120 && score < 125
switch statements just restatements of if (a = b) or (a = c) or (a = d) ... type constructs. THey're not intended for ranges. You can somewhat simulate it using fallthroughs:
switch (score) {
case 10:
case 11:
case 12:
case 13:
case etc...
blah blah blah
break;
}
but that's a ludicrously dumb way to go. Much easier/terser to use a regular if()
ActionScript's switch statement doesn't work with ranges, but you can easily do it with if/else chains:
if (score >= 10 && score <= 100)
{
//10 - 100
}
else if (score <= 110)
{
//101 - 110
}
else if (score <= 1000)
{
//111 - 1000
}
For those looking for how to use this in HTML/jQuery, I've used #OXMO456's answer to create this simple pen: http://codepen.io/anon/pen/jHFoB
You just have to set the var normally and remove the lines starting with trace.
Ps. I'm adding this as an answer since I don't have enough rep to comment on his. If anyone can, please move/copy this there. Thanks!
Related
This is UCB CS61A's firt project hog 's problem5
this is wrong code(my code)
while(score0<goal and score1<goal):
if who==0 :
cur_strategy = strategy0
cur_score = score0
cur_op_score=score1
cur_op_strategy = strategy1
else:
cur_strategy = strategy1
cur_score = score1
cur_op_score=score0
cur_op_strategy = strategy0
cur_score=cur_score+take_turn(cur_strategy(cur_score,cur_op_score),cur_op_score,dice)
if(extra_turn(cur_score,cur_op_score)==False):
who=other(who)
this is the correct code(I have tested it)
while score0 < goal and score1 < goal:
if who == 0:
num_rolls = strategy0(score0, score1)
score0 += take_turn(num_rolls, score1, dice)
who = other(who) if extra_turn(score0, score1) == False else who
else:
num_rolls = strategy1(score1, score0)
score1 += take_turn(num_rolls, score0, dice)
who = other(who) if extra_turn(score1, score0) == False else who
But actually,I think these two codes are essentially same.
I don't know whether this is the problem (the quote from the project)
Only call a strategy function once per turn (or risk breaking the GUI).
I think that comes from the fact that in order to update the score0 an score1 variables, you define cur_score and update it instead. The problem is that cur_score is only a copy of the score you want to update, it is not the same object.
I am trying to make a for loop for this nested if statement in a Bejeweled clone I am practicing to code. As you can see it's pretty dumb to do it this way, because I want to loop through more matches, so I was wondering if there is a more efficient way than going through and making even more silly If statement nests about 7 times AND for each direction since the grid is 8 by 8.
The if ( i !== 0 ) bit is to prevent null errors. I think with the For statement if it were to be used it can be something like if ( i < loopvar ) instead
Thank you :3
if (i !== 0 && map[i][j].name == map[i-1][j].name) // this nest of if statements are gonna check the one to the left of the jewel we are looping through, and see if they match.
{
map[i][j].jewelsWest++;
if ( i !== 1 && map[i-1][j].name == map[i-2][j].name)
{
map[i][j].jewelsWest++;
}
}
I think recursion might me handy in this case:
map[i][j].jewelsWest = countMe(i, j, -1, 0);
map[i][j].jewelsEast = countMe(i, j, 1, 0);
map[i][j].jewelsNorth = countMe(i, j, 0, -1);
map[i][j].jewelsSouth = countMe(i, j, 0, 1);
private function countMe(x, y, xDiff, yDiff):int
{
if(map[x+xDiff] && map[x+xDiff][y+yDiff] && map[x][y].name == map[x+xDiff][y+yDiff].name)
{
return 1 + countMe(x+xDiff, y+yDiff, xDiff, yDiff);
}
else
{
return 0;
}
}
in the following code script for google Spreadsheets, I tried to make a program in which two pieces of information would be inputted to return a desired value that depends on BOTH values. Say, getValcharge ("OptionA", 2000) would return "76", or getValcharge ("OptionB",6000) would return 70. However, it seems to me that I keep getting returned the very last value possible: getValcharge("OptionA"/"OptionB"/"OptionC",1000) would return me "30". Even if I were to put an "OptionD" for the value, it would return "30" if the second number is under 5001.
Thus, it seems to only follow the second parameter --and thus only the second--even when closed off and is supposed to be not accessible to the first.
I am new to Script editor but do have modest Java experience (it'd work were this Java..) Could someone offer any advice/fixes? Any is appreciated. Thanks.
function getValcharge (valType, valAmount) {
var valcost =0;
if(valType="OptionA"){
if(valAmount < 5001)
{valcost = 76;}
if(valAmount > 5000 && valAmount <10001)
{valcost = 113;}
}
if(valType="OptionB"){
if(valAmount < 5001)
{valcost=43; }
if(valAmount > 5000 && valAmount <10001)
{valcost = 70;}
}
if(valType="OptionC")
{
if(valAmount < 5001)
{ valcost = 30; }
if(valAmount > 5000 && valAmount <10001)
{ valcost = 46; }
}
return valcost;
}
In Javascript you need to use a double-equals sign to test for equivalence, eg:
if(valType=="OptionA"){
I'm having a problem handling some numbers from a json request. Based on the results I'm trying to output some various bits of HTML. Specifically the problem is when I come to check whether a number is greater than -1, but less than 6. Code excerpt is as follows…
else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1) {
//Something else happens here
}
else if(parseInt(myvariable, 10) > 6) {
//This is where the third thing should happen, but doesn't?
}
It seems that despite the value being 7 or 70 the second 'else if' is as far as it gets.
Is there a way that I can check that the number is more than -1 but less than 6 so that it moves on to the next conditional statement.
I'm guessing that (like my previous question) there's a very simple answer so please excuse my naivety.
Thanks in advance.
Conditions are executed only until one is found to be true.
In other words, you need to re-jig their order or tighten them to make your current order work.
7 is above -1, so the second condition resolves to true. So for 7, the 3rd condition is never needed.
if(parseInt(myvariable, 10) < -1) {
//number is less than -1
}
else if(parseInt(myvariable, 10) > 6) {
//number is above 6
}
else {
//neither, so must be inbetween -1 an 6
}
The if condition is wrong. Let's think of this: myvariable is 7.
In your code will happen:
else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1) {
**// HERE THE CONDITION IS TRUE, BECAUSE 7 > -1**
}
else if(parseInt(myvariable, 10) > 6) {
// This is where the third thing should happen, but doesn't?
}
You can change it as
else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > 6) {
// This is where the third thing should happen, but doesn't?
}
else if(parseInt(myvariable, 10) > -1) {
// Moved
}
To make it work...
Another solution is to change the second line:
else if(parseInt(myvariable, 10) > -1)
to:
else if(parseInt(myvariable, 10) <= 6)
There are so many ways of writing this.
yes because any number you will write greater than -1 will never go throw the third block of code , it will go throw the second, as you said "the number is more than -1 but less than 6" you can simply do like this :
else if(parseInt(myvariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myvariable, 10) > -1 && parseInt(myvariable, 10) < 6) {
//Something else happens here
}
I guess you can do this easily do something like this:
considering your variable value is (7):
else if(parseInt(myVariable, 10) < -1) {
//this is where one thing happens
}
else if(parseInt(myVariable, 10) > -1) {
//now 'myVariable' is greater than -1, then let's check if it is greater than 6
if(parseInt(myVariable, 10) > 6) {
//this where what you should do if 'myVariable' greater than -1 AND greater than 6
}
}
What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.
Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}
Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.