I am new to javascript, please help me with this code, where did I get it wrong?
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
console.log("Before: " + JSON.stringify(arrangment));
console.log(magix2(bum, 33));
console.log("After: " + JSON.stringify(arrangment));
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
//console.log("Before: " + JSON.stringify(arrangment));
console.log(magix2(bum, 33));
//console.log("After: " + JSON.stringify(arrangment));
arrangment is the argument of function, you can't access that argument outside the function, that's I commented out these console lines.
arrangment is an argument of magix2 function you can not access it outside. Try accessing bum instead.
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
console.log("Before: " + JSON.stringify(bum));
console.log(magix2(bum, 33));
console.log("After: " + JSON.stringify(bum));
The correct way to perform your operation. Console Return 26, as it is first element, also 33 is added to list
I'm getting undefined instead of a String on return of the answer - though it is the right correct characters being logged. How do I get it to output a definite string?
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
You forgot to return a value, so the return-value is undefined.
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
return 'Hello ' + first + second + '!';
}
console.log(greet('Gemma'));
The console will print the result of evaluating an expression. You can notice if you set
let name = 'john'
it will print undefined in the very next line.
That is also happening here. First it is printing your value, then it print undefined.
Your function is working fine - you just need to invoke it and feed its argument with a name...
[for the result look in the console]
var greet = function(name) {
let first = String(name.charAt(0).toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
;
greet("henky");
This is my very first question.
How can i run an "IF STATEMENT" in side the raw code of QZ tray where Var = print data [];
The below code works wonderful without IF STATEMENT, but the codes cannot parse once i use it.
var printData = [
'<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
'^O0\n'
'<xpml></page></xpml><xpml><page quantity="9" pitch="127.0 mm"></xpml>~MDELF,FORMAT_0\n',
'^E10.0\n',
'^L\n',
'C0,0000000000000000,+1,prompt_C0\n',
'C1,0000000000000000,+1,prompt_C1\n',
'C2,000,+1,prompt_C2\n',
'Lo,51,438,761,440\n',
'Lo,51,678,761,680\n',
'Lo,51,558,761,560\n',
'Lo,51,158,761,160\n',
'AH,320,31,1,1,0,0,'+ acs +'\n',
'BQ2,160,742,4,8,156,0,0,C^C0\n',
'AD,254,900,1,1,0,0,^C1\n',
'AA,439,440,1,1,0,0,Service\n',
'Lo,425,440,427,678\n',
'AA,442,560,1,1,0,0,Total No of Pieces\n',
'AA,439,684,1,1,0,0,Origin\n',
'AB,511,684,1,1,0,0,' + origin +'\n',
'AF,182,590,1,1,0,0,'+ destination+'\n',
'R49,13,762,999,3,3\n',
'E\n',
'^KFORMAT_0\n',
if (pcstart.length ==1)
{
premawb + postmawb +'0000'+ pcstart +'\n',
}
else {
premawb + postmawb +'000'+ pcstart +'\n',
}
pcstart + '\n',
'E\n',
'~P'+ copyPrint+'\n',
qz.print(config, printData).catch(displayError);
}
How can i run an "IF STATEMENT" in side the raw code of QZ tray
You can't mid-array, but you can add a ternary operator which does the same thing for a simple if/else statement:
pcstart.length == 1 ? '0000' : '000'
... and in context...
var printData = [
'<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
'...',
'^KFORMAT_0\n',
premawb + postmawb + (pcstart.length == 1 ? '0000' : '000') + pcstart + '\n',
pcstart + '\n',
'E\n',
'~P'+ copyPrint + '\n'
];
qz.print(config, printData).catch(displayError);
You can also call a function on the array element, so you may find it more desirable to roll your own pad(...) function and then call pad on the entire number or concatenated string... e.g:
premawb + postmawb + pad(pcstart, 4) +' \n',
I the above example, pad(...) is a function you make that can contain all the if/else statements you need and returns the formatted value.
I'm building a trading bot and I have this code which takes an array of arrays of 5 (API allows 5 price requests at a time). For each of these currencies, this api does an API call and retrieves results with a promise. In the promise I forEach through each currency returned, calculate the average of the prices and then save it to MongoDB.
Market.prototype.fetchPrices = function(currencyPairs){
var self = this
let currencyPairGroups = chunkBy5(currencyPairs)
_.forEach(currencyPairGroups, function(currencyPairGroup){
wc.price(currencyPairGroup).then(function(res){
let pairs = Object.keys(res);
_.forEach(pairs, function(pair) {
let currentObj = res[pair]
currentObj.pair = pair
currentObj.mid = _.mean([currentObj.bid, currentObj.ask])
console.log('Before saving ' + '\n' +
'this is the pair ' + currentObj.pair + '\n' +
'and this is the bid ' + currentObj.bid + '\n' +
'and this is the ask ' + currentObj.ask + '\n' +
'and this is the mid ' + currentObj.mid + '\n' +
'is the mid a NaN? ' + isNaN(currentObj.mid) + '\n')
savePrice(currentObj)
self.emit('emitPrice', currentObj)
})
})
})
}
Almost always, this works perfectly. Sometimes, however, the currentObj.mid gets passed as NaN and the save validation to Mongoose fails. The console.log looks like this.
this is the pair USD-MXN
and this is the bid 17.90732
and this is the ask 17.91332
and this is the mid NaN
is the mid a NaN? true
I feel like it's an async issue, but I don't think anything in my promise is async. Also, don't know why it works almost every other time. Don't even know what question to ask to figure this out.
I'm using Oracle JDK 1.8.0_65 with nashorn to run some test cases and I found a very strange behavior when parsing an empty JSON Array.
Here's the code i'm running in nashorn:
var testCase = {
start:function() {
// Case 1: a initialized from JavaScript Array
var a = [];
this.log.debug("a before:" + JSON.stringify(a) + " (length:" + a.length + ")");
a.push(15);
this.log.debug("a after:" + JSON.stringify(a) + " (length:" + a.length + ")");
// Case 2: b initialized parsing a JSON Array
var b = JSON.parse("[]");
this.log.debug("b before:" + JSON.stringify(b) + " (length:" + b.length + ")");
b.push(15);
this.log.debug("b after:" + JSON.stringify(b) + " (length:" + b.length + ")");
}
};
and the output is:
a before:[] (length:0)
a after:[15] (length:1)
b before:[] (length:0)
b after:[0,15] (length:2)
I'ts look like a bug in nashorn JSON parser. Returned Array is not really an empty Array, but it's look like that before pushing the first element. There's a hidden "0" that appears after the first push.
Can't find any bug report about this behavior.
Am I using JSON.parse in a wrong way?
Thanks.
J
Your usage is correct. It seems to be a bug and it appears to have been fixed. I just tried 1.8.0_112. It works as expected.