-3

I'm trying to use regex to evaluate whether the newArray at index 0 is equivalent to the value stored in vowel. I know that this method doesn't work but I don't understand why. BTW I've just started learning how to code so I only really know Vanilla JS

function translate(val) {
    let newArray = Array.from(val)
    let vowel = /^[aeiouy]/gi
    let consonant = /[^aeiouy]/
    if (newArray[0] == vowel) {
        return 'vowel';
    } else if (newArray[0] == consonant) {
        return 'consonant'
    } {
        return 'none';
    }
}
 translate('inglewood')
3
  • Is Array.from(var) supposed to be Array.from(val)? Commented Nov 9, 2020 at 2:35
  • Yes! just fixed that @ThumChoonTat Commented Nov 9, 2020 at 2:37
  • newArray[0] == consonant that's not how regex's are used ... use String .match or RegExp .test Commented Nov 9, 2020 at 2:40

2 Answers 2

1

You should be using the regex test method here:

function translate(val) {
    let vowel = /^[aeiouy]/gi;
    let consonant = /^[^aeiouy]/;
    if (vowel.test(val)) {
        return 'vowel';
    } else if (consonant.test(val)) {
        return 'consonant'
    } else {
        return 'none';
    }
}

console.log(translate('inglewood'));

Note: I don't see any point in using Array.from() here. Instead, we can just run test directly against an input string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This will actually make the next steps so much easier for me code!
0

You need to .test() the string with the regex

function translate(val) {
    let newArray = Array.from(val)
    let vowel = /^[aeiouy]/gi
    let consonant = /[^aeiouy]/

    if ( vowel.test( newArray[0] ) ) {
        return 'vowel';
    } else if ( consonant.test( newArray[0] ) ) {
        return 'consonant'
    } {
        return 'none';
    }
}
console.log( translate('inglewood') );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.