Abbreviate check function - javascript

I'm just starting to learn javascript and i'm trying to figure out how to make this code shorter.
Right now, the check function evaluates if a = "admin" and after that if a = "manager".
Is it possible to do this evaluation in one line?
Kind of "if (a = "admin" or "manager") ..."
const valid = "User name valid";
const invalid = "User name invalid";
function check(a, b) {
if (a === "admin") {
return valid;
} else if (a === "manager") {
return valid;
} else if (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
console.log(check("manager", "ikey"));
console.log(check("admin", "root"));
console.log(check("user", "ikey"));
console.log(check("user", "Mikey"));
Thanks!!

You can use the logical or operator to handle all valid cases together.
function check(a, b) {
if (a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
You can also simplify this to one statement using the ternary operator.
function check(a, b) {
return a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10 ? valid : invalid;
}

You should do it with or operator || like so:
if (a === "admin" || a === "manager" || (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10))
return valid;
else
return invalid;

Related

What means this kind of expression with logics operators

I have an boolean expression in javascript and i don't know what it means.
a = (b === LEFT && -2 || b === RIGHT && 2 || 0)
Please what does it mean ?
The && is a hacky shortcut if:
if (B === LEFT) {
a = -2;
} else if (B === RIGHT) {
a = 2;
} else {
a = 0;
}
one more shortcut with ternary operator
a = b === LEFT? -2: (b === RIGHT? 2 : 0)

Need solution for if statement logic

I need help on a condition logic using if statement as following:
Variables A and B contain 3 properties which is level1, level2 and level3.
level1 and level2 can be 0 or more and level3 is null or numeric.
Variables A and B can be null.
Currently I have this condition:
if (A.level1 == 0 and B.level1 == 0) {
code here
} else if (A.level2 == 0 and B.level2 == 0) {
code here
} else if (A.level3 != null and B.level3 != null) {
code here
}
The problem is that this code doesn't handle the Variables A and B can be null part. The code should handle that part like this:
When A is null, B will still go through the same condition but without A and vice versa.
However, if A and B is null then the condition will be false at once.
I have problem in how to implement the Variables A and B can be null part in my condition, any advice?
Add the A and B isNull check in your condition:
if (A == null && B == null) {
return;
} else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) {
// code here
} else if ((A == null || A.level2 == 0) && (B == null || B.level2 == 0)) {
// code here
} else if ((A == null || A.level3 != null) && (B == null || B.level3 != null)) {
// code here
}
Explanation,
Take A for example in this else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) statement:
By putting the A == null || in (A == null || A.level1 == 0) && (B == null || B.level1 == 0), if A is null, then this A.level1 == 0 check will be ignored, thus the check will be equivalent with else if (B == null || B.level1 == 0).
Since the first if already check A == null && B == null, the else if below won't have A and B both null. Therefore now else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) will be equivalent to else if (B.level1 == 0).
p.s. Here we are taking advantage of the || characteristic, that is if the first condition is fulfilled, the second condition will be ignored.
Seems like this is what you're looking for:
// Both A and B are null
if(A == null && B == null){
// do something
}
// Only A is null
else if(A == null){
if(B.level1 == 0){
// do something
}
else if(B.level2 == 0){
// do something
}
else if(B.level3 != null){
// do something
}
}
// Only B is null
else if(B == null){
if(A.level1 == 0){
// do something
}
else if(A.level2 == 0){
// do something
}
else if(A.level3 != null){
// do something
}
}
// Neither A or B are null
else{
if(A.level1 == 0 && B.level1 == 0){
// do something
}
else if(A.level2 == 0 && B.level1 == 0){
// do something
}
else if(A.level3 != null && B.level3 != null){
// do something
}
}
I suggest to check for falsy values of A or B first and then check the properties.
var A = null,
B = null;
if (!A && !B) {
console.log('A or B is null');
} else if (A.level1 === 0 && B.level1 === 0) {
console.log(1);
} else if (A.level2 === 0 && B.level2 === 0) {
console.log(2);
} else if (A.level3 != null && B.level3 != null) {
console.log(3);
}
Handle A and B separately,
if(!A){
if(A.level1 === 0 && A.level2 ===0){
}
else if(A.level1 === 0){
}
else if(A.level1 > 0){
}
if(A.level2 === 0){
}
else if(A.level2 > 0){
}
if(A.level3 === null){
}
else if (typeof (A.level3) === "number"){
}
}
same for B here...

Simple PIN validation

Task:
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
If the function is passed a valid PIN string, return true, else return false.
My solution:
function validatePIN (pin) {
//return true or false
if (!isNaN(pin) && Number.isInteger(pin) && pin.toString().length == 4 || pin.toString().length == 6) {
return true
} else {
return false
}
}
The only bug I get is when I pass 4 digits as a string ("1234") - it equals false.
function validatePIN (pin) {
return typeof pin === 'string' && // verify that the pin is a string
Number.isInteger(+pin) && // make sure that the string is an integer when converted into a number
[4, 6].includes(pin.length) // only accepts 4 and 6 character pins
}
function validatePIN(pin) {
var isNumber = /^\d+$/.test(pin) && (pin.length == 4 || pin.length == 6)
return isNumber
}
validatePIN('0193')
//returns true
You can use Array.prototype.every(), Array.prototype.some(), String.prototype.match()
<input type="text" />
<button>validate pin</button>
<script>
var validatePIN = (args) => {[...args] = args;
return args.every(v => v.match(/\d/)) &&
[4, 6].some(n => args.length === n)};
document.querySelector("button")
.addEventListener("click", (e) =>
alert(validatePIN(e.target.previousElementSibling.value))
)
</script>
function validatePIN (pin) {
//return true or false
return /^\d+$/.test(pin) && (pin.length === 4 || pin.length === 6)
}
function validatePIN (pin) {
if (pin.length !== 4 && pin.length !== 6) {
return false;
}
for (let i = 0; i < pin.length; i++) {
if (pin[i] > '9' || pin[i] < '0') {
return false;
}
}
return true;
}
Here is another way to solve using regular expression.
function validatePIN(pin) {
return /^(\d{4}|\d{6})$/.test(pin)
}
validatePIN('2345')
//returns true
validatePIN('2.45')
//reutrns false
function validatePIN (pin) {
if (pin.length == 4 || pin.length == 6)
{
for (let i = 0; i < pin.length; i++)
{
if (pin[i] == "0" ||
pin[i] == "1" ||
pin[i] == "2" ||
pin[i] == "3" ||
pin[i] == "4" ||
pin[i] == "5" ||
pin[i] == "6" ||
pin[i] == "7" ||
pin[i] == "8" ||
pin[i] == "9") ;
else return false;
}
return true;
}
else return false;
}
this code checks the PIN-length and passes all test tasks with numbers and not numbers...
public static boolean validatePin(String pin) {
return pin.matches("\\d{4}|\\d{6}");
}

jQuery if condition 1 or condition 2 is true than

I am trying to alert "yes" if ether of the conditions in my if statement are true:
var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?
a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==
=== (Identity operator) is the correct way to compare if both the types are same.
if (a === 1 || b === 1 ) {
=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.
For your or case, You may use || operator
var bootresul = someExpression || anotherExpression
Corrected code
var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
alert('yes');
} else {
alert('no');
}
You are assiging value rather then comparing
Try like this
if (a == 1 || b == 1)
To compare strictly use ===
Like this
if (a === 1 || b === 1)
JSFIDDLE
you can do this
var a = 2;
var b = 1;
if ((a == 1) || (b == 1 )) {
alert('yes');// should alert in this case
} else {
alert('no');
}
the == is one of the relational operator for checking equality and || is a logical operator that is a notion of logical OR
use this to compare just values
if (a == 1 || b == 1){
}
OR use this to compare values and type of variable
if (a === 1 || b === 1){
}
note : == will just check of values and === this will check value with type of variable
var a = 2;
var b = 1;
if (a == 1 || b == 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
I think you were doing assignment instead of comparison
Try using this:
if(a === 1 || b === 1){
alert('YES!')
}else{
alert('NO!')
}
OR you can use ternary operator condition instead of if else
(a == 1 || b == 1) ? alert('YES!') : alert('NO!')

compare three variable using java script

Hi I have three dynamic variable
my below code is working to check if all are equal
if ((a == b) && (b == c)) {
// they're all equal ...
}
But, I want to create a function passing three variable to check if any one variable is equal to other variable.
a=1;b=2;c=1;
isEqual = compareVariable(a,b,c);
here isEqual should be true .
How to create this function
function compareVariable(a,b,c) {
return a==b || b==c || c==a;
}
Try using it like this:
function compareVariable(a,b,c){
var a = a;
var b = b;
var c = c;
if ((a == b) && (b == c)) {
return true
}
else
{
return false;
}
}
compareVariable(1,2,3);
function compareVariable(a,b,c)
{
if ((a == b) || (b == c) || (a == c) ) {
alert("equal");
// they're all equal ...
}
else
{
alert("notequal");
}
}
Try this:
function compareVariable(a,b,c){
if ((a == b) || (b == c) || (a == c)) {
return true
// any or all equal
}
else
{
return false;
// if none of two is equal
}
}
To create a function you need to use the reserved keyword 'function' followed by the name of the function. In this case:
function compareVariable(a,b,c) {
return ((a == b) || (b == c) || (a == c));
}
This function will take your three variables and return boolean true if all the variables are the same.

Categories

Resources