aidi
aidi

Reputation: 11

How do i check if input is a number (any number) in an array

Im making a bot that prevents users from talking about age, but i have the problem, that i don't know how to solve. If a user inputs "i am (number)" it should reply with " Don't talk about age here!". This is my code (the part i need help with).

var age = ['how old', 'how old am i', 'how old are you', `i am ${!NaN}`];

if (age.includes(message.content)) {
    message.reply('Don't talk about age here!')
}

i am ${!Nan} should be any number but it doesn't work.

Upvotes: 1

Views: 58

Answers (2)

Ben Aston
Ben Aston

Reputation: 55769

You can use a regular expression to look for digit (number) characters in the input string.

const containsNumber = (str) => /\d/.test(str)

const result1 = containsNumber('I am 25 years old.')
console.log(result1) // true

const result2 = containsNumber('My age is a secret.')
console.log(result2) // false

And the template literal syntax is as follows:

const myAge = 39
const myString = `I am ${myAge} years old.`
console.log(myString)

Upvotes: 1

codeanjero
codeanjero

Reputation: 694

For the regex you might wanna try something like so :

var age = ['how old', 'how old am i', 'how old are you', 'i am 10'];

if (age[3].match(/(\d+)/)!=null) {
    console.log("Don't talk about age here!")
}

Upvotes: 1

Related Questions