Javascript won't recognise json element as number [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 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);

Related

Getting to know if '*' is included in a string? [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 1 year ago.
Improve this question
I have a string variable and I want to know if the multiplication symbol is in the string. This is what I did:
const str = "45*33"
const arr = str.split('')
if(arr.includes("*"){
console.log("Symbol found!")
}
else{
console.log("Error")
})
This does not work, is there a way I can make this work?
You don't need to split at all; you can directly use String#includes.
const str = "45*33";
if (str.includes("*")) {
console.log("Symbol found!")
} else {
console.log("Error")
}
Use the console of your browser to check for errors.
It is a simple syntax error at:
if(arr.includes("*"){
should be:
if(arr.includes("*")){
Working perfectly now, it was a typo in the code that my eye just did not see. There is a reason why I had to split the string to an array. I could have just used the str.includes().
const str = "45*33"
const arr = str.split('')
if(arr.includes("*")){
console.log("Symbol found!")
}
else{
console.log("Error")
})
Thank you.

How to avoid 'Uncaught ReferenceError: ? is undefined'? [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 1 year ago.
Improve this question
I have a script (config.js) at the base of every page. But, that script contains code that isn't used on every page. The console returns unused or undeclared variables as undefined.
let currentURL = document.location.href;
function redirectURL() {
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
redirectURL();
This is what's returned.
How do I fix this?
You need to check whether the redirect element exists before trying to use it.
function redirectURL() {
if (typeof redirect == 'undefined') {
return;
}
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
This will allow the function to work without error on pages that don't define redirect.

Cannot assign to read only property 'x' of true [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I'm running into a frustrating issue in my Angular controller. I'm trying to conditionally set the values of certain fields of objects I attach to my scope. The if block below works perfectly by itself, but as soon as I add the else block I run into the following error:
TypeError: Cannot assign to read only property 'gameType' of true
var getGames = function() {
var defer = $q.defer();
playersService.getGames({
playerId: playerId
}).$promise.then(function(data) {
vm.games = data;
for (var x in vm.games) {
if (vm.games[x].activity_type === 'preseason') {
vm.games[x].gameType = 'preseason';
} else {
vm.games[x].gameType = vm.games[x].type;
}
}
defer.resolve(data);
});
return defer.promise;
};
After console.log'ing vm.games[x] within the else block I'm seeing the error occurs when I hit:
Promise {$$state: Object}
true
...whereas all the other lines show:
Resource {id: "...", ...}
Don't use a for in loop on array, use a standard for loop. for in is for iterating the properties of an object. This could be causing some hiccups when assigning property values.
Your vm.games[x].gameType data type and vm.games[x].type; data type may different.
if your if condition worked perfectly then you can try
if (vm.games[x].activity_type === 'preseason') {
vm.games[x].gameType = 'preseason';
} else {
vm.games[x].gameType = ''+ vm.games[x].type; // ''+ is convert to string
}

Object property is undefined when it comes from method parameter [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'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);

jQuery error = SyntaxError: missing : after property id [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
There seems to be a lot of these errors on SO but I can't find any to relate to.
The error is: SyntaxError: missing : after property id and it's complaining about this line: var msg = ""; (Line 3).
function checkSubmission()({
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0){
msg+="LMS Name Required.<br/>";
}else{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex)) {
} else {
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lots more code here...
Any ideas? FYI I'm a complete newbie (for now...).
Thanks in advance!
The code in your comment on #Shoaib Raza, prompted me to post this. (please accept his answer rather than mine, since his was first.)
Quote OP Comment:
FYI, I changed it to just function checkSubmission( var msg = ""; ... and the error still appeared.
And that's totally wrong too. checkSubmission( ... is not what his answer is showing, so the new error message is accurate: SyntaxError: missing ( before { -> function checkSubmission{
This line of your original code contains the original syntax error...
function checkSubmission()({
should be this...
function checkSubmission() {
Notice the difference? You simply need to remove the extra ( and nothing else.
Change your function to :
function checkSubmission()
{
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0)
{
msg+="LMS Name Required.<br/>";
}
else
{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex))
{
}
else
{
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lot more code in the function here.. if any
}
Hope this helps.

Categories

Resources