This question already has answers here:
Consecutive Number After 16 in iOS
(4 answers)
Closed 9 years ago.
i have an integer called count , and i want it when it comes 1 or 17 or 33 to forever by the same sequence i.e count += 16
the first ball be unhidden , and the same thing when count comes to 2 or 18 or 34 to forever by the same sequence i.e count += 16 , the second ball to unhidden
at first i typed this code but it is very hard to type all numbers with their increments to the end in if condition
if(count == 1 || count == 17 || count == 33 || count == 49 || count == 65 || count == 81 || count == 97 || count == 113 || count == 129 || count == 145 || count == 161 || count == 177 || count == 193 || count == 209 || count == 225 || count == 241)
{
_firstBall.hidden = NO;
}
if(count == 2 || count == 18 || count == 34 || count == 50 || count == 66 || count == 82 || count == 98 || count == 114 || count == 130 || count == 146 || count == 162 || count == 178 || count == 194 || count == 210 || count == 226 || count == 242)
{
_secondBall.hidden = NO;
}
Try this:
if( ((count-1) % 16) == 0) {
_first.hidden = NO;
}
if( ((count-2) % 16) == 0) {
_secondBall.hidden = NO;
}
The % operator returns the integer remainder (or modulus). Your first sequence: 1, 17, 33 is 16 * n + 1 so the remainder when divided by 16 is 1. Similarly your second second sequence is 16 * n + 2.
So you can:
switch (count % 16) // switch on the remainder of division by 16
{
case 1:
_firstBall.hidden = NO; break;
case 2:
_secondBall.hidden = NO; break;
// add more cases if needed
}
Related
In what way can I refactor nested if statements like this? A condition is met, however there are some exceptions to the condition. I recognize that this starts to approach the Arrow Anti-Pattern.
Specifically, are there any dart language features that can help me refactor or re-write this code to be more clear?
Let's use "leap years" as an example:
// on every year that is evenly divisible by 4
// except every year that is evenly divisible by 100
// unless the year is also evenly divisible by 400
bool isLeapYear(int year) {
assert(!year.isNegative);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
return false;
}
return true;
}
return false;
}
You are combining boolean checks with boolean returns.
That probably means you can do it all in one expression.
I'd do:
bool isLeapYear(int year) =>
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
(Now, if you want to optimize, then modulo, %, is more expensive than bitwise and, &, so you can also do:
bool isLeapYear(int year) =>
year & 3 == 0 && (year & 15 == 0 || year % 100 != 0);
which should be slightly more efficient. Doing & 15, aka % 16, is sufficient to detecting being a multiple of 400 if you know the number is a multiple of 100.)
For the general case, you can reverse your if branches and return early:
bool isLeapYear(int year) {
if (year % 4 != 0) return false; // Not multiple of 4.
// Year is a multiple of 4.
if (year % 100 != 0) return true; // Not multiple of 100.
// Year is a multiple 100.
return year % 400 == 0;
}
(Noticing that
if (year % 400 == 0) {
return true;
}
return false;
is the same as return year % 400 == 0;.)
I want to calculate the total duration a gif plays. It can be either duration of a gif or frame count of the gif. Have tried using FLAnimatedImage, SDWebImage and YYImage but can't really attain what I am looking for. The gif is loaded from remote url and then I want to calculate the duration it plays.
This is the function that returns the total duration in GIF time units (1 unit = 10 msec).
data is a pointer to GIF data, size is its size.
long Duration(uint8_t *data, long size) {
long desc, time = 0;
uint8_t *buff;
if ((size > 13) && data && (data[0] == 71) && (data[1] == 73)
&& (data[2] == 70) && (data[3] == 56) && (data[5] == 97)
&& ((data[4] == 55) || (data[4] == 57))) {
buff = data + 13 + ((data[10] & 0x80)? 6 << (data[10] & 7) : 0);
if ((size -= buff - data) > 0)
while ((desc = *buff++) != 0x3B) {
size--;
if (desc == 0x2C) {
desc = 9 + ((buff[8] & 0x80)? 6 << (buff[8] & 7) : 0);
buff += desc;
if ((size -= desc) <= 0)
break;
}
else if ((desc == 0x21) && (*buff == 0xF9))
time += *(uint16_t*)(buff + 3);
buff++;
if (--size <= 0)
break;
do {
buff += (desc = 1 + *buff);
if ((size -= desc) <= 0)
return time;
} while (desc > 1);
}
}
return time;
}
This function parses GIF images by hand, extracting frame delay information and summing it.
After looking at many solutions, I got the following solutions that does exactly what I want.
SOLUTION 1 : works well except it does not work in IE(11)
I will much appreciate if someone can help me out fixing this for IE.
code taken from :https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement
function checkRows(oField, oKeyEvent) {
var nKey = (oKeyEvent || /* old IE */ window.event || /* check is not supported! */ { keyCode: 38 }).keyCode,
// put here the maximum number of characters per line:
nCols = 30,
// put here the maximum number of lines:
nRows = 5,
nSelS = oField.selectionStart, nSelE = oField.selectionEnd,
sVal = oField.value, nLen = sVal.length,
nBackward = nSelS >= nCols ? nSelS - nCols : 0,
nDeltaForw = sVal.substring(nBackward, nSelS).search(new RegExp("\\n(?!.{0," + String(nCols - 2) + "}\\n)")) + 1,
nRowStart = nBackward + nDeltaForw,
aReturns = (sVal.substring(0, nSelS) + sVal.substring(nSelE, sVal.length)).match(/\n/g),
nRowEnd = nSelE + nRowStart + nCols - nSelS,
sRow = sVal.substring(nRowStart, nSelS) + sVal.substring(nSelE, nRowEnd > nLen ? nLen : nRowEnd),
bKeepCols = nKey === 13 || nLen + 1 < nCols || /\n/.test(sRow) || ((nRowStart === 0 || nDeltaForw > 0 || nKey > 0) && (sRow.length < nCols || (nKey > 0 && (nLen === nRowEnd || sVal.charAt(nRowEnd) === "\n"))));
return (nKey !== 13 || (aReturns ? aReturns.length + 1 : 1) < nRows) && ((nKey > 32 && nKey < 41) || bKeepCols);
}
<form>
<p>Textarea with fixed number of characters per line:<br />
<textarea cols="50" rows="10" onkeypress="return checkRows(this, event);" onpaste="return false;" /></textarea></p>
</form>
SOLUTION 2
It works in IE but it fails in when editing the lines. You type a line, go back using left arrow keys and type you can type 1 letter and the cursor goes back to the end.
code taken from : http://cgodmer.com/?p=55 that
//limit # of lines of a text area and length of those lines
//<textarea rows="4" chars="40" onkeyup="limitTextareaLine(this, event)" ></textarea>
//Author: CGodmer (Feb 22, 2012)
function limitTextareaLine(x, e, nRows, nChars) {
var rows = $(x).val().split("\n").length; //number of rows
var lineCharLimit = nChars; //number of characters to limit each row to
var rowLimit = nRows; //number of rows to allow
//limit length of lines
for (var i = 0; i < rows; i++) {
var rowLength = $(x).val().split("\n")[i].length;
//check to see if any of the rows have a length greater than the limit
if (rowLength > lineCharLimit) {
//if it does save the beg index of the row
var rowstartindex = $(x).val().indexOf($(x).val().split("\n")[i]);
//use the index to get a new value w/ first lineCharLimit number of characters
var newval = $(x).val().substring(0, rowstartindex + lineCharLimit)
+ $(x).val().substring(rowstartindex + rowLength, $(x).val().length);
//replace that value in the textarea
$(x).val(newval);
//set character position back to end of the modified row
setCaretPosition($(x)[0], rowstartindex + lineCharLimit);
}
}
//limit # of lines to limit to is rows attribute
while($(x).val().split("\n").length > rowLimit) {
$(x).val($(x).val().substring(0, $(x).val().length - $(x).val().split("\n")[rowLimit].length - 1));
}
}
//Set caret position in the supplied control to position
//From: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
function setCaretPosition(ctrl, pos) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos, pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows="5" cols="10" onkeyup="limitTextareaLine(this, event, 5, 10)" ></textarea>
Try this one, it may solve this problem.
function ValidationAddress() {
var allText;
allText = document.getElementById("<%= txtAdd1.ClientID %>").value;
allText = document.getElementById('<%=txtAdd1.ClientID%>').value;
var A = allText.split('\n');
var L = A.length;
if (L > 3 && event.keyCode != 8 && event.keyCode != 46) {
alert("You have exceeded maximum limit.Cannot insert more than 3 lines.");
valert = false;
return false;
}
var arr = allText.split("\n");
for (var i = 0; i < arr.length; i++) {
if (arr[i].length > 10) {
alert("You have exceeded the Maximum Limit..Characters per line is 70 in Address Field !");
valert = false;
return false;
}
}
}
I have a grid of buttons 11 buttons x 7 buttons. I need to check the tags of the buttons that are not on the outside edge of the grid. My current solution is to exclude the tags that are on the outside edge. The buttons are in an outlet collection. So the tags I need to exclude are 0-10, 21, 32, 43, 54, 65, 76, 0, 11, 22, 33, 44, 55, 66-76.
How do you check if an item in an if-statement, that's inside of a for-loop, matches multiple numbers? You can see the code below will become a mess if I play it out with 28 different || conditions. The numbers I'm trying to match are not in sequence.
for item in buttonOutlets {
if item.tag != 0 || item.tag != 1 || item.tag != 2 {
var tag = item.tag
var tagMinusOne: Int? = Int(item.tag) - 1
var tagMinusTen: Int? = Int(item.tag) - 10
var tagMinusEleven: Int? = Int(item.tag) - 11
var tagMinusTwelve: Int? = Int(item.tag) - 12
var titleLabel = item.titleLabel?.text
var minusTwelve: String? = buttonOutlets[tagMinusTwelve!].titleLabel?.text!
var minusOne: String? = buttonOutlets[tagMinusOne!].titleLabel?.text!
}
}
Here is what works but is a mess. Note that the tags are not in a perfect range. Updated code:
func checkForMatchingCells() {
for item in buttonOutlets {
var tag = item.tag as Int
if tag != 0
&& tag != 1
&& tag != 2
&& tag != 3
&& tag != 4
&& tag != 5
&& tag != 6
&& tag != 7
&& tag != 8
&& tag != 9
&& tag != 10
&& tag != 11
&& tag != 12
&& tag != 33
&& tag != 44
&& tag != 55
&& tag != 21
&& tag != 32
&& tag != 43
&& tag != 54
&& tag != 65
&& tag != 66
&& tag != 67
&& tag != 68
&& tag != 69
&& tag != 70
&& tag != 71
&& tag != 72
&& tag != 73
&& tag != 74
&& tag != 75
&& tag != 76 {
println(tag)
var tagMinusOne: Int? = Int(item.tag) - 1
var tagMinusTen: Int? = Int(item.tag) - 10
var tagMinusEleven: Int? = Int(item.tag) - 11
var tagMinusTwelve: Int? = Int(item.tag) - 12
var titleLabel = item.titleLabel?.text
var minusTwelve: String? = buttonOutlets[tagMinusTwelve!].titleLabel?.text
var minusOne: String? = buttonOutlets[tagMinusOne!].titleLabel?.text
println(minusOne)
}
}
I can think of two ways. First is with ClosedIntervals:
(0...10).contains(3) // true
Or if you have more complex numbers, you can use contains methods on other CollectionTypes:
Set([2, 5, 8]).contains(5) // true
Although I'm not sure what you're trying to do. (The statement x != 0 || x != 1 || x != 2 will return true for every number, for example. Maybe you meant x != 0 && x != 1 && x != 2?)
With your edit, the most effecient solution is to create a Set of things you want to exclude:
var toExclude: Set = [21, 32, 43, 54, 65, 76, 0, 11, 22, 33, 44, 55]
toExclude.unionInPlace(0...10)
toExclude.unionInPlace(66...76)
And then the condition in your if statement would be:
if !toExclude.contains(item.tag) {...
In the rendered view, if a user enters a value such as "1.0" or "1.2" instead of "1" then UpdateModel throws an exception if the property in question is an integer.
Is there a decent fix for this?
You could simply block the user from entering a decimal point (or letters also in the code I provided) in the text box. I use the below code for that purpose.
$(document).ready(function() {
$("#txtboxToFilter").keydown(function(event) {
// Allow: backspace, delete, tab, escape, and enter
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// Ensure that it is a number and stop the keypress if it is not
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
event.preventDefault();
}
}
});
});