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);
Related
how can i extract the first number from this array?
The number is taken from the variable and then divided, from 0222 to [ 0, 2, 2, 2 ]
I need to check the first number, but I can't.
thank you
var cellF9 = sh.getRange(9, 6, 1, 1).getValue();
String(cellF9).split("").map(Number);
function splitNum(num) {
return String(num).split("").map(Number);
}
console.log(splitNum(cellF9));
switch (cellF9[0]) {
case 0:
sh.getRange("F18").setValue("test");
break;
default:
SpreadsheetApp.getUi().alert(".......", ".............", SpreadsheetApp.getUi().ButtonSet.OK);
}
In switch (cellF9[0]) { you are indexing against the original string "0222". You can either do
switch (splitNum(cellF9[0])) {
or
const splittedNumber = splitNum(cellF9[0])
switch (splittedNumber) {
A solution without splitting it into an array:
const cell = '0222'
console.log(cell.charAt(0))
let array = [1,2,3,4]
const firstElement = array[0]
Have you tried just accessing it?
solved thanks :)
var arrayf9=splitNum(cellF9);
switch (arrayf9[0]) {
..........
..........
looking for a way to automatically update a cell value when a record is created.
I use Airtable, and I would like to automatically assign a developer to the newest record.
Ex. I have 3 developer, christophe/thomas/hugo
Using basic algorithm, but the issue is that when there is a new Biz Dev, we have to manually add him to the arrayBizDev and add a new switch case:
`let arrayBizDev = ["Christophe", "Thomas", "Hugo"]
let nbBizDev = arrayBizDev.length;
let bizDev = "";
switch (nbRdvPris % nbBizDev) {
case 0:
bizDev = "Christophe";
break;
case 1:
bizDev = "Thomas";
break;
case 2:
bizDev = "Hugo";
break;
default:
console.log(erreur);
}
output.set('Business Developper', bizDev);`
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.
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 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";
}