connect 3 different true statements to an IF ELSE - javascript

I want return true if the number is an integer with 4 or 6 digits. no decimals or letter allowed
The thing is not working is the if its really a number check and if its got a decimal.
i think i got already the right functions applied to it but i just cant connect them properly to my if statement.
so i want to check if 3 different things a true then return true but didnt figured out
Please if possible only answer with a hint or a link or SUDO Code or stuff i can look up.
gonna answer the question myself when i figured it out
JS
function validatePIN (pin) {
//return true or false
var result = (pin - Math.floor(pin)) !== 0;
if( pin.length === 4 || isNaN(pin) || result) {
return true
} else if ( pin.length === 6 || isNaN(pin) || result) {
return true
} else return false
}
Thanks

A simple regular expression can be used to test that is is 4 or 6 numbers.
function isValidPin (pin) {
return /^(\d{4}|\d{6})$/.test(pin.toString());
}
console.log(isValidPin(123));
console.log(isValidPin("1234"));
console.log(isValidPin("12345"));
console.log(isValidPin("123456"));
console.log(isValidPin("1234567"));
console.log(isValidPin("12.45"));
console.log(isValidPin("12e45"));

You can check the conditions with the AND operator (&&).
function validatePIN (pin) {
//return true or false
var result = (pin - Math.floor(pin)) !== 0;
if( pin.length === 4 && isNaN(pin) && result)
{ return true} else if ( pin.length === 6 && isNaN(pin) && result) {
return true
} else return false
}

You need to change your or to and
function validatePIN (pin) {
//return true or false
var result = (pin - Math.floor(pin)) !== 0;
if( pin.length === 4 && isNaN(pin) && result)
{ return true}
else if ( pin.length === 6 && isNaN(pin) && result) {
return true
} else return false
}

Ty this:
function validatePIN(pin) {
var parsed = Math.parseInt(pin, 10);
// if it's not an integer
if(pin !== parsed.toString()) return false;
return pin.length === 4 || pin.length === 6;
}

perhaps I'm mistaken - but you could just check if the length is 4 OR 6, and continue your other two checks:
function validatePIN (pin) {
//return true or false
var result = (pin - Math.floor(pin)) !== 0;
if(!isNaN(pin) && (pin.length === 4 || pin.length === 6) && result) {
return true
} else return false
}
I've also edited your code, as it seemed illogical returning true for NaN.

I think you want this. You should use regular expression for simplicity.
console.log(445584, validatePin(445584));
console.log("445584", validatePin("445584"));
console.log("alj454", validatePin("alj454"));
console.log(4455.84, validatePin(4455.84));
function validatePin(pin){
return /^(\d{4}|\d{6})$/.test(pin);
}

You might want to look into using isNaN() to detect if there are any characters that aren't numbers.
Also, using .toString() so you can check the .length.

Related

Checking same case in JavaScript

