conditional "if" "else" statement for string "number" and real "number" - javascript

I am attempting to make a conditional statement where the code will only run if it is a real number 90 vs a string number "90". I don't know what to type into the number part in my conditional statement. I have tried using Number and I have also tried variations of typeof() method. My code is listed below. Thank you
function sumAll(start, end) {
let x = 0;
array = [];
if (start === Number && end === Number) {
if (start < end && start >= 0) {
for (let i = start; i <= end; i++) {
array.push(i);
console.log(array);
x = i + x;
}
return x;
} else if (start > end) {
for (let i = start; i >= end; i--) {
array.push(i);
// console.log(array);
x = i + x;
}
return x;
} else {
return 'ERROR';
}
} else {
return 'ERROR not a number';
}
}
console.log(sumAll(10, 90));
sumAll(10, "90")
// expect(sumAll(10, "90")).toEqual('ERROR');
```

let n = 5;
console.log(n === Number);
console.log(typeof n === "number");
The first one logs false, the second one true.
You should try using typeof to check if start and end are numbers in your code.

You need to use the typeof operator
Using it, you can check to see if start and end are 'number'
function sumAll(start, end) {
let x = 0;
array = [];
if (typeof start === 'number' && typeof end === 'number') {
if (start < end && start >= 0) {
for (let i = start; i <= end; i++) {
array.push(i);
console.log(array);
x = i + x;
}
return x;
} else if (start > end) {
for (let i = start; i >= end; i--) {
array.push(i);
// console.log(array);
x = i + x;
}
return x;
} else {
return 'ERROR';
}
} else {
return 'ERROR not a number';
}
}
console.log(sumAll(10, 90));
console.log(sumAll(10, "90"));
// expect(sumAll(10, "90")).toEqual('ERROR');}

I think you used typeof wrongly, because that is the answer you need here.
const a = 5;
const b = '5';
if (typeof a === 'number') { } //true
if (typeof a === 'string') { } //false
if (typeof b === 'number') { } //false
if (typeof b === 'string') { } //true
console.log('type of a: ', typeof a);
console.log('type of b: ', typeof b);
But it seems like you can use string '90' as well converting it into number.
function sumAll(start, end) {
let x = 0;
array = [];
if (typeof start === 'string') { start = parseInt(start, 10); } // case '90'
if (typeof end === 'string') { end = parseInt(end, 10); } // case '90'
if (Number.isNaN(start) || Number.isNaN(end)) {
// Error case
return 'Error not a number'; // Throw could be better here though
}
// any operation you need to do is here
// if you need to sum number between start and end its called
// For example summing operation
return end > start
? (end * (end + 1) / 2) - ((start - 1) * start / 2)
: (start* (start + 1) / 2) - ((end- 1) * end / 2)
}
console.log(sumAll(10, 12));

Related

How do i return the result of all loops in javascript?

I am trying to insert dashes ('-') between each two odd numbers and insert asterisks ('*') between each two even numbers, but I am only getting the last result.
I want to print out all the elements in the array.
For example: if num is 4546793 the output should be 454*67-9-3. I Did not count zero as an odd or even number.
function StringChallenge(num) {
let result = "";
for (let i = 0; i < num.length; i++) {
if (num[i] === 0) {
continue;
}
if (num[i - 1] % 2 == 0 && num[i] % 2 == 0) {
result = num[i - 1] + "*" + num[i];
continue;
}
if (num[i - 1] % 2 == !0 && num[i] % 2 == !0) {
result = num[i - 1] + "-" + num[i];
continue;
}
}
return result;
}
console.log(StringChallenge([4,5,4,6,7,9,3]));
You do not need to check as if&continue. Inserting given numbers to the result string and only adding "-" when index and previous are odd, and "*" when index and previous are even.
function StringChallenge(num) {
let result = "";
for (let i = 0; i < num.length; i++) {
if (num[i]%2 ===0) {// even
if(i !== 0 && num[i-1]%2===0){// previous is even either
result+="*"+num[i];
}else{
result+=num[i];
}
}else{// odd
if(i !== 0 && num[i-1]%2===1){// previous is odd either
result+="-"+num[i];
}else{
result+=num[i];
}
}
}
return result;
}
console.log(StringChallenge([4,5,4,6,7,9,3]));
Try this :)
function test(a){
let result=""
for(let i=0; i < a.length; i++){
if(a[i] != 0 && a[i-1] % 2 == 0 && a[i] % 2 == 0){
result = result + '*' + a[i]
}
else if (a[i] != 0 && a[i-1] % 2 != 0 && a[i] % 2 != 0){
result = result + '-' + a[i]
}
else{
result = result + a[i]
}
}
return result
}
console.log(test([4,5,4,6,7,9,3]));
As everyone has identified, the problem is you are not adding to result.
But here is a suggestion to make your code easier to read
// These one line functions make your code easier to read
function IsEven(num){
return num % 2 === 0;
}
function IsOdd(num){
return num % 2 !== 0;
}
function StringChallenge(numArray) {
// return empty string if not an array or empty array
if(!Array.isArray(numArray) || numArray.length === 0) return "";
let result = "" + numArray[0]; // use "" to coerce first element of numArray from number to string
for (let i = 1; i < numArray.length; i++) {
// focus on the conditions to determine the separator you want between each element
separator = "";
if (numArray[i] !== 0) {
if (IsEven(numArray[i]) && IsEven(numArray[i - 1])) {
separator = "*";
} else if (IsOdd(numArray[i]) && IsOdd(numArray[i - 1])){
separator = "-";
}
}
// build the result
result += separator + numArray[i];
}
return result;
}
I will do that this way :
== some advices for 2 cents ==
1 - try to make your code as readable as possible.
2 - use boolean tests rather than calculations to simply do a parity test
3 - ES7 has greatly improved the writing of JS code, so take advantage of it
console.log(StringChallenge([4,5,4,6,7,9,3])); // 454*67-9-3
function StringChallenge( Nums = [] )
{
const
isOdd = x => !!(x & 1) // Boolean test on binary value
, isEven = x => !(x & 1) && x!==0 // zero is not accepted as Even value
;
let result = `${Nums[0]??''}`; // get first number as
// result if Nums.length > 0
for (let i=1; i<Nums.length; i++)
{
if ( isOdd(Nums[i-1]) && isOdd(Nums[i]) ) result += '-';
if ( isEven(Nums[i-1]) && isEven(Nums[i]) ) result += '*';
result += `${Nums[i]}`; // same as Nums[i].toString(10);
}
return result
}
I hope this helps. I tried to keep it as simple as possible.
function StringChallenge(num) {
//start with a string to concatenate, or else interpreter tries to do math
operations
let result = num[0].toString();
function checkOdd(num){ //helper function to check if odd
return num % 2
}
for (let i = 0; i < num.length - 1; i++) {
if (checkOdd(num[i]) && checkOdd(num[i+1])) { //checks if both odd
result += `-${num[i+1]}`; //adds - and next number
} else if (!checkOdd(num[i]) && !checkOdd(num[i+1])) { //checks if both even
result += `*${num[i+1]}`; //adds * and next number
} else { //otherwise
result += num[i+1]; //just add next number
}
}
return result;
}
console.log(StringChallenge([4,5,4,6,7,9,3]));
Use +=. And, change your logic, your code prints out "4*67-99-3".
The zero check was pretty hard for me I hope the variables in my code explain itself. If not, let me know.
function even(num) {
return num % 2 === 0;
}
function odd(num) {
return num % 2 !== 0;
}
function StringChallenge(num) {
let result = "";
for (let i = 0; i < num.length; i++) {
var currentZero = num[i] === 0
var previousZero = num[i-1] === 0
var bothEven = even(num[i]) && even(num[i-1])
var bothOdd = odd(num[i]) && odd(num[i-1])
var firstNumber = (i === 0)
if (!currentZero) {
if (firstNumber) {
result += num[i]
} else {
if (bothEven && !previousZero) {
result += "*" + num[i]
} else if (bothOdd && !currentZero) {
result += "-" + num[i]
} else {
result += num[i]
}
}
}
}
return result;
}
console.log(StringChallenge([0,4,5,0,4,6,7,9,3]));

Find and print the biggest prime number (JS)

I'm studying node.js and have some interesting task - Write a program that finds and prints the biggest prime number which is <= N.
Input // Output - 13 // 13
126 // 113
26 // 23
In last course with java i have the same task and my code is really simple:
import java.util.Scanner;
public class BiggestPrimeNumber {
public static void main(String[] args){
int n;
Scanner in = new Scanner(System.in);
n=in.nextInt();
while(prim(n) == false){
n--;
}
System.out.println(n);
}
public static boolean prim(int m){
int n=m;
for(int i=2;i<n;i++){
if(n%i == 0){
return false;
}
}
return true;
}
}
I try similar way to test it, but I'm don't have idea how to convert it:
let n = 126;
while (isPrime(n) === false) {
n -= 1;
}
console.log(n);
let n = m;
for (let i = 2; i < n; i += 1) {
if (n % i === 0) {
return false;
}
}
return true;
Can you help me, because I'm really have problem with js using in console.
I think this is what you want. You only need to declare a function and use it as you are doing.
let n = 126;
while (isPrime(n) === false) {
n -= 1;
}
console.log(n);
function isPrime(m) {
let n = m;
for (let i = 2; i < n; i += 1) {
if (n % i === 0) {
return false;
}
}
return true;
}
If your running it with NodeJS in console, you can save it in a file called prime.js (for example) and execute it with: node prime.js.
You can pass parameters to the script like: node prime.js 126 and then get them in the code. That will be something like that:
const args = process.argv;
let n = args[2];
while (isPrime(n) === false) {
n -= 1;
}
console.log(n);
function isPrime(m) {
let n = m;
for (let i = 2; i < n; i += 1) {
if (n % i === 0) {
return false;
}
}
return true;
}
You're pretty close. First off, you don't have isPrime defined. Second, if you were to paste all of your code into the browser console, it isn't going to like that you are defining n twice. I also cleaned up your isPrime bit of code.
let n = 100;
let result = n;
const isPrime = num => {
for(let i = 2; i < num; i++)
if(num % i === 0) return false;
return num !== 1 && num !== 0;
}
while (isPrime(result) === false) {
result -= 1;
}
console.log(result + " is the next prime below " + n);
Also, remember that javascript is not a compiled language, so unless you are defining your function in a class, the browser will interpret the code sequentially. Therefore, you have to have isPrime defined before you use it.
The algorithm to find the nearest prime number can be further optimized. All prime numbers are of the form 6k+1 or 6k-1 except the numbers 2 and 3. Also, instead of checking all the way to the number the check can be made till Sqrt(n). Here is the modified isPrime function:
let n = 126;
while (isPrime(n) === false) {
n -= 1;
}
console.log(n);
function isPrime(num) {
if (num <= 1) return false;
if (num < 4) return true;
if (num%2 === 0 || num%3 === 0) return false;
for (var i = 5; i*i <= num; i+=6) {
if (num % i === 0 || num % (i + 2) === 0)
return false;
}
return true;
}

JS: Using i as an Index of a String in a Loop Is Returning an Integer, Not the Letter

Learning JS/programming and running through some basic exercises.
This one is to determine if a word is a palindrome (reads the same forwards as backwards).
My problem is that the function is returning false even when I input a palindrome. From looking at it stepping over, it seems string[i] is giving an integer to compare, instead of the character at that current index.
let input = "hannah";
let firstCharacter = input[0];
let lastIndex = input.length -1;
let lastCharacter = input[lastIndex];
function palTest(string) {
let j = string[lastIndex];
if (string.length % 2 === 0 )
{
for (i = 0; i <= string.length / 2; i++) {
if (string[i] === string[j]) {
j--;
return true;
} else {
return false;
}
}
} else {
let middleCharacter = (string.length + 1) / 2;
for (i = 0; i <= ((string.length + 1) / 2) - 1; i++) {
if (string[i] === string[j] && middleCharacter == "a" || "e" || "i" || "o" || "u" ) {
j--;
return true;
} else {
return false;
}
}
}
}
let x = palTest(input);
console.log(x); // false
console.log(input[0]); // h
console.log(input[1]); // a
console.log(input[2]); // n
console.log(input[3]); // n
console.log(input[4]); // a
console.log(input[5]); // h
Inside the for loop on the first loop, hannah[i] is, I think, 0 instead of "h": so it's comparing 0 to "h" (hannah[j]) and returning false?
You need to decrement lastIndex instead of value at last index
function palTest(string) {
var lastIndex = string.length - 1; //initialize lastIndex here
let j = string[lastIndex];
if (string.length % 2 === 0) {
for (i = 0; i <= string.length / 2; i++) {
if (string[i] === string[lastIndex]) { //compare with lastIndex
lastIndex--; //decrement last index
return true;
} else {
return false;
}
}
} else {
let middleCharacter = (string.length + 1) / 2;
for (i = 0; i <= ((string.length + 1) / 2) - 1; i++) {
if (string[i] === string[lastIndex] && middleCharacter == "a" || "e" || "i" || "o" || "u") {
lastIndex--;
return true;
} else {
return false;
}
}
}
}
console.log( palTest("hannah") );
console.log( palTest("hantnah") );
console.log( palTest("Not a palindrome") );
A less verbose method could be
var input = "hannah";
var fnCheckPal = ( input ) => input == input.split( "" ).reverse().join("");
Demo
var fnCheckPal = (input) => input == input.split("").reverse().join("");
console.log( fnCheckPal( "hannah" ) );
console.log( fnCheckPal( "hantnah" ) );
console.log( fnCheckPal( "hann33ah" ) );
I would suggest simplifying your logic a bit, as you only need one loop, one variable and no global stored things for this. Just loops once, check if the current index equals the same index from the end and if it's not, it's not a palindrome. Even or uneven amount of letters don't even make a difference here:
function testPalindrome( string ){
for( let i = 0; i < string.length / 2; i++ ){
if( string[ i ] !== string[ string.length - 1 - i ] ){
return false;
};
}
return true;
}
console.log( testPalindrome( 'hannah' ) ); // true
console.log( testPalindrome( 'aba' ) ); // true
console.log( testPalindrome( 'stackoverflow' ) ); // false
Some thoughts:
no need to differ of strings with odd or even length, by using an approach with counting from start and from end,
only one loop,
take the index until the middle of the string,
check if unequal then exit function with false,
if end of function is reached, return with true, because the string is a palindrome.
function palTest(string) {
var i, l;
for (i = 0, l = Math.floor(string.length / 2); i < l; i++) {
if (string[i] !== string[string.length - 1 - i]) {
return false;
}
}
return true;
}
console.log(palTest('hannah'));
console.log(palTest('foo'));
BTW, a check with this pattern,
middleCharacter == "a" || "e" || "i" || "o" || "u"
checks only the first part with the comparison and if not true, the next string "e" is taken as truthy value as return value of the condition.
At all, there happens no real check.

How to convert lowercase uppercase?(with if conditions on different digits.)

Hi This is my first time using this website, I did do some research about how to convert lowercase letter to uppercase letter but still filles. The requirement is to check if "even", covert the even digit letter to different type(lower to upper or upper to lower). below is my code:
function question4(str,pos)
{ var newLetter;
var kkk=str;
if (pos='even')
{
for (var i=0;i<str.length;i=i+2)
{
if (str[i].toString()==str[i].toString().toUpperCase())
{
newLetter=str[i].toString().toLowerCase();
kkk[i]=newLetter;
}else
{
newLetter=str[i].toUpperCase();
kkk[i]=newLetter;
}
}
}else if (pos='odd')
for ( i=0;i<str.length;i=i+2)
{
if (str[i]===str[i].toLowerCase())
{
alert('3');
}else if (str[i]===str[i].toUpperCase())
{
alert('4');
}
}
return kkk;
}
the requirement is: Write a function to change the case of all the characters in string based on their position which matches the value of the pos parameter function. function (str, pos [even|odd]). Example ( (‘abCd’, ‘odd’) return Abcd)
Update: now I have make "odd" condition working, but "even "still is not working, can any one take a look why?
function question4(strr,pos) {
var result ;
var sum="";
var aaa;
for (var i = 0; i <= strr.length - 1; i = i + 1)
{
if (pos == "odd"&&i%2==0)
{ aaa=strr.charCodeAt(i);
if (aaa >= 65 && aaa <= 90 )
{
result = String.fromCharCode(aaa + 32);
} else
result = String.fromCharCode(aaa - 32);
}
else if (pos == "even"&&i%2==1)
{
if (aaa >= 65 && aaa <= 90 )
{
result= String.fromCharCode(aaa + 32);
} else
result = String.fromCharCode(aaa - 32);
}else result=strr[i];
sum+=result;
}
return sum;
}
To achieve this, you can construct a string by concating char by char:
function question4(strInput, pos) {
let str = ""; // The string to construct
if (!pos || (pos !== "even" && pos !== "odd")) { // Validating pos
throw "invalid pos";
}
for (var i=0;i<strInput.length;i++) // Looping on strInput
{
let huPos = i + 1;
if ((pos === "even" && huPos%2 == 1) ||
(pos === "odd" && huPos%2 == 0)) {
/* If we want switch odd and we are on even position or if we want switch even and we are on odd position, then we add the original char
*/
str += strInput[i];
}
else {
// In others case, we switch lower to upper and upper to lower
let char = strInput[i];
str += char == char.toUpperCase() ? char.toLowerCase() : char.toUpperCase();
}
}
return str;
}
console.log(question4('abCdef', "odd")); // Return "AbcdEf"
Associated bin
EDIT:
After seeing edit, i can see you want to do it without using toLower/UpperCase. As stated in comment i think it is a bad idea in js, but to experiment you can achieve this:
const reverser = {
"a": "a".charCodeAt(0),
"z": "z".charCodeAt(0),
"A": "A".charCodeAt(0),
"Z": "Z".charCodeAt(0),
};
const conversionValueToLower = reverser.a - reverser.A;
const conversionValueToUpper = reverser.A - reverser.a;
function reverseChar(char) {
var code = char.charCodeAt(0);
// If you want to go from upper to lower
if (code >= reverser.A && code <= reverser.Z) {
// Simply add the difference between lower and upper
return String.fromCharCode(code + conversionValueToLower);
} // Same logic here
else if (code >= reverser.a && code <= reverser.z) {
return String.fromCharCode(code + conversionValueToUpper);
}
/**
Or use if you want full digit
if (code <= 90 && code >= 65) {
return String.fromCharCode(code + 32);
}
else if (code >= 97 && code <= 122) {
return String.fromCharCode(code - 32);
}
**/
return char; // Other case return original char
}
function question4(strInput, pos) {
let str = "";
if (!pos || (pos !== "even" && pos !== "odd")) {
throw "invalid pos";
}
for (var i=0;i<strInput.length;i++)
{
let huPos = i + 1;
if ((pos === "even" && huPos%2 == 1) ||
(pos === "odd" && huPos%2 == 0)) {
str += strInput[i];
}
else {
str += reverseChar(strInput[i]);
}
}
return str;
}
console.log(question4('abCdef', "odd")); // return "AbcdEf"
Associated bin
Another way could be to code utils functions imitating toLower/UpperCase
I corrected your code in your answer aswell, without changing original logic
function question4(strr,pos) {
var result ;
var sum="";
var aaa;
for (var i = 0; i <= strr.length - 1; i = i + 1)
{
if (pos == "odd"&&i%2==0)
{ aaa=strr.charCodeAt(i);
if (aaa >= 65 && aaa <= 90 )
{
result = String.fromCharCode(aaa + 32);
} else if(aaa >=97&&aaa <=122)
{ result = String.fromCharCode(aaa - 32);}
else {result=strr[i];}
}
else if (pos == "even"&&i%2==1)
{ aaa=strr.charCodeAt(i);
if (aaa >= 65 && aaa <= 90 )
{
result= String.fromCharCode(aaa + 32);
} else if(aaa >=97&&aaa <=122)
{ result = String.fromCharCode(aaa - 32);}
else {result=strr[i];}
}else {result=strr[i];}
sum+=result;
}
return sum;
}
console.log(question4("abCd", "odd")) // return Abcd;
A simple solution for this question
// Function used to invert the letter case
const changeCase = c => {
if (c === c.toUpperCase()) return c.toLowerCase()
return c.toUpperCase()
}
const swapCaseConditional = (str, pos) => {
// Use split method to convert string into array and map the array
return str.split('').map((c, index) => {
if (pos === 'even') {
// if pos and index are even, change the letter case
if (index % 2) return changeCase(c)
return c
}
else {
// if pos and index are odd, change the letter case
if (!(index%2)) return changeCase(c)
return c
}
// Convert to string
}).join('')
}
console.log(swapCaseConditional('abCd', 'odd'))
I worked two nights and finally got it working. although not fully cover all the situations, but almost there.
function question4(strr,pos) {
var result ;
var sum="";
var aaa;
for (var i = 0; i <= strr.length - 1; i = i + 1)
{
if (pos == "odd"&&i%2==0)
{ aaa=strr.charCodeAt(i);
if (aaa >= 65 && aaa <= 90 )
{
result = String.fromCharCode(aaa + 32);
} else
result = String.fromCharCode(aaa - 32);
}
else if (pos == "even"&&i%2==1)
{ aaa=strr.charCodeAt(i);
if (aaa >= 65 && aaa <= 90 )
{
result= String.fromCharCode(aaa + 32);
} else if(aaa >=97&&aaa <=122)
{ result = String.fromCharCode(aaa - 32);}
else {result=strr[i];}
}else {result=strr[i];}
sum+=result;
}
return sum;
}

Compare two numeric range in javascript

I have two number range.
Lets say 1st rage is 6-9 and second is 1-15
I need to check that if it is conflicting. I mean if 1-15 is crossing 6-9,
Valid ranges are
1-5,
10-15
but 1-15, 2-18 like this should return me that it is violating 6-9.
Currently I am checking only signle digit if it falls between range,
if (typeof (Number.prototype.isBetween) === "undefined") {
Number.prototype.isBetween = function (min, max, notBoundaries) {
var between = false;
if (notBoundaries) {
if ((this < max) && (this > min)) between = true;
alert('notBoundaries');
} else {
if ((this <= max) && (this >= min)) between = true;
alert('Boundaries');
}
alert('here');
return between;
}
}
But now I need to check range. Any help is appreciated
Making use of your function, I added a new function to compare ranges
if (typeof (Number.prototype.isBetween) === "undefined") {
Number.prototype.isBetween = function (min, max, notBoundaries) {
var between = false;
if (notBoundaries) {
if ((this < max) && (this > min)) between = true;
} else {
if ((this <= max) && (this >= min)) between = true;
}
return between;
}
}
if (typeof (Array.prototype.isRangeCrossed) === "undefined") {
Array.prototype.isRangeCrossed = function (target,notBoundaries) {
var result = false;
if ((target[0].isBetween(this[0],this[1],notBoundaries) ) || (target[1].isBetween(this[0],this[1],notBoundaries))){
result = true;
}
return result;
}
}
var range1 = [6,9];
var range2 = [1,15];
var range3 = [2,5];
console.log(range2.isRangeCrossed(range1,false));
console.log(range3.isRangeCrossed(range1,false));
If you want a pure function, this should suffice:
var rangesOverlap = function(n1, n2, r1, r2, boundaries) {
if (boundaries) {
return n1 >= n2 || n2 >= r1 || r1 >= r2;
} else {
return n1 > n2 || n2 > r1 || r1 > r2;
}
}
n1 and n2 are the first range, r1 and r2 are the second range, and boundaries indicate allowing overlaps on the boundary.
On the Array prototype, where comp is the 2nd range array:
Array.prototype.rangesOverlap = function(comp, boundaries) {
if (boundaries) {
return this[0] > this[1] || this[1] > comp[0] || comp[0] > comp[1];
} else {
return this[0] >= this[1] || this[1] >= comp[0] || comp[0] >= comp[1];
}
}
See this JSFiddle for a working example.
You can simplify your range checking code.
if (typeof(Number.prototype.isBetween) === 'undefined') {
Number.prototype.isBetween = function(min, max, inclusive) {
return inclusive ? (this <= max && this >= min) : (this < max && this > min);
}
}
var range = { min : 7, max : 12 };
var validMinInclusive = range.min.isBetween(range.min, range.max, true);
var validMaxInclusive = range.max.isBetween(range.min, range.max, true);
var invalidMinExclusive = range.min.isBetween(range.min, range.max, false);
var invalidMaxExclusive = range.max.isBetween(range.min, range.max, false);
console.log(validMinInclusive, validMaxInclusive, invalidMinExclusive, invalidMaxExclusive);
The following code constructs a Range object with a method for checking if another range fits inside of it. There is an inclusive flag like the one used in the above code.
function Range(min, max) {
if (arguments.length === 0) {
throw 'Range must include at least one value.';
}
if (arguments.length === 1) {
max = min;
min = 0;
}
this.min = min;
this.max = max;
}
Range.inBounds = function(outer, inner, inclusive) {
return inclusive
? (inner.min >= outer.min && inner.max <= outer.max)
: (inner.min > outer.min && inner.max < outer.max);
};
Range.prototype.inBounds = function(target, inclusive) {
return Range.inBounds(this, target, inclusive);
};
var rangeInner = new Range(6, 9);
var rangeOuter = new Range(1, 15);
var validRangeBounds = rangeOuter.inBounds(rangeInner, false); // true
var invalidRangeBounds = rangeInner.inBounds(rangeOuter, false); // false
console.log(validRangeBounds, invalidRangeBounds);

Categories

Resources