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 append this to my DOM for every 'answer_' in my DB.
.append('<span>')
.text(format_date(answer_.LastModifiedDate))
But .LastModifiedDate won't always exist. Can I check for .LastModifiedDate in the text field? Maybe like this?
.append('<span>')
.text((answer_.LastModifiedDate) ? format_date(answer_.LastModifiedDate) : '')
Which doesn't work...
EDIT
I was stupidly checking for answer_.LastModifiedDate, instead of just answer.
So the following line works. Thanks for all the responses!
.append('<span>')
.text((answer_) ? format_date(answer_.LastModifiedDate) : '')
Of course that works. Ternary operators work anywhere you could normally place a variable. They evaluate to a value, just as if you used a string literal.
As #FreeAsInBeer pointed out, ternary works everywhere.
The only problem with your code is that you can't just use a (maybe) non-existant value as a boolean to check whether it is defined or not; How would you check if a variable holding "false" exists?
Instead you need to check the variables type:
.text(typeof answer_.LastModifiedDate !== 'undefined' ? format_date(answer_.LastModifiedDate) : '')
I was stupidly checking for answer_.LastModifiedDate, instead of just answer. So the following line works. Thanks for all the responses!
.append('<span>')
.text((answer_) ? format_date(answer_.LastModifiedDate) : '')
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 last year.
Improve this question
Recently while solving some coding problems I ran into a weird situation where i had to check if:
String starts with another string
I solved it quite simply with
if(string.includes(substring))
{
//code
}
And it worked..... BUT it returned TRUE when:
const string = "is it you or is it me or what?";
const substring = "is it me or";
What is the reason behind this returning true?
I even tried on different compiler websites and they all returned true. I really am puzzled as my understanding of .includes() is that it should check if a string contains the sub-string.
EDIT: I'm actually blind and i didn't notice that it contains the same string in middle of the sentence. Sorry, didn't have much sleep.
If you only need to return true if string starts with substring, use .startsWith() instead. For example:
> 'abc'.startsWith('a')
true
> 'abc'.startsWith('b')
false
Both .contains() (deprecated now, but might be supplied by polyfill) and .includes() (supported universally), however, will return true in both cases, as it checks whether or not a substring is contained within a given string, not necessarily at its beginning:
> 'abc'.includes('a')
true
> 'abc'.includes('b')
true
To answer your specific question, is it me or is part of that bigger string. You can use .indexOf() as a little helper here to provide you the exact position of that string:
const string = "is it you or is it me or what?";
const substring = "is it me or";
string.indexOf(substring); // 13
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'm trying to return a Boolean value based on truthy of a given condition from an arrow function. The code is look like following
checkIsBasicInformationCompleted() {
const info = this.basicInformation;
const valid = () => {return !!(info.firstName && info.lastName && info.email && info.phone);};
console.log(valid);
},
But here instead of returning true/false, this function is printing itself. Can anybody explain the thing running here ? And how can I get a true/false value here ?
Fiddle sample: https://jsfiddle.net/tebz4Lc3/
You are printing a reference to the function here, use console.log( valid() ) to actually execute the function and print the return value.
console.log(valid());
This is how the logging should be.
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 working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.
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.
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'm having trouble finding why this doesn't work:
value = ( (value == undefined) || (typeof(value) !=== "number") ) ? 1 : value;
From what I understand this should set value to either 1 or value(variable), depending on whether or not value is not a number.
If value is not a number, change it to 1, if it is, keep it.
What am I doing wrong here?
From a quick look:
!=== (with three equal signs) is a typo. The correct operator is !== (with two equal signs).
Whenever you have Javascript problems, I strongly urge working through all warnings and problems at http://www.jslint.com/ --it helps catch a lot of stuff the eye doesn't see easily.
This works just fine if you change !=== (which doesn't actually exist) to !== (which is the logical inverse of ===).
You were using one too many equals signs. The correct syntax is !==.
Just use value = (typeof(value) !== "number") ? 1 : value;
The (value == undefined) condition is redundant, because the latter is always true when the former is.