Object property is undefined when it comes from method parameter [closed] - javascript

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 8 years ago.
Improve this question
I'm stuck on this:
var Names = function (name,integer) {
this.name = name;
this.integer = integer;
};
var Discount = {
applyDiscount: function(person) {
console.log("person name is: " + person); //Eve
console.log("person integer is: " + person["integer"]); //undefined
console.log("person integer is: " + Eve["integer"]); // 23
}
};
var Eve = new Names("Eve Something", 23);
Discount.applyDiscount("Eve");
When I try to get Eve.integer value (using person parameter) - I've get undefined.
Why person["integer"] isn't working in this case?

You're passing the string "Eve" to the function and not the object that the variable Eve points to. Remove the quotes.

Needed use Object, not a string:
Discount.applyDiscount(Eve);

Related

The find function is not available on my array object [closed]

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 5 months ago.
Improve this question
Consider this simple attempt to find a value in an object:
var config = {
product1: {
ids: ['master']
},
product2: {
ids: ['ps1', 'ps2']
}
};
var id = 'ps2';
var slotID = 'master';
var categorySlotIds = config[slotID].ids;
categorySlotIds.find(id);
I get: TypeError: Cannot find function find in object product1, product2
If I do typeof(categorySlotIDs) the result is object.
The manual for find says: The find() method returns the first element in the provided array
What gives?

Javascript won't recognise json element as number [closed]

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 3 years ago.
Improve this question
I'm processing a json object and want to display a number with two decimal places, but javascript says the type is 'undefined'.
I've tried using .toFixed(2), Number() and ParseFloat() to force it to be a number, and I've even tried String() to force it to change to a string, but it stubbornly remains as 'undefined'. typeof isn't even recognised as a property of the original element.
Surely there's a way to convert it / properly define it. I'd appreciate help and an explanation of why what I'm doing doesn't work.
data = {
list: {
9: {
balance: 256.3999999999942
}
},
action: 'load',
status: 'ACCTLOADED'
}
showAccounts(data);
function showAccounts(data) {
action = data.action;
status = data.status;
accs = data.list;
if ((action == 'load') && (status == 'ACCTLOADED')) { // data retrieved
$.each(accs, function(acc, details) { // display each account
let bal = 0;
bal = Number(details.balance);
console.log('bal ' + bal + bal.typeof);
});
}
} // end function showAccounts
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I think bal is already type of number.
Use like:
console.log('bal ' + bal + typeof bal);

SyntaxError: missing ) after argument list var x = $("input[name="+value+'[]'"]") [closed]

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 am getting error on this. i don't know what happen here.
enter code here
$(".addCategory").on('click',function(){
var value= $(".getvalue").val();
var x = $("input[name="+value+'[]'"]")
$(x).each(function(key,val){
if($(val).val().length<=0)
{
alert ("Please fill all the fileds");
}
});
});
You have problem with quotes
var x = $("input[name='" + value + "[]']")
Problem was with your quotes:
replace with following code
$(".addCategory").on('click',function(){
var value= $(".getvalue").val();
var x = $("input[name='"+value+"[]']") // add proper quotes
$(x).each(function(key,val){
if($(val).val().length<=0)
{
alert ("Please fill all the fileds");
}
});
});
See How it should wrap up
var x = $("input[name='"+value+"[]']")

Dealing with javascript objects [closed]

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!

How to pass value for the JavaScript statements? [closed]

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 8 years ago.
Improve this question
I want to pass a value to a Javascript Statement. Is it possible?
For Example :
function Show(msg, Id, Msgtype) { // I need to pass this Id argument to below javascript innerHtml statement
var strVar = "";
strVar = "Hello" + Msgtype + " <i class='icon-remove close'></i> " + msg + "</div>";
document.getElementById("???").innerHTML = strVar; // ??? should be the Id above in the function arguments
}
Or in other way, how can I achieve this scenario?
Just pass Id, like this
document.getElementById(Id).innerHTML = strVar;

Categories

Resources