Checking Number Within Specified Range

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of checking whether a given number falls within a specified range. We will use arithmetic comparison to check if the number is in the range and handle cases where the end of the range is not specified. By the end of the lab, you will have a better understanding of how to check if a number is within a given range in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") subgraph Lab Skills javascript/variables -.-> lab-28515{{"`Checking Number Within Specified Range`"}} javascript/data_types -.-> lab-28515{{"`Checking Number Within Specified Range`"}} javascript/arith_ops -.-> lab-28515{{"`Checking Number Within Specified Range`"}} javascript/comp_ops -.-> lab-28515{{"`Checking Number Within Specified Range`"}} javascript/cond_stmts -.-> lab-28515{{"`Checking Number Within Specified Range`"}} end

Function to Check if a Number is Within a Given Range

To check if a number falls within a specified range, use the inRange function. Begin by opening the Terminal/SSH and typing node to start coding.

Here are the steps to use the inRange function:

  1. Use arithmetic comparison to check if the given number is in the specified range.
  2. If the second argument, end, is not specified, the range is considered to be from 0 to start.
  3. The inRange function takes three arguments: n, start, and end.
  4. If end is less than start, the function swaps the values of start and end.
  5. If end is not specified, the function checks if n is greater than or equal to 0 and less than start.
  6. If end is specified, the function checks if n is greater than or equal to start and less than end.
  7. The function returns true if n is within the specified range, and false otherwise.

Here is the inRange function:

const inRange = (n, start, end = null) => {
  if (end && start > end) [end, start] = [start, end];
  return end == null ? n >= 0 && n < start : n >= start && n < end;
};

Here are some examples of how to use the inRange function:

inRange(3, 2, 5); // true
inRange(3, 4); // true
inRange(2, 3, 5); // false
inRange(3, 2); // false

Summary

Congratulations! You have completed the Number in Range lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like