Angularjs switch statement - angularjs

I am fetching few values from my backend and assigning then to my scope variables in angular controller. for one of the variables I get the values between 1 till 7 and depending on the which number it is I want to do something like following:
.success(function(response){
$scope.initial_data=response;
angular.forEach($scope.initial_data, function(item){
$scope.exercise_id=item.exercise_type;
alert($scope.exercise_id) // This gives me either 1 or any number till 7
switch($scope.exercise_id){
case '1':
alert("1");
break;
case '2':
alert("2");
break;
default:
alert("Default");
}
However, this piece of code for switch statement always alerts gives me Default. What am I doing wrong?

Related

C: handling case...else

What is the correct way to put an else clause in a switch statement? For example, instead of:
switch (input) {
case (1):
printf("yes!");
break
case (0):
printf("no!");
break;
default:
printf("invalid!");
}
To do something along the lines of (if possible):
switch (input) {
case (1):
printf("yes!");
case (0):
printf("no!");
else:
printf("invalid!");
}
I'm simply asking if there is a short-cut to not have to add a break after every case statement to act as-if it were the else part of an if statement.
The correct way is using default. Also ,you don't have to use break for non-default cases, only if you don't want execution to fall into the next case.

Node.js how to read and write arrays to file as binary

I have a big array that I need to read in from a file. Normally I would save it as JSON, but the size of the UTF-8 encoded file is substantially larger and I have very strict size requirements so every Kb is precious.
Here's what I have so far:
// https://www.npmjs.com/package/typedarray-to-buffer
var toBuffer = require('typedarray-to-buffer');
var buffer = toBuffer(myArray);
var wstream = fs.createWriteStream('./myArray');
wstream.write(buffer);
// later...
var buff = new Buffer(data); // <-- file data passed in as a buffer
myArray = buff; // <--still binary :-(
I'm following this helpful article about writing binary files but it doesn't cover how to read them back in. I think the part I'm stuck in is turning the buffer data back into an array.
Update
Here's the console.log() for the array before it's written to file:
Int32Array {
'0': 2107281600,
'1': -370226405,
'2': 274543611,
'3': 172775319,
'4': -1927927544,
'5': -248215383,
'6': -1295527238,
'7': -1774538531,
'8': -784581845,
'9': 651425656,
'10': -534521241,
'11': -1788883022,
'12': 1679049410,
'13': -1728518340,
...
It is actually very simple to write binary to file. Use the "binary" encoding type string for the writeFileSync method from FS. Like so :
// Assume you have an Int32Array called myArray
var fs = require('fs');
fs.writeFileSync('/tmp/test.bin', myArray, 'binary');
Now you can check that the size of the file matches the byte size of the typed array. For example, myArray.length*4 will be the size of your data which in my case is 1228624. The file system says :
$ ls -l /tmp/test.bin
-rw-rw-r-- 1 flatmax flatmax 1228624 May 5 17:58 /tmp/test.bin

angularjs $event get the same code for uppercase and lowercase for a key

i'm struggling to create hotkeys using angularjs $event. i need to simplify the codes for uppercase and lowercase. however i viewed from the console.log($event) they have different keyCode.
p is 112
P is 80
HTML:
<body ng-app="pointofsale" ng-controller="PointofSaleCtrl" ng-keypress="hotKeyEvent($event)">
Javascript:
$scope.hotKeyEvent = function(data){
console.log(data);
switch(data.keyCode){
case 112: // p
break;
case 80: // P
break;
default:
console.log('No key associated')
}
}
What am i missing here? i don't want to create two conditions for the same character but different case.
Appreciate any help, Thanks!
You could simplify your switch by grouping the case statements
like this:
switch(data.keyCode){
case 112: // p
case 80: // P
break;
default:
console.log('No key associated')
}
It's known as falling through - essentially, anything matched will continue to run until a break is hit.
Makes maintenance quite a bit easier too.

Is there a way to change local variables partway through switch case without exiting the switch statement?

I'm dealing with a rather large enumeration, which can conceptually be divided as representing four different categories. Currently I'm using this enumeration in a switch case statement in order to map values into four separate arrays (in line with the four different categories).
What I'm curious about is if it is possible to change a locally defined variable after arbitrary cases in the switch case statement. This would allow for the ability to break the switch statement into these four different sections, and value assignments that would occur for each case -- if equivalent -- can occur at these sections.
A simplified example what I'm going for is as follows:
Setup
enum incidental_indexes {
arr1_0, arr1_2, arr2_0, arr1_1, arr2_1
} indexes;
struct foobar{
int arr1[3];
int arr2[2];
}
enum indexes unknown_index = ???; // In my code there are two separate indexes being mapped
// from one another, so for the sake of example imagine that
// this index is unknown
enum indexes curr_index = arr1_1; //Value here does not matter
struct foobar my_struc;
int * curr_arr;
int mapped_index;
Brute force approach
switch(unknown_index){
case(arr1_0):
curr_arr = my_struc.arr_1; //First category array
curr_index = arr1_0;
break;
case(arr1_1):
curr_arr = my_struc.arr_1; //First category array, again
curr_index = arr1_1;
break;
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
curr_arr = my_struc.arr_2; //Second category array
break;
case(arr2_1):
curr_index = arr2_1;
curr_arr = my_struc.arr_2; //....
break;
}
Ideal Approach
switch(unknown_index){
default: //Notice no break.
curr_arr = my_struc.arr_1; //First category array
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
default: //Something like a second default, however disallowed
curr_arr = my_struc.arr_2; //Second category array
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}
The functional benefits are obviously nill, however I'm curious if this functionality even exists in C, or if there is perhaps a better method for executing this.
Thanks!
Switch statements only perform a single branch, so you can't jump around inside of the switch like that. What you can do however is group certain cases together without a break in between:
switch(curr_index){
case arr1_0:
case arr1_1:
case arr1_2:
curr_arr = my_struc.arr_1;
break;
case arr2_0:
case arr2_1:
curr_arr = my_struc.arr_2;
break;
}
EDIT:
For the index assignment part, you could do a second switch like this:
switch(unknown_index){
case arr1_0:
curr_index = arr1_0;
break;
case arr1_1:
curr_index = arr1_1;
break;
case arr1_2:
curr_index = arr1_2;
break;
case arr2_0:
curr_index = arr2_0;
break;
case arr2_1:
curr_index = arr2_1;
break;
}
But since you're always assigning whatever the value of unknown_index is, the above is the same as this:
curr_index = unknown_index;
One, no.
Two, just use ifs and elses. As the saying goes, when you have a hammer, everything looks like a nail. switch is a really weird "hammer" to try applying to everything.
Three, um, I guess you could use goto everywhere, but we decided this was a bad idea and creates horribly messes of code in the 80s or something.
Within a switch you have access to all the locally defined variables. I'm not quite sure I understand your question... it seems like what you're trying to do is best accomplished by 2 switches:
switch(unknown_index){
case(arr1_0):
case(arr1_1):
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
break;
case(arr2_0):
case(arr2_1):
curr_arr = my_struc.arr_2; //....
break;
}
switch(unknown_index){
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}

Using values returned by Meteor template helpers for other calculations [SOLVING WITH AGGREGATION]

I'm having trouble understanding how to run some simple calculations in a Meteor app.
The following helpers return three values (numerator, denominator and the result of dividing the two). The first two values currently come from a collection (will eventually come from an API feed) and are dependent on two values from a different collection (called using Template.parentData). The third value is not saved to the database. This sequence is repeated through a loop to display the three values in a table.
Template.ValuationTable.helpers({
selections: function () {
var selected = this.valuationSelections;
return Companies.find({ticker: {$in: selected}})
},
valuationNum: function() {
var valuationMetric = Template.parentData(1).valuationMetric;
switch (valuationMetric) {
case "EV/Revenue":
return this.enterpriseValue.toFixed(1);
break;
case "EV/EBITDA":
return this.enterpriseValue.toFixed(1);
break;
default:
return "";
}
},
valuationDen: function() {
var valuationMetric = Template.parentData(1).valuationMetric;
var valuationPeriod = Template.parentData(1).valuationPeriod;
switch (valuationMetric) {
case "EV/Revenue":
switch (valuationPeriod) {
case "LTM":
return this.ltm.revenue.toFixed(1);
break;
case "FY1":
return this.fy1.revenue.toFixed(1);
break;
case "FY2":
return this.fy2.revenue.toFixed(1);
break;
}
break;
case "EV/EBITDA":
switch (valuationPeriod) {
case "LTM":
return this.ltm.ebitda.toFixed(1);
break;
case "FY1":
return this.fy1.ebitda.toFixed(1);
break;
case "FY2":
return this.fy2.ebitda.toFixed(1);
break;
}
break;
default:
return "";
}
}
});
Template.registerHelper('divide', function(a, b) {
return (a / b).toFixed(1);
});
<template name="ValuationTable">
{{#each selections}}
<tr>
<td>
<a>${{valuationNum}}</a>
</td>
<td>
<a>${{valuationDen}}</a>
</td>
<td>
<a>{{divide valuationNum valuationDen}}x</a>
</td>
</tr>
{{/each}}
</template>
I need to then calculate the average and median of the series of third values, both to display on the page and to use as a data point for D3. I know I need these values in an array to calculate average and median, but I don't know how to "get" them from the helper results.
First off, should I instead be building the switch in an object (in the subscription perhaps?) and passing that into the helper? If so, any guidance would be appreciated.
Second, how do I get the third value to add to an array? Can I access it directly from the helper?
Thanks in advance, sorry if these are two different questions, I think they are closely related though.
EDIT:
I realized, per Michel's suggestion of meteorhacks:aggregate, that I was thinking about this incorrectly and that aggregation is needed, not helpers.
It's impractical to perform aggregation operations in helper functions that deal with individual documents. You may find a package such as meteorhacks:aggregate to implement aggregation functions.
Why is it impractical? For starters, due to reactivity any value and even the number of documents can change at any moment. You can't really perform aggregation as the documents are displayed in functions that have a scope of one document.

Resources