Back to Freecodecamp

Challenge 163: Consonant Case

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/694596b0585c11170ac7c7fb.md

latest1.5 KB
Original Source

--description--

Given a string representing a variable name, convert it to consonant case using the following rules:

  • All consonants should be converted to uppercase.
  • All vowels (a, e, i, o, u in any case) should be converted to lowercase.
  • All hyphens (-) should be converted to underscores (_).

--hints--

toConsonantCase("helloworld") should return "HeLLoWoRLD".

js
assert.equal(toConsonantCase("helloworld"), "HeLLoWoRLD");

toConsonantCase("HELLOWORLD") should return "HeLLoWoRLD".

js
assert.equal(toConsonantCase("HELLOWORLD"), "HeLLoWoRLD");

toConsonantCase("_hElLO-WOrlD-") should return "_HeLLo_WoRLD_".

js
assert.equal(toConsonantCase("_hElLO-WOrlD-"), "_HeLLo_WoRLD_");

toConsonantCase("_~-generic_~-variable_~-name_~-here-~_") should return "_~_GeNeRiC_~_VaRiaBLe_~_NaMe_~_HeRe_~_".

js
assert.equal(toConsonantCase("_~-generic_~-variable_~-name_~-here-~_"), "_~_GeNeRiC_~_VaRiaBLe_~_NaMe_~_HeRe_~_");

--seed--

--seed-contents--

js
function toConsonantCase(str) {

  return str;
}

--solutions--

js
function toConsonantCase(str) {
  const vowels = "aeiouAEIOU";
  let result = "";

  for (let char of str) {
    if (char === "-") {
      result += "_";
    } else if (/[a-zA-Z]/.test(char)) {
      if (vowels.includes(char)) {
        result += char.toLowerCase();
      } else {
        result += char.toUpperCase();
      }
    } else {
      result += char;
    }
  }

  return result;
}