Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have this challenge with my code where I want to change this:
function validateLoanAmount(){
var loan_amount = $('[name=loan_amount]');
if(loan_amount.val() == ''){
showError(loan_amount, 'Field can\'t be empty');
return false;
}
if(loan_amount.val() <= 0){
showError(loan_amount, 'Value can\'t equal 0 or less');
return false;
}
}
into this:
function validateLoanTerm(){
var loan_term = $('[name=loan_term]');
isEmpty(loan_term);
isEqualZeroOrLess(loan_term);
}
function isEmpty(element){
if(element.val() == ''){
showError(element, 'Field can\'t be empty');
return false;
}
}
function isEqualZeroOrLess(element){
if(element.val() <= 0){
showError(element, 'Value can\'t equal 0 or less');
return false;
}
}
I tried to put return in front of isEmpty() and isEqualToZeroOrLess(), but then only first function resolved. It looks like a simple function to make shorter (because I have many similar inputs to validate and I want to keep it DRY), but looks like it's more complicated than I initially thought. Any thoughts?
Combine the calls with &&:
function validateLoanTerm(){
var loan_term = $('[name=loan_term]');
return isEmpty(loan_term) && isEqualZeroOrLess(loan_term);
}
But for this to work, the functions need to return true when there's no error.
function isEmpty(element){
if(element.val() == ''){
showError(element, 'Field can\'t be empty');
return false;
}
return true;
}
function isEqualZeroOrLess(element){
if(element.val() <= 0){
showError(element, 'Value can\'t equal 0 or less');
return false;
}
return true;
}
Related
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){ }
}
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'));
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I have question about else if
in my case last else not working
var pakeEach = $(".Thide").each(function () {
var cat = $(this).val();
if (cat.indexOf(PCatId) == -1) {
console.log("Pilih tidak ada di your aksess")
} else {
if (cat.indexOf(PCatId) != -1) {
console.log("Show only " + cat);
} else {
console.log("Remove");//why not working last else
}
}
});
Starting with your logic:
if (value == -1) {
...
} else {
if (value != -1) {
...
} else {
...
}
}
you can simplify it to:
if (value == -1) {
...
} else if (value != -1) {
...
} else {
...
}
As you can see, you're saying "if value is -1 then ..., otherwise if value is not -1..." That covers both possible circumstances: value cannot be anything but -1 or not -1 (as discussed by A. Wolff in the comments).
The else can never be hit, since all possible branches have already been covered.
If you want to include the remove logic in one of the other branches (probably the branch where the item was found), you should use something like:
if (value === -1) {
console.log("Pilih tidak ada di your aksess")
} else {
console.log("Show only " + cat);
console.log("Remove!");
}
This is a much more concise way of writing the same thing. Note that the === operator is generally preferred over ==, especially when you know you'll be comparing objects of the same type.
Because you are calling indexOf, the only possible result is the item is in the collection (indexOf(foo) > -1) or not in the collection (indexOf(foo) === -1). There is no third option here, unless you care about where in the collection it is.
For example, to remove the item if it is not the first item present in the collection, you would use:
if (value === -1) {
console.log("Pilih tidak ada di your aksess")
} else if (value === 0) {
console.log("Show only " + cat);
} else {
console.log("Remove!");
}
Because the if conditions only match a small subset of the possible values, the else can actually be hit in this case.
Algorithmically you are saying
if cat.indexOf(PCatId) is equal to -1 then execute the first if block
else execute the second block which is logically treating the case where cat.indexOf(PCatId) is different then -1.
I mean in the first else you can never ever get cat.indexOf(PCatId) == -1 because you are catching it in the first if
here is your answer with some modifications and comments :
var pakeEach = $(".Thide").each(function () {
var cat = $(this).val();
if (cat.indexOf(PCatId) == -1) {
// this part of code is executed if cat.indexOf(PCatId) == -1
console.log("Pilih tidak ada di your aksess")
} else {
// here at this level we can never get cat.indexOf(PCatId) == 1
if (cat.indexOf(PCatId) != -1) {
console.log("Show only " + cat);
}
// this else should be removed !!!! because it is useless
else {
console.log("Remove");//why not working last else
}
}
});
Try this:
var pakeEach = $(".Thide").each(function () {
var cat = $(this).val();
if (cat.indexOf(PCatId) == -1)
{
console.log("Pilih tidak ada di your aksess");
console.log("Remove");
}
else
{
console.log("Show only " + cat);
}
});
What you are trying to do is this:
var pakeEach = $(".Thide").each(function () {
var cat = $(this).val();
if (!cat) {
console.log("Pilih tidak ada di your aksess")
} else if (cat === PCatID) {
console.log("Show only " + cat);
} else {
console.log("Remove");
}
});
First check if cat is empty, then branch according to its value.
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
Am learning Javascript in code academy. This is the test I have been given,
"Inside the eat function, create an if statement that returns true only if both hungry and foodHere are true, and false otherwise."
My code below is executing but it has a warning. What could be the problem?
var hungry = true;
var foodHere = true;
var eat = function() {
if(hungry && foodHere){
console.log("I am hungry");
}else if(hungry && foodHere = false){
console.log("Choose one");
}
};
eat();
var hungry = true;
var foodHere = true;
var eat = function() {
if(hungry && foodHere){
console.log("I am hungry");
}else {
console.log("Choose one");
}
};
eat();
the problem was, that you tried to assign a value in the condition foodHere = false. If you want to compare things you need == and if you want to be sure that the types are the same use ===.
But you don't need that condition at all!
The assignment want you to return a boolean value (true or false) and not to print something, so i guess your code should look like this:
var hungry = true;
var foodHere = true;
var eat = function() {
return (hungry && foodHere)
};
eat();
Single = is for assignment, == is for comparison
var hungry = true;
var foodHere = true;
var eat = function() {
if(hungry && foodHere){
console.log("I am hungry");
}else if(hungry && foodHere == false){
console.log("Choose one");
}
};
eat();
Anyways, you don't need the second comparison since if the first one is false it will always go through the else
I just want to give additional information for emilioicai's answer, there are two kind of "equal comparison" in JavaScript
equal to ( == )
exactly equal to ( === )
Reference: http://www.w3schools.com/js/js_comparisons.asp
= is for assignment, == is for value comparison and === is for value and datatype comparison. So you can use == while comparing with the false. But this is not the best practice. We always uses '!' negation operator while converting true to false
var hungry = true;
var foodHere = true;
var eat = function() {
if(hungry && foodHere){
console.log("I am hungry");
}else if(!hungry || !foodHere){
console.log("Choose one");
}
};
eat()
I am using || operator assuming that any one needs to be false.You can go for && if both false are required
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