Unable to pass string into a function? - javascript

My Javascript code is
function ad_cart(nm, mmi, pr) {
alert(imm);
}
The value i'm passing from onclick is this
onclick="ad_cart(Drafting Factory Folders ,2,3.50)"
But it is showing a error as
SyntaxError: missing ) after argument list
In mozilla
In chrome the error is
Uncaught SyntaxError: Unexpected identifier
But if i pass integer instead of string like
onclick="ad_cart(1,2,3.50)"
then it is working fine

Try like
onclick="ad_cart('Drafting Factory Folders' ,2,3.50);"
And your function will be like
function ad_cart(nm, mmi, pr) {
alert(mmi); // Instead of `imm`
}

function ad_cart(nm, mmi, pr) {
alert(imm);
}
Seems to spell mistake also, getting mmi and showing imm
And the string should be wrap by ''
edit:
function ad_cart(nm, mmi, pr) {
alert(mmi);
}
ad_cart('string' ,number,number)

Related

How to avoid the error Expected 1 argument but got 0 on Typescript

This is my JavaScript function
function movements(remove) {
var op = remove ? 'remove' : 'add';
crossvent[op](documentElement, 'selectstart', preventGrabbed); // IE8
crossvent[op](documentElement, 'click', preventGrabbed);
} function move(value) {
And this is how it's called
movements();
You can find reference for in jkanban.js file.
Now I have to change it to Typescript and I got this error on function calling,
Expected 1 arguments, but got 0
How can I resolve this problem in typescript ?
Simply add the question mark to the argument that requires your function, example:
function movements(remove?) {
// ...
}
You need to specify the input for calling movements().
You can set default to the variable using this:
function movements(remove = null) {
so that the function won't break even if you don't give it the input.
You can default it to anything you like though.

Cannot read property of undefined in class

I'm new to javascript so can anyone help me figure out why this code is not working?
I have a class and it calls a cordova barcode scanning function. I've got an example that works, however I want to be able to separate out the function(result) and function(error) and use onSuccess(result) and onFailure(error).
I have no idea why this is happening so if anyone can help that would be great.
EDIT: so ive updated the code based on Stradosphere said however im still getting result is not defined errors.
Full error message:
Uncaught ReferenceError: result is not defined at barcodeScanner.scanBarcode (barcodeScanner.js:10) at HTMLButtonElement.myFunction (main.js:18)
var me = this;
class barcodeScanner {
constructor() {
this._barcodeResult = 0;
}
scanBarcode() {
//THIS THROWS result is not defined error
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
//THIS WORKS
cordova.plugins.barcodeScanner.scan(
function (result) {
me._barcodeResult = result.text;
alert("Barcode Scanned:" + me._barcodeResult);
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
onSuccess(result) {
this._barcodeResult = result.text;
alert("Barcode Scanned:" + this._barcodeResult);
}
onFailure(error) {
alert("Scanning failed: " + error);
}
}
Looking at the docs, it appears that cordova.plugins.barcodeScanner.scan() expects you to pass a function into it. But you are calling it like this:
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
This is passing the result of the function .onSuccess(result), but result is not defined, so you are getting an error. Additionally, you want this to be the class instance, but by defining me as this outside the class, me won't equal the class instance like you want it to. But you don't need it anyway.
Try passing functions in instead:
cordova.plugins.barcodeScanner.scan((result) => this.onSuccess(result),(error)=> this.onFailure(error))
Maybe a scope issue on your use of this. Try:
var me = this; //(put this at class level)
cordova.plugins.barcodeScanner.scan(me.onSuccess, me.onFailure);

`Uncaught SyntaxError: Unexpected string` in Angularjs app

app.js is:
directive('test', ['name', function ($name) {
return {/// DDO
template:'<h1>'.$name.'<h1>',
link: function () {
console.log($name);
}
};
}]).
While above name a service, which I am injecting into above directive. Above code works fine and data shows up both in console and web page.
BUT
error occurs when I replace template: $name with template: '<h1>'.$name.'</h1>'.
The error I get is this:
Uncaught SyntaxError: Unexpected string
So if I can't concatenate string named $name like that with <h1> tags then how do I do it there inside the DDO?
Note: Above given code is definitely not complete code, it's just the part I had problem with. Also service named name was declared/defined/created(or whatever it's called) by using value function.
Concatenation in JS is done with the + symbol.
directive('test', ['name', function ($name) {
return {/// DDO
template:'<h1>' + $name + '<h1>',
link: function () {
console.log($name);
}
};
}])
To concat string in javascript you have to use +
like this
'<h1>'+$name+'</h1>'
concat string by . is in php not in javascript

How do I use UI.registerHelper in Meteor 0.8.0?

In 0.7.2, I could have something like this
<span>{{color 'blue'}}</span>
Handlebars.registerHelper('color', function (parameter) {
return Session.equals('color', parameter) ? 'blue' : 'red';
});
and it would work perfectly fine. What is the equivalent in the 0.8.0 release? I know they replaced Handlebars.registerHelper with UI.registerHelper, but this
UI.registerHelper('color', function (parameter) {
return Session.equals('color', parameter) ? 'blue' : 'red';
});
still returns this error
Exception in Meteor UI: Error: Can't call non-function: [object Object]
Any help?
Turns out it was all because one of my registerHelpers was the word 'parent'. Using that threw an error so it made it seem like all of my helpers were messed up. I'm guessing that it's a reserved word.
I'm doing this & it works (exactly as I use it). Do you use iron-router? This might do too.
UI.registerHelper('routeActive', function(routeName) {
var route = Router.current();
if(route && route.route && route.route.name == routeName) return 'active';
});
Then you can use the name of your template/route in routeName
I'm a bit unsure why you're getting that error are you sure its from the Handlebars expression.
You probably need to pass the parameter with a name:
<span>{{activePath path='home'}}</span>
UI.registerHelper('activePath', function() {
return Session.equals('activePath', this.path) ? 'active' : '';
});

JavaScript: why .forEach doesn't work?

Here is a little piece of code:
window.addEventListener('load', function() {
['echo'].forEach(function(entity) {
console.log('loaded entity=' + entity)
})
})
console.log(['echo'])
console.log(['echo'].forEach)
['echo'].forEach(function(entity) {
console.log('entity=' + entity)
})
Output looks like this:
["echo"]
function forEach() { [native code] }
Uncaught TypeError: Cannot read property 'echo' of undefined
loaded entity=echo
Why does this error occur? I assume that undefined is this inside .forEach. Why doesn't it get passed when calling .forEach?
SEMICOLONS!
window.addEventListener('load', function() {
['echo'].forEach(function(entity) {
console.log('loaded entity=' + entity);
})
});
console.log(['echo']);
console.log(['echo'].forEach);
['echo'].forEach(function(entity) {
console.log('entity=' + entity);
});
The problem is here:
console.log(['echo'].forEach)
['echo'].forEach(function(entity) {
The line break is ignored, at it gets parsed as this:
console.log(['echo'].forEach)['echo'].forEach(function(entity) {
console.log() returns undefined, and undefined['echo'] raises an exception.
So use semicolons and be happy. Or don't and suffer.
You need to add semi-colons. Your script is being evaluated as:
console.log(['echo'].forEach)['echo'].forEach(function(entity) {
console.log('entity=' + entity)
})
And since console.log returns undefined, you get an uncaught TypeError because you can't access an echo property on undefined.
Javascript can work without semicolons (treating newlines as end of statement), as long as concatenation of following lines is syntactically incorrect & parsing makes no sense.
For eg:
var a=1
var b=2
would work since the semicolon will be added as var a=1 var b=2 doesn't make sense.
Thus it will be treated as var a=1; var b=2. Similarly,
console.log(['echo'].forEach)
['echo'].forEach(function(entity) {
console.log('entity=' + entity)
})
is read as :
console.log(['echo'].forEach)['echo'].forEach(function(entity) {
console.log('entity=' + entity)
})
Here console.log(...) is treated an object with the property 'echo'. Hence the error.

Categories