I was trying to solve a code wars kata called "Check same case", and I'm not understanding what is wrong with my solution and why it doesn't pass.
The function should check:
If either of the characters is not a letter, return -1
If both characters are the same case, return 1
If both characters are letters, but not the same case, return 0
Here's my solution:
function sameCase(a, b) {
if (!a.match(/[A-z]/g) || !b.match(/[A-z]/g)) {
return -1
} else if ((a.match(/[A-Z]/g) && b.match(/[A-Z]/g)) || (a.match(/[a-z]/g) && b.match(/[a-z]/g))) {
return 1
} else {
return 0
}
}
The solution passes in all the tests, but when I click on "Attempt" I get these two errors:
For sameCase("^","B"): expected 0 to equal -1
function sameCase(a, b) {
if (!a.match(/[A-z]/g) || !b.match(/[A-z]/g)) {
return -1
} else if ((a.match(/[A-Z]/g) && b.match(/[A-Z]/g)) || (a.match(/[a-z]/g) && b.match(/[a-z]/g))) {
return 1
} else {
return 0
}
}
console.log(sameCase("^", "B"));
For sameCase("]","D"): expected 0 to equal -1
function sameCase(a, b) {
if (!a.match(/[A-z]/g) || !b.match(/[A-z]/g)) {
return -1
} else if ((a.match(/[A-Z]/g) && b.match(/[A-Z]/g)) || (a.match(/[a-z]/g) && b.match(/[a-z]/g))) {
return 1
} else {
return 0
}
}
console.log(sameCase("]", "D"));
As anticipated in the comments, your solution fails the tests because the range you use in your RegExp ([A-z] = from char code 65 to char code 122) includes also some non-alphabetic characters, such as the ones used in the tests: ] (char code 93) and ^ (char code 94).
That said, your solution presents also another issue as you are not checking that the arguments passed to the function are single characters rather than strings. In fact:
function sameCase(a, b) {
if (!a.match(/[A-z]/g) || !b.match(/[A-z]/g)) {
return -1
} else if ((a.match(/[A-Z]/g) && b.match(/[A-Z]/g)) || (a.match(/[a-z]/g) && b.match(/[a-z]/g))) {
return 1
} else {
return 0
}
}
console.log(sameCase("0123A", "A4567")); // will return 1 because the only letter of both string is in the same case
Solution
You can solve both issues simply by tuning the RegExp:
function sameCase(a, b) {
if (!a.match(/^[A-Za-z]$/) || !b.match(/^[A-Za-z]$/)) {
return -1
} else if (
(a.match(/^[A-Z]$/) && b.match(/^[A-Z]$/)) ||
(a.match(/^[a-z]$/) && b.match(/^[a-z]$/))
) {
return 1
} else {
return 0
}
}
console.log(sameCase('D', ']'));
console.log(sameCase('D', '^'));
console.log(sameCase('0123A', 'A4567'));
Although in a case like this I would tend to use RegExp.prototype.test(), which returns a boolean, because there is no real use for the array returned by String.prototype.match():
function sameCase(a, b) {
if (!/^[A-Za-z]$/.test(a) || !/^[A-Za-z]$/.test(b)) {
return -1
} else if (
(/^[A-Z]$/.test(a) && /^[A-Z]$/.test(b)) ||
(/^[a-z]$/.test(a) && /^[a-z]$/.test(b))
) {
return 1
} else {
return 0
}
}
console.log(sameCase('D', ']'));
console.log(sameCase('D', '^'));
console.log(sameCase('0123A', 'A4567'));
[A-Za-z] will check that the sub-strings contain only Latin letters and the use of ^ and $ to indicate the beginning and the ending of the string will check that they are only one character each.
Note
Please ignore my comment about the use of String.prototype.charCodeAt(), as I misread the requirements and assumed the function had to check whether the two arguments were the same letter in the same case... and even in that case, you could check it with a strict equality operator (a === b) with no need to over-complicate it with charCodeAt(). I do not really know what I was thinking. :P

Only evaluate parts of a condition when the corresponding flag is set

For example, I have flag1, flag2, flag3,..., flagN as boolean values for flags.
I need to write an if statement like this: If flagK is false, then turn off the "K" part of the condition:
if (condition0 && (flag1 && condition1) && (flag2 && condition2) ... && (flagN && conditionN))
{
// do something
}
// For example, if flag 2 is false then the condition should only be:
if (condition0 && (flag1 && condition1) && ... && (flagN && conditionN))
{
//do something}
}
Particularly, given an example like this (for demo only not my real problem):
const divby2 = false; //if this is false, then ignore the **i%2 === 0** below
const divby3 = true;
const divby4 = true;
const divby5 = true;
//......const divbyN....
const array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,42,45,241,526]
array.forEach((i) => {
if(i >= 0 && (divby2 && i%2 === 0) && (divby3 && i%3 === 0)) {
console.log(i) // output here should be 3,6,9,12 instead of nothing
}
}
)
Example Result:
The term you are looking for is "short-circuit" similar to the way in real electronic circuit if some part is not working you just short circuit it and by pass flow to rest
if(i >= 0 && ( divby2 && i%2 === 0 || !divby2) && (divby3 && i%3 === 0))
In this case if you are wanting that filtered number should be divisible by 2 that time you set divby2 = true
And when you just want to ignore and don't care about the divisibility by 2 you set divby2 = false
In pseudo
(is-checking-for-divby-2? AND is-current-number-divby-2?) OR (NOT is-checking-for-divby-2?)
As soon as you are not checking for divby 2 you make this logic fragment true so it won't affect evaulation of the follolwing logic fragments
And..Why should you bother making this fragments TRUE?
Because you ANDing them
Similarly you can go for divby3, divby4 ...
I would have an object with your conditions, and then filter out the functions you don't want to run, and then just reduce the object to a single function which runs all of the enabled functions:
const conditions = {
divby2: i => i % 2 === 0,
divby3: i => i % 3 === 0,
};
const enabled = {
divby2: false, //if this is false, then need to ignore the **i%2 === 0** below
divby3: true
}
const enabledConditions = (i) => {
return Object.entries(conditions).filter(
([key, value]) => enabled[key]
).reduce((carry, [_, condition]) => {
return condition(i) && carry
}, i !== false);
}
//......const divbyN....
const array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,42,45,241,526]
array.forEach((i) => {
if(i >= 0 && enabledConditions(i)){
console.log(i) //output here should be 3,6,9,12 instead of nothing
}
}
)

