How to remove 'Undefined' status on forEach function variable - javascript

Im a beginner to coding JS. When im trying to create an array function for diplaying object reference. I write it on the VS code. See the image for the details.
The problem is that i want to remove the 'undefined' status that is printed on the cmder. Im using Node and cmder to run the code.
Hope this clear for you to understand my problem. Thanks

You are logging the return value of foodList.
foodList returns the return value of forEach.
forEach always returns undefined
If you don't want to log that, then remove the console.log() from around the return value of foodList.

You are doing
console.log(foodList(animal))
this console.log is returning undefined
do this
foodList(animal)

Yes, it's my fault, i didnt know that console.log return undefined when using forEach(). Sorry everyone, my bad. Thanks for the helps!

Related

Using a for loop in JavaScript to loop over an array of objects

My code and terminal
Just ignore the comments, they are in danish as it is for a school assignment..
I need some help. My loop keeps giving me an undefined value in my terminal, and I can't seem to find the issue. It is working as it should, and gives me the correct values, but still has those 'undefined' ones which irritates me.
EDIT: Has been fixed by #aqq, thx for the help everybody!
As #dikuw mentioned the undefined call might be coming from the gørBeskedPersonlig function being called.
I don't see that function being defined in your code so that's probably it, try commenting out the following line:
console.log(gørBeskedPersonlig(katalog[index].navn));
UPDATE: After reviewing your code, i can see that the function gørBeskedPersonlig was not returning anything.
Updating it to return the new value has fixed the "undefined" error.
function gørBeskedPersonlig(navn){
hemmeligBesked.splice(1,1,navn+'!');
return hemmeligBesked.join(' ');
}
It seems that you have some console.log() in function gørBeskedPersonlig that is returning undefined. Send a code for that function.

check for undefined not working

I just started programming with javascript no long ago. But I am still confused with the way it handles arrays.
the problem is that I have an array in which there some content as undefined. I want to check for those undefined values and escape them. How do I do it. the following code is not working and I have not found a way to solve this problem.
if(myarray[0]!=='undefined')
if(myarray[0])!='undefined') // I have also tried
if(myarray[0]).search('undefined')==(-1)) // and also this
however none of these options is working. I could see when debugging that the value in the array is 'undefined' but the condition is never met. once again my aim is to do something when it is not undefined. please how could I do it. I found a similar question Test if something is not undefined in JavaScript but it does not work
You are comparing undefined to 'undefined', which is a string so its different. You need to compare like this:
myarray=[undefined];
if(myarray[0]!==undefined){
console.log(4)
}
juvian provided a good answer. You need to use undefined instead of 'undefined' because you are comparing your array to a string.
On my current project, I use underscore to check for undefined. It's pretty simple and straight-forward.
var array = [];
if (_.isUndefined(array));
http://underscorejs.org/#isUndefined
You're comparing the myarray[0] value to a string value of undefined.
What you need is this:
if (typeof(myarray[0]) !== 'undefined')
Hope this helps!
You should check it as follows: if(typeof(myarray[0]) != 'undefined')

return a value from this variable in jquery

This has been driving me crazy.. I am trying to return a value from a variable and I can't seem to do it. When I multiply the variable by something, it works fine, but when I try to just show that one variable on a keyup function, it's a no go.
This is the problem in the script I am running into:
$("input.numberOfAccounts").keyup(function () {
$("input.pricingPerAccount").val($(newPricePerAccount));
});
As you can see, newPricePerAccount is the value I want to return and I have tried everything to make it just return that. What am I doing wrong?
Here it is in a jsfiddle, it will give you more insight.
http://jsfiddle.net/GLnQx/
EDIT: I have updated the fiddle.. All I am really trying to do is match input.pricingPerAccount to the variable of newPricePerAccount and it keeps returning just object, object. What's wrong?
Is this what you wanted? http://jsfiddle.net/GLnQx/2/
EDIT:
If I understand your comment correctly, you want to re-update the value of newPricePerAccount each time as well? Is this what you mean? http://jsfiddle.net/GLnQx/6/
var newPricePerAccount = $("input.pricingPerAccount").val() * $("input.numberOfAccounts").val();
alert(newPricePerAccount);
Is that what you are looking for?

Looping over object values works as intended, but first value is undefined?

Could someone explain to me why objectInfo method on the third button returns undefined for the first value? http://jsfiddle.net/PnSSX/11/
I can't figure out where this comes from, because there is no property before name...
Can you help? Am I missing something?
Best regards,
shapeshifta
It's because loop is initially undefined and you're calling += so it gets a converted to a string, to fix it, change this:
var loop;
To this:
var loop = "";
You can see the updated/working version here.

JavaScript if statement returns wrong values

Does anybody knows why this snippet returns false even if the passed string is "Active"?
if ($('status_'+id).getText()=="Active")
I've also tried changing the code to
if ($('status_'+id).getText()==String("Active"))
and
if (String($('status_'+id).getText())=="Active")
and still no luck.
I've also checked $('status_'+id).getText() through console.log to verify if it really returns "Active"
i wonder why it doesnt work?
any ideas?
Silly question: are you sure the returned string doesn't contain spaces?
The first step in any debugging task is to check your assumptions. Use a debugger or a series of alerts to check the following:
what's the value of id?
does$('status_'+id) evaluate to a DOM
element?
what does
$('status_'+id).getText() actually
return

Categories

Resources