I'm working on a simple function that should return the quantity of specific elements in a passed range. I would like to use SWITCH statement but for some reason it doesn't work as I would expect:
function groupResult(range) {
const resultsQuantity = {
na: 0,
fail: 0,
pass: 0,
empty: 0
}
for(let i of range) {
switch(i){
case 'N/A':
resultsQuantity.na++;
break;
case "FAIL":
resultsQuantity.fail++;
break;
case "PASS":
resultsQuantity.pass++;
break;
default:
resultsQuantity.empty++
}
}
return resultsQuantity.na;
}
the function call in the spreadsheet looks like follows:
call of the function in the spreadsheet
but as result I get "0" instead of expected "2"
Use
for(let i of range.flat())
Because the range is a 2d array.
Related
So I have an object build out that has a bunch of methods inside, I want to be able to narrow down some methods and see if there are other ways to do thing, so I will go into more description below:
So I have the following method which returns me a bunch of class names:
function class_names() {
return [
'optanon-category-C0001',
'optanon-category-C0002',
'optanon-category-C0003',
'optanon-category-C0004',
'optanon-category-C0005'
];
}
Now I have another method which basically outputs me a string based on the classname passed inside the parameter:
function classname_output(class_name) {
let output = '';
switch (class_name) {
case 'optanon-category-C0001':
output = 'Strictly Necessary Cookies';
break;
case 'optanon-category-C0002':
output = 'Performance Cookies';
break;
case 'optanon-category-C0003':
output = 'Functional Cookies';
break;
case 'optanon-category-C0004':
output = 'Targeting Cookies';
break;
case 'optanon-category-C0005':
output = 'Social Media Cookies';
break;
default:
output = 'No cookies match the specified class.';
break;
}
return output;
}
Is there a way that I can infuse the two methods into a single method with an object return and then target the object key?
You can have object (dictionary) that maps class names (key) to string (value), and then have a function to return the value if the key exists in the dictionary, or a default "Doesn't exist" string if it doesn't.
const dict = {
'optanon-category-C0001': 'Strictly Necessary Cookies',
'optanon-category-C0002': 'Performance Cookies',
'optanon-category-C0003': 'Functional Cookies',
'optanon-category-C0004': 'Targeting Cookies',
'optanon-category-C0005': 'Social Media Cookies'
};
function check(dict, className) {
return dict[className] ?? 'No cookies match the specified class.';
}
console.log(check(dict, 'optanon-category-C0003'));
console.log(check(dict, 'optanon-category-C0005'));
console.log(check(dict, 'optanon-category-C0000'));
Additional documentation
Nullish coalescing operator
I'm using Cheerp (https://www.leaningtech.com/cheerp/) to transpile some C++ code into JavaScript.
Is there any option to preserve variable names? Looks like the names get always mangled
Original C++ code:
void myClass::myMethod(int32_T myParam, boolean_T *rty_Result)
{
switch (myParam) {
case Mycase1:
case Mycase2:
case Mycase3:
case Mycase4:
case Mycase5:
*rty_Result = true;
break;
case Mycase6:
*rty_Result = (filter.field1.field2 == 1);
break;
default:
*rty_Result = false;
break;
}
}
Output from Cheerp:
function __ZN8JsBridge12AvailabilityEP9bFilter_Ti(Lthis,filter,myParam){
var tmp0=0;
switch(myParam|0){
case 5:
{
tmp0=filter.a3.i2|0;
return (((tmp0|0)===1?1:0)?1:0)|0;
break;
}
case 1:
case 2:
case 4:
case 6:
case 3:
{
return 1|0;
break;
}
default:{
return 0|0;
break;
}
}
}
I don't find any options in the documentation:
https://github.com/leaningtech/cheerp-meta/wiki
You can try to pass the option:
-cheerp-pretty-code
Source: https://github.com/leaningtech/cheerp-meta/wiki/JavaScript-interoperability#clobbering-names
If that doesn't work, then I'm quite sure that this just unfortunately cannot be accomplished.
I have an object called allInvalidFields which lists invalid fields under an identifier e.g _0 or _3
The object could look like this
allInvalidFields = {
"_0" : {
0: input.foo,
1: select.la
}
"_1" : {
0: input.foofoo,
1: select.lala
}
}
But equally it could not have _0 as the first key and could look like this:
allInvalidFields = {
"_1" : {
0: input.alice,
1: select.bob
}
"_3" : {
0: input.foo
}
}
How can I get the first value from the first object in the list? So in the example above it would be input.foo or input.alice depending on which dataset was being searched.
Inferred that the keys are in numeric order and prepended with "_".
Under these assumptions :
function first(obj,n){
let smallest = Infinity;
for(i in obj){
console.log(i);
let val='';
if (i.toString()[0]=='_'){
val = parseInt(i.toString().substring(1));
}else{
val = i;
}
if( val < smallest ){
smallest = val;
}
}
if(n>0){
return first(obj["_"+smallest],n-1);
}
return obj[smallest];
}
first({"_0" : {
0: "input.foo",
1: "select.la"
},
"_1" : {
0: "input.foofoo",
1: "select.lala"
}},1); // input.foo
You need something like this.
var allInvalidFields = {
"_0": {
"0": "input.foo",
"1": "select.la"
},
"_1": {
"0": "input.foofoo",
"1": "select.lala"
}
};
var firstInput = Object.keys(allInvalidFields)[0];
console.log(allInvalidFields[firstInput][0]);
Object.values(allInvalidFields)[0][0];
take the first value of the first property object.
Small note: having such keys like _1 is bad style...
Short version of programaths answer:
allInvalidFields["_"+Object.keys(allInvalidFields).map(el=>el=+el.replace("_","")).sort()[0]][0]
I'm learning JavaScript objects and have one question regarding the code below: the object "cashRegister" has only 1 method "add()". Then, outside the object there is another method "scan()". How is it possible at the end of the code to invoke "cashRegister.scan()" if the "scan" method does not belong to the "cachRegister" object? Is it because "scan()" uses "add()" method which belongs to "cashRegister" and that usage makes "scan()" the method of "cashRegister" or what?
var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item, quantity) {
switch (item) {
case "A": this.add(0.98 * quantity); break;
case "B": this.add(1.23 * quantity); break;
case "C": this.add(4.99 * quantity); break;
case "D": this.add(0.45 * quantity); break;
}
}
};
// scan each item 4 times
cashRegister.scan("A", 4);
cashRegister.scan("B", 2);
cashRegister.scan("C", 4);
cashRegister.scan("D", 3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
In the given code scan method is actually inside the cashRegister object and as this is a method of object "cashRegister", we can invoke like "cashRegister.scan()". Otherwise this will give error. And in the given code error will come on calling "this.add()" method also if "scan()" method will be outside the cashRegister object.
In your code, the scan method is actually inside the cashRegister object.
scan method belongs to cashregister.
Here's the code reformated a bit, so you can see it:
var cashRegister =
{
total:0,
add: function(itemCost)
{
this.total += itemCost;
},
scan: function(item, quantity)
{
switch (item)
{
case "A": this.add(0.98 * quantity); break;
case "B": this.add(1.23 * quantity); break;
case "C": this.add(4.99 * quantity); break;
case "D": this.add(0.45 * quantity); break;
}
}
};
// scan each item 4 times
cashRegister.scan("A", 4);
cashRegister.scan("B", 2);
cashRegister.scan("C", 4);
cashRegister.scan("D", 3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
I'm looking for the best solution here, i've got an idea but thinking it could be done prettier.
I'm making an simple weather application. And i'm using Yahoo Weather api were they have got codes for weather conditions.
Depending on the condition i'm giving a code. Now, there are 50 codes and i've categorised them into 5 categories. In my case ex. my categori Snow contains 15 of Yahoo's condition codes.
Well, if you got a better idea (which i bet there is) be free to suggest.
My thought is to return the matching value from a set of arrays, but not shure how to do it.
My code now looks like this:
function getCondition(code) {
var snow = [1, 2, 3],
sun = [4, 5, 6];
}
What i need is the variable name that contains the matching number of the code?
I've made a JS-Fiddle
http://jsfiddle.net/BH8r6/
The fastest lookup (translating a Yahoo code to your label) is to use the code as array key (if they are sequential).
var weather = [];
weather[0] = "no_weather";
weather[1] = "snow";
weather[2] = "snow";
weather[3] = "snow";
weather[4] = "sun";
weather[5] = "sun";
weather[6] = "sun";
function getCondition(code) {
return weather[code];
}
Why dont you try an associative array when your key is your variable name and your values is the corresponding code for the variable name, thus your code will be something like this:
var myCodeArray=[];
myCodeArray["snow"]=[1, 2, 3];
myCodeArray["sun"] = [4, 5, 6];
now your method getCondition will be
function getCondition(code)
{
for(var definedCodeName in myCodeArray)
{
if(myCodeArray.hasOwnProperty(definedCodeName))
{
var array=myCodeArray[definedCodeName ];
for(var i=0;i<array.length;i++)
{
if(array[i]==code){
return definedCodeName ;}
}
}
}
return "Not found";
}
Demo
Why to complicate everything?! Just use 'switch' :
function getCondition(code) {
switch( code ){
case 1:
case 2:
case 4:
case 6:
return "snow";
case 3:
case 8:
case 9:
return "sun";
case 5:
case 7:
case 10:
return "cloudy";
}
return "none";
}