Why in console even numbers gives empty list? - dart

void main() {
List<int> numbers = [55,58,62,15,14,19,20];
List oddNumbers = [];
List evenNumbers = [];
for (int index = 0; index < numbers.length; index++) {
print(numbers[index]);
if(numbers[index]% 2 !=0) {
oddNumbers.add(numbers[index]);
} else if(numbers[index]%2 != 0) {
evenNumbers.add(numbers[index]);
}
}
print("Odd numbers:$oddNumbers");
print("Even numbers:$evenNumbers");
}
it gives:
Odd numbers:[55, 15, 19]
Even numbers:[]
Why the even numbers list is empty?

Hi You're using same logic in both if and else conditions. Please check below snippet.
if(numbers[index]% 2 !=0) {
oddNumbers.add(numbers[index]); } else
if(numbers[index]%2 == 0) {
evenNumbers.add(numbers[index]); } }

for (int index = 0; index < numbers.length; index++) {
if(numbers[index]% 2 !=0) {
oddNumbers.add(numbers[index]);
} else if(numbers[index]% 2 != 1) {
evenNumbers.add(numbers[index]);
}
}
print("Odd numbers:$oddNumbers");
print("Even numbers:$evenNumbers");

Related

Getting Illegal Instruction:4

I was trying to do this function in C but for some reason is giving me "Illegal instruction: 4"... From what I looked up it may be because I'm using iOs, but still I'm using VSCode and not a project so I have no idea how to correct it.
The function is the following:
void divideSocios(void){
int i;
for (i = 0; i < MAXFILA || socios[i].next != -1; i++){
if (socios[i].sc.pago >= 50){
if (semDiv[0].next == 0){
semDiv[0].next = -1;
semDiv[0].sc.id = socios[i].sc.id;
semDiv[0].sc.pago = socios[i].sc.pago;
strcpy(semDiv[0].sc.nome, socios[i].sc.nome);
}
else{
for (; i < MAXFILA && semDiv[i].next != -1; i++);
semDiv[i-1].next = i;
semDiv[i].next = -1;
semDiv[i].sc.pago = socios[i].sc.pago;
semDiv[i].sc.id = socios[i].sc.id;
strcpy(semDiv[i].sc.nome, socios[i].sc.nome);
}
} else {
if (comDiv[0].next == 0){
comDiv[0].next = -1;
comDiv[0].sc.id = socios[i].sc.id;
comDiv[0].sc.pago = socios[i].sc.pago;
strcpy(comDiv[0].sc.nome, socios[i].sc.nome);
}
else{
for (; i < MAXFILA && comDiv[i].next != -1; i++);
comDiv[i-1].next = i;
comDiv[i].next = -1;
comDiv[i].sc.pago = socios[i].sc.pago;
comDiv[i].sc.id = socios[i].sc.id;
strcpy(comDiv[i].sc.nome, socios[i].sc.nome);
}
}
}
}
I basically have 3 different linked lists by matrix, each node with a struct and the index of the current node, and depending on the "pago" atribute from the struct of the node I want to separate them from the original list between the other 2.

Z sorting multiple nodes in Objective-C

I'm trying to build a game in iOS and am currently using Objective-C. The problem I'm running into is Z-sorting different enemy and player nodes. This is my current code for Z-sorting just one enemy type, and I am only copying this code to z-sort every different type of enemy (Mob). This is leading to ALOT of code that all does essentially the same thing for different objects. The only way I know how to fix this problem is by using template functions, but Objective-C doesn't support that to my knowledge. There has to be a better way...
// If two monsters are standing on the same cell, we need to z sort them based on position
// south-western most body will be closest to the "camera" from players perspective
-(void) zSortMobs {
int i = 0;
int j = 0;
for (i = 0; i < self.maxMobs; i++) {
Mob * mob = [self.mobs objectAtIndex: i];
if (mob.state != 0) {
for (j = 0; j < self.maxMobs; j++) {
Mob * mob2 = [self.mobs objectAtIndex: j];
if (mob2.state != 0) {
if (mob != mob2) {
if (mob.z == mob2.z) {
if (mob.y < mob2.y) {
mob.z++;
}
else if (mob.y == mob2.y) {
if (mob.x < mob2.x) {
mob.z++;
}
else {
mob2.z++;
}
}
else {
mob2.z++;
}
}
}
[mob setZPosition: mob.z];
[mob2 setZPosition: mob2.z];
}
}
for (j = 0; j < self.maxTanks; j++) {
Tank * tank = [self.tanks objectAtIndex: j];
if (tank.state != 0) {
if (mob.z == tank.z) {
if (mob.y < tank.y) {
mob.z++;
}
else if (mob.y == tank.y) {
if (mob.x < tank.x) {
mob.z++;
}
else {
tank.z++;
}
}
else {
tank.z++;
}
}
[mob setZPosition: mob.z];
[tank setZPosition: tank.z];
}
}
if (mob.z == self.gameScene.player.z) {
if (mob.y < self.gameScene.player.y) {
mob.z++;
}
else if (mob.y == self.gameScene.player.y) {
if (mob.x < self.gameScene.player.x) {
mob.z++;
}
else {
self.gameScene.player.z++;
}
}
else {
self.gameScene.player.z++;
}
}
[mob setZPosition: mob.z];
[self.gameScene.player setZPosition: self.gameScene.player.z];
}
}
}

