If condition (multiple cond) [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Need help with my if conditions.
if(userId && !targetId){
// query logic for userid and no targetid
}
else if (!userId && targetId){
// query logic for targeted and no user id
}
Now how do I write the "else" part? another query logic for when both userid and target id are present.

You may simply tag on another if else:
if (userId && !targetId) {
// query logic for userid and no targetid
}
else if (!userId && targetId) {
// query logic for targeted and no user id
}
else if (userId && targetId) {
// query logic for both true
}
else {
// this defaults to both being false, the other three
// conditions already being handled above
}

you could do
else
{
if(userId && targetId){ }
}

Related

i try filter array base on search input and select menu [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to make filter base on input search and select menu at the same time.The filter works for select and search but separately if I try marge I don't have any errors output but the filter doesn't work...
(zemlja.name.toLowerCase().indexOf(search) === 0)
if I remove this from filter, filter will work for select menu but I want to make one filter for search and select
const selectFilter = zemlje.filter((zemlja) => {
if ((zemlja.name.toLowerCase().indexOf(search) === 0) || (select === 'All')
|| (zemlja.region === select)) return true;
});
I would expect the results to contain the search-string and pass the filter-criteria, which is why I would use &&:
const selectFilter = zemlje.filter((zemlja) => {
const searchHit = (zemlja.name.toLowerCase().indexOf(search) === 0);
const filterPass = ((select === 'All') || (zemlja.region === select));
if ( searchHit && filterPass ) return true;
return false;
});

I need to sort it out this quiz [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
my code is not working at all
I need to solve this quiz
question is write convertToString as function !
this function should convert to string from parameter
ex )
let output = convertToString(120);
console.log(output); // --> '120'
let output2 = convertToString('hello');
console.log(output2); // --> 'hello'
let output3 = convertToString(true);
console.log(output3); // --> 'true'
this is what I wrote
function convertToString(anything) {
if (typeof anything === 'number' && typeof anything === 'boolean') {
let ret = anything.toString()
} else {
return anything;
}
return ret1;
}
convertToString(120);
The easiest way to convert anything is by making + operation with ""
function convertToString(anything) {
return "" + anything
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
console.log(convertToString(null));
console.log(convertToString(undefined));
Zero checks necessary.
function convertToString(val) {
return String(val);
// or return val.toString();
// or return '' + val;
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));

String as a Single value and Array as multiple value handling [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
If I select one value the I am getting value is like
val : "ABC"
But if I select multiple value my val is comming in array
val : ["ABC","DEF"].
Now because of this problem is neighther I can work on for single value nor multiple. Can anybody tell me how to handle this.
eg:
var myObj = {
data : "M1",
val : "Abc"
}
After adding another value it become
var myObj = {
data : "M2",
val : Abc,Def
}
function handleVal(val){
if ("string" === typeof val){
console.log("Do sth with string");
} else if (Array.isArray(val)){
console.log("Do sth with array")
}
}
handleVal("ABC");
handleVal([1,2])

Test the return value of my function correctly at the time of return [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I can not test the return value of my function correctly at the time of return.
My exercise is this: I must compare two arrays and return true if they have at least one identical element but I need some assistance in figuring out what is wrong with my code:
function duplicateElements(m, n){
function test (element){
return n.includes(element);
}
return m.filter(test) != [] ? true:false;
}
You have to test the length property of the return value.
function duplicateElements(m, n) {
function test(element) {
return n.includes(element);
}
return m.filter(test).length > 0 ? true : false;
}

Javascript variable returning blank [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am testing this code:
var thecol = '';
// Note: I know that value.cat = '3'
if (value.cat === '1') {
thecol = 'cat1';
} else if (value.cat === '2') {
thecol = 'cat2';
} else if (value.cat === '3') {
thecol = 'cat3';
} else if (value.cat === '4') {
thecol = 'cat4';
} else if (value.cat === '5') {
thecol = 'cat5';
};
alert(thecol);
The alert is blank for some reason.
Any ideas why.
Are you sure that value.cat is '3' and not 3? You can easily find out by logging typeof value.cat. Since you are using the deep equal it will only return true if both, the value and the type are the same:
'3' === 3 // -> false
'3' == 3 // -> true

Categories