Comparing result of .match()

Hello there, can somebody explain to me why do we write consonants != null?
I tried: consonants != [""] but it doesn’t work. Why is it just null and not an empty string or something.

Below I added a “SELF ATTEMPT” code which I tried before getting hints. I tried converting the result of .match() to a string to get the length for .slice(). Is this wrong. I’m just curious if it is possible to do it this way. Would be great if somebody can give advice on improvement/advice on what to be avoided. Thanks in advance! Really enjoying this community ^^

  **Your code so far**

function translatePigLatin(str) {
var regexVowel = /^[^aeiou]+/;
let consonants = str.match(regexVowel);
return consonants !== null
  ? str
    .replace(consonants, "")
    .concat(consonants)
    .concat("ay")
  : str
    .concat("way");

}

translatePigLatin("eight");

SELF ATTEMPT

  var regexVowel = /^[^aeiou]+/;
  var front = str.match(regexVowel);
  front = front.toString();
  console.log("toStr: " + front);
  if (front === str && front !== "") {
    return str.concat("ay");
  } else if (front !== str && front !== "") {
    let length = front.length;
    return str.slice(length) + front + "ay";
  } else {
    //str + "way";
  }
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15

Challenge: Pig Latin

Link to the challenge:

function translatePigLatin(str) {

//matches everything before the first vowel
var regexVowel = /^[^aeiou]+/;

//sets consonants to the return value of match
//if no match is made the return value is null
let consonants = str.match(regexVowel);

//a ternary in which the condition is based on if consonants is null
return consonants !== null

//this is what is returned if consonants is not null
  ? str
//if consonants is not null then it is an array with only 
//one property on the array list itself 
//the rest are on the objects property collection which are 
//interacted with like a normal object property

//since match only ever returns an array of length 1 this works
//but this would not work with a multi-length array
    .replace(consonants, "")
    .concat(consonants)
    .concat("ay")
  
//this is what is returned if consonants is null
: str
    .concat("way");

}

translatePigLatin("eight");
1 Like

I would suggest reading the MDN docs on match and see if that doesn’t answer your questions (like that it returns).

Thank you! I find the answer to my question after reading the article!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.