What is the best way to locate the desired number from a group of three numbers in JavaScript?

Is there a way to determine if "x" is equal to any of the numbers 15, 30, 70, or 140?

const x =....;
if(x===?){....}else{....}

Answer №1

For the first method, utilize the includes function:

const nums = [15, 30, 70, 140];
const x = 70;
if (nums.includes(x)) console.log("Yay :)")
else console.log("Noo :(");

If you have the numbers as a space-separated string, apply this code:

const nums = "15 30 70 140".split(" ").map(e => parseInt(e));
const x = 70;
if (nums.includes(x)) console.log("Yay :)")
else console.log("Noo :(");

Answer №2

To handle different cases based on user input, you can utilize a switch statement like the following:

// Capture user input
var value = +prompt()

switch (value) {
  case 10:
  case 20:
  case 50:
  case 100:
    console.log(true)
    break;
  default:
    console.log(false)
    break;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Link a function to a button in a 3rd party library

Whenever I click a button, there is a possibility of an alertify alert window appearing randomly. The alertify alert popup serves as a more aesthetically pleasing alternative to the traditional javascript Alert. Alertify library Below is a snapshot depic ...

The issue arises when a variable inside a mat dialog text is not defined properly within an Angular application

I have utilized angular to create a dialog for gathering information and saving it on my back-end. The issue arises when attempting to send the data to the back-end using the post method, as the variable "val" remains undefined. The specific variable in q ...

Export problem in TypeScript

I'm having trouble with exporting in the prisma TypeScript file while executing it within a Node.js GraphQL project. Here is the error I am encountering: 05-12-2018 18:20:16: SyntaxError: /home/user/Publish/PracticeBusiness/src/generated/prisma.ts: ...

Delete the last row, column, or any other targeted dimension of a randomly assigned N-dimensional array

I'm leveraging numpy's meshgrid to generate a series of coordinate grids that are subsections of a larger meshgrid. The challenge lies in the fact that the dimensions of the meshgrid can vary. For example, for a 2D meshgrid it would look like X[0 ...

Looking for a way to extract information from two nested objects within an array by utilizing lodash, and then transferring it as a property to a react component

My react components require dynamic preloading of values from nested arrays within an array of data. import React, { useEffect } from "react"; export function loadComponent({ config, LayoutRenderer }) { const loadModules = (name) => q ...

Discovering values within an array of objects through the use of JavaScript

Consider the following: let myArray = [ { id: "3283267", innerArray: ["434","6565","343","3665"] }, { id: "9747439", innerArray: ["3434","38493","4308403840","34343"] }, { id: "0849374", innerArray: ["343434","57575","38 ...

Verify whether a loss of focus event was triggered by a tab switch

Is there a way to detect when an input loses focus due to a tab switch or window losing focus? Scenario: I need to reset a form on blur, but I want the data to persist if a user switches tabs or the window loses focus. I know I could simply check for a c ...

What steps should I follow to integrate ng-file-upload with Node.js?

Currently, I am working on a project that involves Angular, Node, Express, Multer, and ng-file-upload. Unfortunately, I have encountered a 400 (bad request) HTTP error while testing my code. Despite trying various solutions, the issue persists. Below is a ...

Monitoring Javascript inactivity timeouts

As I work to incorporate an inactivity timeout monitor on my page, the goal is to track whether individuals working from home are present at their desks or not. The Jquery plugin http://plugins.jquery.com/project/timeout that I am utilizing initiates a ti ...

Accessing a structure object from an unindexed array

In my current implementation, I have a list of structure objects that are initialized as shown below: struct1 SettingList[] = { {"as","bs","cs"} , {"ak","bk",ck"} } This particular structure, struct1, is defined with three string variables like so: stru ...

How can Angular 2 and Firebase be used to search for an email match within all Firebase authentication accounts?

Is there a method to verify the existence of an email within Firebase auth for all registered accounts when users sign up? Can AngularFireAuth be utilized in an Angular 2 service for this purpose? My authentication system is established, but now I am work ...

Json error message displayed

I am currently learning javascript and working on creating a JSON code for a website. However, I am facing some error messages related to style, even though I have used style in the code. The specific error messages are as follows: 1: Bad template.json - E ...

node.js issue with chalk package

**When I want to use the chalk package in node.js, I encounter the following issue:** index.js const chalk = require('chalk'); console.log(chalk.bgRed.inverse("hello world")); console.log(chalk.blue.inverse('Hello') + &ap ...

`problem encountered when attempting to sanitize HTML through the npm package known as "sanitize-html"`

After researching the documentation, I attempted to use this code snippet: const dirty = '<div>Content</div>'; const clean = sanitizeHtml(dirty); The desired result of 'clean' should be "Content", however it seems that &apo ...

Automatically fill in a text box with previously saved information

Does anyone have suggestions on how to create this type of textbox or what it is called? (View original image here) ...

Monitoring the height of an element within an Angular directive

I am attempting to achieve something similar to the following: myindex.html <div ng-controller="MyController" menu> <input ng-model="myVar"/> <div slimscroll="{'height':menuheight,'alwaysVisible':true}"> ...

"Hey, getting an error stating 'require is not defined' while attempting to configure the .env file. Need some help here

I am currently working on setting up a .env file to securely store the credentials for my Firebase database within a vanilla JavaScript project. Despite following various tutorials and referencing the documentation for dotenv, I continue to encounter an er ...

Creating a sorting function in VueJS requires a few key steps

I'm working on implementing a sorting function in my VueJS code that includes the following options: Price: Low to High, Price: High to Low. Here is my template: <div name="divSortBy"> <span> Sort by: & ...

Is converting the inputs into a list not effectively capturing the checkbox values?

On my website, I have a div that contains multiple questions, each with two input fields. When a button is clicked, it triggers a JavaScript function to add the input values to a list. This list is then intended to be passed to a Django view for further pr ...

Using jQuery to determine the value and dynamically stay on the current page

Is there a way to ensure that the page stays the same if the login (data.login != 1) is incorrect? I attempted using preventDefault, but it doesn't seem to be effective. Can someone offer assistance? $(document).on('submit', '#login&ap ...