0

My question is this ...

I get a value through a parameter, but I want to check if that value is really what I need. Example:

rightWord = 'AC' || 'AL' || 'AP' || 'AM' || 'BA' || 'CE' || 'ES' || 'GO' || 'MA' || 'MT' || 'MS' || 'MG' || 'PA' || 'PB' || 'PR' || 'PE' || 'PI' || 'RJ' || 'RN' || 'RS' || 'RO' || 'RR' || 'SC' || 'SP' || 'SE' || 'TO' || 'DF'
function testing (test){
    if(rightWord === test){
        console.log('ok')
    }else{
        console.log('not')
    }
}

testing('TEST')

How can I do, to check if the parameter I pass through testing () is the same as some words I want to validate?

Obs: Sorry, I'm studying but I can't solve this problem.

  • `rightWord = 'AC' || ... ` is the same as `rightWord = true`. Nonempty strings are always truthy. – ggorlen Oct 14 '20 at 19:44

2 Answers2

5

Make rightWord an array, and then just call the .includes method.

const rightWord = ['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO', 'DF'];

function testing (test){
    if(rightWord.includes(test)){
        console.log('ok')
    }else{
        console.log('not')
    }
}

Another option would be to create a set, and then use the .has method (thanks to @ggorlen in the comments):

const rightWord = new Set(['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO', 'DF']);

function testing (test){
    if(rightWord.has(test)){
        console.log('ok')
    }else{
        console.log('not')
    }
}

More information on Set is available here

Donut
  • 110,061
  • 20
  • 134
  • 146
0
let rightWord = ['AC', 'AL', 'AP', ... ];

if (rightWord.includes(test)) {...}
Bulent
  • 3,307
  • 1
  • 14
  • 22