MVC Survey and capturing data

Im kinda new to MVC and have built a site with a risk calculator.
This all works fine the site works for users logging in and the form calculates the form then sends the user to the correct page.
When a user signs in with account and fills out the survey i want to capture the score on the database but cannot work out how/where to do this. Should i be writing the code in the behind file for the survey form?
here is the script
<script type="text/javascript">
function test_it(entry) {
if (entry.value != null && entry.value.length != 0) {
entry.value = "" + eval(entry.value);
}
computeForm(entry.form);
}
function computeForm(form) {
var total = 0
for (var count = 0; count < 5; count++) {
if (form.a[count].checked) {
var total = total + parseInt(form.a[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.b[count].checked) {
var total = total + parseInt(form.b[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.c[count].checked) {
var total = total + parseInt(form.c[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.d[count].checked) {
var total = total + parseInt(form.d[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.e[count].checked) {
var total = total + parseInt(form.e[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.f[count].checked) {
var total = total + parseInt(form.f[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.g[count].checked) {
var total = total + parseInt(form.g[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.h[count].checked) {
var total = total + parseInt(form.h[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.i[count].checked) {
var total = total + parseInt(form.i[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.j[count].checked) {
var total = total + parseInt(form.j[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.k[count].checked) {
var total = total + parseInt(form.k[count].value);
}
}
for (var count = 0; count < 5; count++) {
if (form.l[count].checked) {
var total = total + parseInt(form.l[count].value);
}
}
if (total <= 5) { window.location = "End_Page_1" }
else if (total <= 12) { window.location = "End_Page_2" }
else if (total <= 24) { window.location = "End_Page_3" }
else if (total <= 30) { window.location = "End_Page_4" }
else if (total <= 48) { window.location = "End_Page_5" }
}
</script>
Any ideas how i could do this would be much appreciated.
Andy
Suppose you have a Model
public class Calculator
{
public string Field1 {get;set;}
public string Field2 {get;set;}
public string Field3 {get;set;}
...
}
write a controller which would just send a empty model to the view. Like
public ActionResult FillSurvey()
{
Calculator calci = new Calculator();
return View(calci);
}
now right click this action and choose add view here choose strongly typed view. Choose your Model Calculator and also choose scffold template. choose create. A new View will be generated where in you can enter values and write one more method/Action in controller.Like
[HTTPPOST]
public ActionResult FillSurvey(Calculator calci)
{
//do whatever you want here. You will have all the values entered in UI
//captured in this method
}

Javascript - getElementID from scratch using BFS?

I'm trying to learn javascript, and spent tonight writing a getElementByID() function using Breadth-First Search. In short: I'm lost.
Fiddle: http://jsfiddle.net/timdown/a2Fm6/
Code:
var nodes = [];
function getElementById(node, id) {
alert(nodes.length);
if (node.childNodes[i].id == id) {
return node.childNodes[i];
} else if (node.childNodes[i].length > 0) {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
nodes.push(node.childNodes[i]);
}
}
if (nodes.length > 0) {
getElementById(nodes[0], id);
}
}
var el = getElementById(document.body, 'id');
Any help?
You're missing a for loop in the top half of your code. Where is i defined?
Here's how I'd write it:
function getElementById(node, id) {
//An array of all the nodes at the same depth
var nodes = [node];
//While the array is not empty
while(nodes.length) {
var newNodes = [];
for(var i = 0; i < nodes.length; i++) {
var children = nodes[i].childNodes;
for(var j = 0; j < children.length; j++) {
var child = children[j];
if(child.id == id) {
return child
}
newNodes.push(child);
}
}
//Replace nodes with an array of the nodes the next level down
nodes = newNodes
}
}

IndexOutOfBoundsException when updating a contact in contact list - Blackberry

Software and Simulator version i am using
Blackberry Smartphone simulator: 2.13.0.65
Blackberry software version 5.0.0_5.0.0.14
I am looking at modifying contacts. Below is the code snippet i am using.
I am getting a IndexOutOfBounds Exception at line
String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]);
Can someone advise what is going wrong here. Following is the code snippet
.....
// Load the addressbook and let the user choose from list of contact
BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
PIMItem pimItem = contactList.choose();
BlackBerryContact blackBerryContact = (BlackBerryContact)pimItem;
PIMList pimList = blackBerryContact.getPIMList();
// get the supported attributes for Contact.TEL
int[] supportedAttributes = pimList.getSupportedAttributes(Contact.TEL);
Dialog.alert("Supported Attributes "+supportedAttributes.length); // gives me 8
for (int i=0; i < supportedAttributes.length;i++){
if(blackBerryContact.ATTR_WORK == supportedAttributes[i]){
Dialog.alert("updating Work"); // This alert is shown
Dialog.alert("is supported "+ pimList.isSupportedAttribute(BlackBerryContact.TEL, supportedAttributes[i])+" "+pimList.getAttributeLabel(supportedAttributes[i])); // shows true and work
String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]); // I get a IndexOutOfBounds Exception here
if(wtel != ""){
pimItem.removeValue(BlackBerryContact.TEL, supportedAttributes[i]);
}
pimItem.addString( Contact.TEL, BlackBerryContact.ATTR_WORK, number); // passing the number that has to be updated
if(pimItem.isModified()) {
pimItem.commit();
Dialog.alert("Updated Work Number");
}
}
}
.....
I want to update all the supported attributes for Contact.TEL field
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/blackberry/api/pdap/BlackBerryContact.html
Field Values Per Field Supported Attributes
-----------------------------------------------------------------------------
Contact.TEL 8 Contact.ATTR_WORK, Contact.ATTR_HOME,
Contact.ATTR_MOBILE, Contact.ATTR_PAGER,
Contact.ATTR_FAX, Contact.ATTR_OTHER,
Contact.ATTR_HOME2, Contact.ATTR_WORK2
Reading contact number.
int number = contact.countValues(BlackBerryContact.TEL);
Hashtable multipleContactNumbers = new Hashtable();
for (int i = 0; i < number; i++) {
if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_WORK) {
multipleContactNumbers.put("Work: ", contact.getString(
BlackBerryContact.TEL, i));
} else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_WORK2) {
multipleContactNumbers.put("Work 2: ", contact.getString(
BlackBerryContact.TEL, i));
} else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_HOME) {
multipleContactNumbers.put("Home: ", contact.getString(
BlackBerryContact.TEL, i));
} else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_HOME2) {
multipleContactNumbers.put("Home 2: ", contact.getString(
BlackBerryContact.TEL, i));
} else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_MOBILE) {
multipleContactNumbers.put("Mobile: ", contact.getString(
BlackBerryContact.TEL, i));
} else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_OTHER) {
multipleContactNumbers.put("Other: ", contact.getString(
BlackBerryContact.TEL, i));
}
}
Add new contact numbers.
contact.addString(Contact.TEL, Contact.ATTR_HOME, "5555550100");
contact.addString(Contact.TEL, Contact.ATTR_WORK, "5555550103");
contact.addString(Contact.TEL, BlackBerryContact.ATTR_WORK2, "5555550104");
update contact numbers.
int telCount = contact.countValues(Contact.TEL);
for (int i = 0; i < telCount; ++i)
{
int telAttrs = contact.getAttributes(Contact.TEL, i);
if ((telAttrs & Contact.ATTR_MOBILE) != 0)
{
contact.setString(Contact.TEL, i, Contact.ATTR_MOBILE, "5555550109");
break;
}
}

Resources