Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a function in javascript as so :
function player(){
var cards=[];
this.score=0;
var self=this;
this.addCard=addCard;
this.resetCards=resetCards;
function addCard(card){
cards.push(card);
this.score=+card.value;
}
function resetCards(){
cards=[];
score=0;
}
}
I user a constructor to call the function :
var player1=new player();
Then I call some of its enclosed functions like this
player1.addCard(someCardObject);//card someCardObject has .value say 5
player1.addCard(someCardObject);//card someCardObject has .value say 7
I expect player1.score to be 5+7=12 ,but it stays 7 .
Can anyone tell me what I am doing wrong here
You've got a simple error in addCard.
this.score=+card.value;
Should be
this.score += card.value;
In the first instance, you're setting this.score equal to card.value, while in the second one, you're adding card.value to it. Remember kids, order of operators matters!
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to make an alert, that prints the value of a variabile, here is my attempt.
var username = prompt("I'm LaunchBot, what's your name?");
var print = alert() ;
alert() is a function. It take a parameter between the parenthesis. So just insert your variable username between them:
var username = prompt("I'm LaunchBot, what's your name?");
alert(username) ;
As suggested in the comment alert() doesn't return anything so do not add a variable assignment before.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is happening on an angular application I'm building. If a user enters 80 into an HTML input, it always seems to get this comparison wrong.
var x = '80';
var y = 150.9800;
/* Returns incorrect answer */
if (parceFloat(x) < y) {
return true;
} else {
return false;
}
You need to use ParseFloat() not parceFloat() ...
parceFloat is not an existing function.
parceFloat() is not a function, the function is parseFloat()
A simple typo is all the error there is.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The code below throws the following exeption:
Uncaught TypeError: someFunction(...) is not a function
What is causing this exception to be thrown?
map = function (someList, someFunction){
var result =[];
for (x = 0; x < someList.length; x++ ){
result.push(someFunction(someList[x])());
}
return result;
};
map([1,2,3,4], function(num){
return num * 10;
});
The problem is in this line
result.push(someFunction(someList[x])());
^^
This extra parentheses are redundant. You are already calling the function someFunction by saying someFunction(someList[x]). By adding these extra parentheses, you are basically trying to call the return value of someFunction(someList[x]), which is a number here, not a function.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
var input = message.content.toUpperCase();
if(input.indexOf("!")
{
bot.sendMessage(message, "!!!");
}
Help would be great, also earlier input was defined earlier
The String#indexOf method returns the index if found else returns -1. In your case .indexOf("!") return 0 and it's a false value and if statement never gets executed,so update your condition based on that.
if(input.indexOf("!") > -1)
or
if(input.indexOf("!") != -1)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.