Skip to content

Instantly share code, notes, and snippets.

@acidtone
Last active November 1, 2021 13:29
Show Gist options
  • Save acidtone/30a77c1eae8575c0d98c691384e4c9c0 to your computer and use it in GitHub Desktop.
Save acidtone/30a77c1eae8575c0d98c691384e4c9c0 to your computer and use it in GitHub Desktop.
Spoilers: Test if an item is in an array

Spoilers: Using Array.includes() to test for an array item

This is a potential answer to the exercise: Test for an array item.

Stretch Activity

  • Try validating the user input before testing:
    • inputValue value missing: "Please enter a Character Class";
// Accept command line input
const inputValue = process.argv[2];
// Define data array
const characterClasses = [
'fighter',
'wizard',
'rogue',
'barbarian',
'bard'
]
// Test if the entered string matches a value in characterClasses
if (characterClasses.includes(inputValue)) {
console.log(`"${inputValue}" is a valid Character Class!`);
} else {
console.log(`"${inputValue}" is NOT a valid Character Class! Please enter another Character Class.`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment