Javascript function keep asking for argument - javascript

First I made function:
function createComment(template) {
let emoji = document.getElementById("emojis").value;
let comment = template.replace(/p/g, emoji)
document.getElementById("comment").innerHTML = "kok" ;
}
and in HTML button : <button type="button" onclick="createComment()">Go</button>
but, I wanted to remove template argument from function because button didn't work so I just reduced JS function to :
function createComment() {
//let emoji = document.getElementById("emojis").value;
//let comment = template.replace(/p/g, emoji)
document.getElementById("comment").innerHTML = "kok" ;
}
but even now developer tools report error Uncaught TypeError: Failed to execute 'createComment' on 'Document': 1 argument required, but only 0 present.
If I change function name to anything to anything else like from createComment to createCommentS and in button too it works. For some reason it just keeps on hanging of original function argument and it's stubborn doesnt wanna give up on asking argument. Why?

Rename the function to something else because it is probably being shadowed by document.createComment()

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.

Angular Scope issue -> function comes off scope after one use?

Here is the relevant snippet of code:
$scope.newLike = LikeFactory.newLike;
$scope.newDislike = LikeFactory.newDislike;
$scope.updateLike = LikeFactory.updateLike;
$scope.updateDislike = LikeFactory.updateDislike;
$scope.vote = function(sockId, nnew, update) {
if (!$scope.verifyUser) $scope.alert("Please log in to like a sock!");
if (!$scope.like.like && !$scope.like.dislike) {
return nnew(sockId).then(function(vote) { $scope.vote = vote; });
} else {
return update(sockId).then(function(update) { $scope.vote = update; });
}
}
I call this function, $scope.vote, in the html with an ng-click="vote(sock.id, newLike, updateLike)" or ng-click="vote(sock.id, newDisike, updateDislike)" whether on an like vs dislike button. The call button works fine when first liking and updating once there is an instance of a 'like' for a particular unique sock/user combo but after one 'update' I get the error:
angular.js:13642 TypeError: v2.vote is not a function
Do I need to trigger a $digest for the function to continue to be in $scope? Does it somehow come off $scope after being used? It seems like a strange error to me.
Edit: duh! It's late, thanks for the answers!
You define $scope.vote as a function in your controller. After first invocation you assign a result that may not be a function to this variable, thus vote is no longer a function:
$scope.vote = function() {} // => 'vote' variable is holding a function
$scope.vote = vote / update // => 'vote' might not reference a function but a value
Log your result after the promise is resolved (in the then block), to understand what is the new assigned value.
It's normal, here : $scope.vote = update you use the same varaible that the function name
The issue must be with the following statement:
$scope.vote = vote;
The vote might not be function and so v2.vote is not a function

How do you pass an argument to the Google Apps Script debugger?

Say I have the following broken example function in a google-apps script. The function is intended to be called from a google sheet with a string argument:
function myFunction(input) {
var caps = input.toUpperCase()
var output = caps.substrin(1, 4)
return output
}
While this example script should break on line 3 when you select myFunction and press debug, as there is no such method as "substrin()," it will break on line 2, because you can't put undefined in all caps:
TypeError: Cannot call method "toUpperCase" of undefined. (line 2,
file "Code")
Question:
Is there an official way to pass a string to a google-apps script for testing/debugging without making an additional function
function myOtherFunction() {
myFunction("testString")
}
and debugging that?
The function as you wrote it does need a parameter and there is no way to avoid that except by including a default value in the function itself. See example below
function myFunction(input) {
input= input||'test';
var caps = input.toUpperCase();
var output = caps.substrin(1, 4);
return output;
}

ExternalInterface.call not working

I am attempting to ExternalInterface.call() from inside my SWF.
Note the call comes from inside a SWF I have embedded into another SWF (the _root SWF I have no control over).
Here is my JavaScript:
function player_DoFSCommand(command, args)
{
args = String(args);
command = String(command);
var arrArgs = args.split(g_strDelim);
switch (command)
{
case "CC_ClosePlayer":
console.log("yo");
break;
default:
// alert(command);
break;
}
}
Here is my AS2 code:
import flash.external.ExternalInterface;
var quiz = _root;
quiz.g_mcFrame.mcFinish.swapDepths(quiz.getNextHighestDepth());
quiz.g_mcFrame.mcFinish._visible = false;
quiz.oSlide.m_oInteraction.m_oVariableMgr.m_arrBoolResumeData = false;
var arrVars:Object = quiz.oSlide.m_oActionHandler.m_oFrame.m_oVariableMgr.m_arrVariables;
var args:Array = [
arrVars[2].m_nNumber, // Points awarded
arrVars[3].m_nNumber, // Max points
arrVars[5].m_nNumber, // Pass percentage
arrVars[6].m_nNumber, // Pass points
arrVars[7].m_nNumber, // Score percent
arrVars[10].m_strString // Result
];
ExternalInterface.call('player_DoFSCommand("CC_ClosePlayer", args)');
ExternalInterface.call('console.log("hello")');
The problem is ExternalInterface.call('player_DoFSCommand("CC_ClosePlayer", args)') doesn't work, or atleast "yo" doesn't appear in the console as expected.
The script is definitely loaded by the time the ExternalInterface is called. The second call works and "hello" appears in the console, however the first function doesn't fire.
I can verify the function works by typing directly into the console player_DoFSCommand("CC_ClosePlayer", args) and it logs successfully.
Can you verify that allowScriptAccess is set to true in the embed code?
Try this:
var isAvailable:Boolean = ExternalInterface.available;
trace(isAvailable);
You also want to use ExternalInterface like this, with your arguments passed as the second variable:
ExternalInterface.call("player_DoFSCommand",args);
or
ExternalInterface.call("console.log","testing...");

Unexpected identifier for function call

I have a simple onclick feature which calls a function but im getting an unexpected identifier but i don't see where the mistake is, it doesn't actually give me the error until i click the link which is confusing.
This is the script:
function discard_item(id,name){
alert('test');
return false;
}
function load(){
name = 'test';
id = 1;
output = [];
output.push('[Discard]');
document.getElementById('main').innerHTML = (output.join(''));
}
load();
When i click the link to call discard_item i get : Unexpected token }
Can't seem to see a mistake in my script though ? http://jsfiddle.net/Lu2HK/6/
Hope you can help!
Try this:
Change the " to escaped 's:
output.push('[Discard]');

Categories