If/Else only works for first if [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 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.

Related

undefine keep on appearing [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 7 months ago.
Improve this question
I have been battling with this code since couple of days and its giving me issue. the other functions are working perfectly except this EQUAL function.
Below is the code
equal.addEventListener("click", function(e){
if (screen.value = "") {
screen.value = "";
} else{
console.log(screen.value);
let answer = eval(screen.value);
screen.value = answer;
}
});
The first thing jumping out to me is that you're using the assignment operator rather than comparison in the if statement. Maybe this resolves your issue:
if (screen.value == "") {
screen.value = "";
} else{

JavaScript function printing itself rather than returning 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 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.

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.

Why does JavaScript get this comparison wrong? [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
This is happening on an angular application I'm building. If a user enters 80 into an HTML input, it always seems to get this comparison wrong.
var x = '80';
var y = 150.9800;
/* Returns incorrect answer */
if (parceFloat(x) < y) {
return true;
} else {
return false;
}
You need to use ParseFloat() not parceFloat() ...
parceFloat is not an existing function.
parceFloat() is not a function, the function is parseFloat()
A simple typo is all the error there is.

Function not returning the value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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 am working on a javascript quiz programme , and i have return a function to check what difficulty level the user wants . below is the code and the jsfiddle :
function getdifficulty(){
var j = 0;
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == easy){
j = questionseasy[0];
}
else if(level == intermediate){
j = questionseasyenuf[0];
}
else{
j = questionshard[0];
}
alert("you did it");
}
getdifficulty();
Jsfiddle here
now the problem is the the alert is not showing up ? whats the problem with this short piece of code ? (In the real programme though i will not use an alert but return statement , i even tried using document.write or console.log but none worked) .
prompt() returns a string. You need to make your comparisons strings by encapsulating them in double quotes (").
function getdifficulty(){
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == "easy"){
alert("easy");
}
else if(level == "intermediate"){
alert("intermediate");
}
else{
alert("hard");
}
}
getdifficulty();
JSFiddle
Also, in the implementation you have provided in your post, questionseasy, questionseasyenuf, and questionshard will not be defined. You must bring them into the scope of the function before you can start using them.
questionseasy is undefined. You can trace javascript as it runs in your browser console and see this.

Categories

Resources