Why does function return undefined when it explicitly is told to return true?

I was trying to solve the following coding exercise.
We have two special characters. The first character can be represented
by one bit 0. The second character can be represented by two bits (10
or 11).
Now given a string represented by several bits. Return whether the
last character must be a one-bit character or not. The given string
will always end with a zero.
example:
Input: bits = [1, 0, 0] Output: True
Below is my solution for the above challenge. Why is this returning undefined? If I use [1,0,1,0] as input, it should return true but I am getting undefined. I am explicitly writing true in the return statement and not the results of a variable.
var isOneBitCharacter = function(bits) {
console.log(bits);
var length = bits.length;
if (length == 1) {
return true;
}
if (length == 0) {return false;}
if (length == 2 && bits[0] === 1) {
return false;
}
if (bits[0] === 1) {
isOneBitCharacter(bits.slice(1));
} else {
isOneBitCharacter(bits.slice(2));
}
};
isOneBitCharacter([1,0,1,0]);
I guess you are missing returns. Here is adjusted code:
var isOneBitCharacter = function(bits) {
console.log(bits);
var length = bits.length;
if (length == 1) {
return true;
}
if (length == 0) {return false;}
// added return here and next statements
if (length == 2 && bits[0] === 1) {
return false;
}
if (bits[0] === 1) {
return isOneBitCharacter(bits.slice(1));
} else {
return isOneBitCharacter(bits.slice(2));
}
};
isOneBitCharacter([1,0,1,0]);

Regex in javascript global catch failed [duplicate]

This question already has answers here:
regex has no method test [closed]
(3 answers)
Closed 7 years ago.
var r = new RegExp('^[0-9]*$');
function validatePIN (pin) {
//return true or false
if(r.test(pin) && pin.length == 4 || pin.length == 6){
return true;
}else{
return false;
}
}
console.log(validatePin(3627i1));
What's wrong with my regex above? I expect true in this case.
Nothing much is wrong but these conditions will not evaluate correctly:
r.test(pin) && pin.length == 4 || pin.length == 6
As it effectively means (r.test(pin) && pin.length == 4) || pin.length == 6 due to && at higher operator precedence than ||.
You need to enclose conditions in parentheses:
r.test(pin) && (pin.length == 4 || pin.length == 6)
btw you don't even need to check for length afterwards as you can do that in regex itself:
var r = /^[0-9]{4}(?:[0-9]{2})?$/
Now this regex will match either 4 digit number or a six digit number.
validatePin is not the same as validatePIN and 3627i1 is not the same as '3627i1'.
So make sure that you are calling the correct function and passing it a correct argument:
console.log(validatePIN('3627i1'));
syntax errors in your code,
var r = new RegExp('^[0-9]*$');
function validatePIN (pin) {
//return true or false
if(r.test(pin) && pin.length == 4 || pin.length == 6){
return true;
}else{
return false;
}
}
console.log(validatePIN("3627i1")); //call valid function, parameter = string

How do I check if a JavaScript parameter is a number?

I'm doing some trouble-shooting and want to add a check that a parameter to a function is a number. How do I do this?
Something like this...
function fn(id) {
return // true iff id is a number else false
}
Even better is if I can check that the parameter is a number AND a valid integer.
function fn(id) {
return typeof(id) === 'number';
}
To also check if it’s an integer:
function fn(id) {
return typeof(id) === 'number' &&
isFinite(id) &&
Math.round(id) === id;
}
i'd say
n === parseInt(n)
is enough. note three '===' - it checks both type and value
Check if the type is number, and whether it is an int using parseInt:
if (typeof id == "number" && id == parseInt(id))
=== means strictly equals to and == checks if values are equal.
that means "2"==2 is true but "2"===2 is false.
using regular expression
var intRegex = /^\d+$/;
if(intRegex.test(num1)) {
//num1 is a valid integer
}
example of
== vs. ===
function fn(id){
if((parseFloat(id) == parseInt(id)) && !isNaN(id)){
return true;
} else {
return false;
}
}
function fn(id) {
var x = /^(\+|-)?\d+$/;
if (x.test(id)) {
//integer
return true;
}
else {
//not an integer
return false;
}
}
Test fiddle: http://jsfiddle.net/xLYW7/

Categories