if/else statements using cookies as condition - javascript

I'm creating an if/else statement that is based on if certain cookies exist, but when I run the code it just gives out: 1000, 1000.
Note: I'm using a cookie plugin too at https://github.com/js-cookie/js-cookie.
Here's the javaScript (only a small part of the actual code):
Cookies.get("options");
Cookies.get("options2");
if (Cookies.get('options'&& 'options2') == '1000', '1000') {
alert("1000, 1000");
} else if (Cookies.get('options') && Cookies.get('options2') == '1100', '1000') {
alert("1100, 1000");
} else if (Cookies.get('options') && Cookies.get('options2') == '1010', '1000') {
alert("1010, 1000");
} else if (Cookies.get('options') && Cookies.get('options2') == '1001', '1000') {
alert("1001, 1000");
} else if (Cookies.get('options') && Cookies.get('options2') == '1110', '1000') {
alert("1110, 1000");
} else {
}

I believe the issue is the following line:
if (Cookies.get('options'&& 'options2') == '1000', '1000') {
Working with two values doesn't work the way you seem to be trying. I would try something like this:
if (Cookies.get('options') == '1000' && Cookies.get('options2') == '1000') {
This is because Cookies.get() only accepts one cookie name at a time and returns the value for that one cookie, and you have to do each comparison separately.

Related

If and Else Multiple Rules Not Functioning Properly

I have been studying the if and else if statements and trying to implement them on a project.
I was able to apply it to a single field on my form but had no success while trying to apply it to multiple fields.
I hope that I can find someone with more understanding to take a look and help me understand better what I do not understand.
Ok, so what I am trying to achieve is a validation of user input on my form.
I use AppleScript for the form and submission is made to the spreadsheet but I am trying to use the if and else if statements to check if a user is providing information that is already recorded on the spreadsheet to prevent multiple inputs.
I do not know if I have to provide the entire code of my app script but,
-1, -5, -6, and -7 stand for the row numbers in the spreadsheet where the user inputs are saved.
(-1 USERNAME), (-5 ID NUMBER), (-6 PHONE NUMBER), (-7 EMAIL).
My script checks if user input does not match with any record in these rows on the spreadsheet and rejects it if there is a match and asks them to provide another one.
To check a single field, I use the code below and it works fine:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};
To check the other fields, I use the below code but does not work:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -5 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","ID No. taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -6 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Phone taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -7 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Email taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1, -5, -6, -7 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};

Best way to check if JSON response is empty or null

I'm just wondering if this is the best way to check if a JSON response is empty or NULL.
This is what I did so far. With this way, how would it figure out which JSON key is actually empty, and which is full, then how would it submit the correct one. That's why I was thinking of the second option. Am I just super over-thinking this?
if ((data.city.length > 0) || (data.region.length > 0) || (data.country.length > 0)) {
$('#city').empty().append(data.city);
$('#region').empty().append(data.region);
$('#country').empty().append(data.country);
} else {
$('#city').empty().append('No Info Available');
$('#region').empty().append('No Info Available');
$('#country').empty().append('No Info Available');
}
But then it got me thinking if this way would be better, it would be a lot longer though. Thoughts on this?
if ((data.city.length > 0) {
$('#city').empty().append(data.city);
} else {
$('#city').empty().append('No Info Available');
}
if ((data.country.length > 0) {
$('#country').empty().append(data.country);
} else {
$('#country').empty().append('No Info Available');
}
If that's the pattern you have, and it's consistent
$.each(data, function(key, value) {
$('#' + key).html(value || 'No Info Available');
});
FIDDLE

birt reports JavaScript conditional expression

I'm trying to get a formula scripted properly, can some one help me please. I'm using Birt 3.7.1. thanks. This is for a Maximo report
if(row["special"] == 'W' && row["metername"] == null){
false} *** I need this coded --don't hide --> must have a task ***
else{
true}
if(row["special"] == 'W' && row["metername"] != null){
false} *** I need this coded --don't hide --> must have task --> view ***
else if(row["special"] == 'W' true
else true}
I'm not entirely sure what you're asking here, but if you're looking to just get your code here formatted validly, here you go:
if (row["special"] == 'W' && row["metername"] == null) {
return false;
} else {
return true;
}
if (row["special"] == 'W' && row["metername"] != null) {
return false;
} else if (row["special"] == 'W') {
return true;
} else {
return true;
}
I'm not sure its the most practical code, but there you go.

Using != in the FizzBuzz for Javascript

I'm trying to figure out if this application of the != operator is valid for the FizzBuzz problem. Here's my code:
function fizzBuzz(num){
if(num==3%0 && num!=5%0){
return("Fizz");
}
else if(num==5%0 && num!=3%0){
return("Buzz");
}
else if(num==5%0 && num==3%0){
return("FizzBuzz")
}
}
else {
return(num);
}

If Statement Not Acting As Expected

$(document).ready(function(){
logger();
});
function logger()
{
if(localStorage.getItem("status") === null)
{
$("#test").html("Not logged in.");
$("#buttonlogin").click(function(){
var ul = $("#userlogin").val();
var pl = $("#passlogin").val();
$.post("includes/logger.php", {type : "login", user : ul, pass : pl}, function(dlogin){
if(dlogin == 1)
{
$("#outlogin").html("Please enter a username.");
$("#userlogin").focus();
}
else if(dlogin == 2)
{
$("#outlogin").html("Please enter password.");
$("#passlogin").focus();
}
else if(dlogin == 3)
{
$("#outlogin").html("This username doesn't exist.");
$("#userlogin").focus();
}
else if(dlogin == 4)
{
$("#outlogin").html("This username and password don't match.");
$("#userlogin").focus();
}
else
{
localStorage.setItem("status", dlogin);
logger();
}
});
});
$("#buttonregister").click(function(){
var ur = $("#userregister").val();
var pr = $("#passregister").val();
var cpr = $("#confirmpassregister").val();
$.post("includes/logger.php", {type : "register", user : ur, pass : pr, cpass : cpr}, function(dregister){
if(dregister == 1)
{
$("#outregister").html("Please enter a username.");
$("#userregister").focus();
}
else if(dregister == 2)
{
$("#outregister").html("Please enter a password.");
$("#passregister").focus();
}
else if(deregister == 3)
{
$("#outregister").html("Please enter a confirm password.");
$("#cpassregister").focus();
}
else if(dregister == 4)
{
$("#outregister").html("Password and confirm password do not match.");
$("#passregister").focus();
}
else if(dregister == 5)
{
$("#outregister").html("This username is already taken.");
$("#userregister").focus();
}
else
{
localStorage.setItem("status", dregister);
logger();
}
});
});
}
else
{
$("#test").html("You are logged in.");
$("#buttonlogout").click(function(){
localStorage.removeItem("status");
logger();
});
}
}
The above code is meant to check whether or not a localStorage variable is in existence or not. If it is then only allow the log out button to be pressed. If is doesn't then let the two forms to work. Once it is done with either it is supposed to recheck if the variable is set and then do as I said above. However it ignores it when a user logs in and allows the forms to run. If you refresh however it works fine. I cannot for the life of me figure out why this is happening, and it is beginning to piss me off. Any help would be appreciated.
On your else statement, try adding:
$('#buttonlogin').unbind('click');
$('#buttonregister').unbind('click');
If I understand your problem correctly, what's happening is those events are registered when you first run $("#buttonlogin").click(function()....
It doesn't matter that you call logger() again and the if statement is false the second time around. If you want to disable these callbacks you have to do it explicitly.

Categories

Resources