Къде ми е грешката в задача 2 Pascal or camel case в DOM introduction - exercise

// 1. Select elements

let textElement = document.querySelector('#text');

let nameElement = document.querySelector('#naming-convention');

 

// 2. Parse Data

let text = textElement.value;

let nameConvention = nameElement.value;

// 3. Main logic

let result = applyNamingConvention(text, nameConvention);

// 4. Display result in DOM

let spanElement = document.getElementById('result');

spanElement.textContent = result;

Това е моето решение:

function applyNamingConvention(text, convention) {

  const conventionSwitch = {

    'Pascal Case': () => text

    .toLowerCase().split(' ')

    .map(x => x[0].toUpperCase() + x.slice(1))

    .join(''),

    'Camel Case': () => text

    .toLowerCase().split(' ')

    .map((x, i)=> x = i !== 0 ? x[0].toUpperCase() + x.slice(1) : x)

    .join(''),

    default: () => 'Error!'

  };

  return (conventionSwitch[convention] || conventionSwitch.default)();

}

 

Ето и условието на задачата:

2.Pascal or Camel Case

An HTML file is given and your task is to write a function that takes two string parameters as an input and transforms the first parameter to the type required by the second parameter.

  • The first parameter will be the text that you need to modify depending on the second parameter. The words in it will always be separated by space.
  • The second parameter will be either "Camel Case" or "Pascal Case". In case of different input, your output should be "Error!"

When the button is clicked the function should convert the first string to either of the cases. The output should consist of only one word - the string you have modified. Once your output is done, you should set it as HTML to the <span> element. For more information, see the examples below:

Example

Input

Output

"this is an example", "Camel Case"

thisIsAnExample

"secOND eXamPLE", "Pascal Case"

SecondExample

"Invalid Input", "Another Case"

Error!

 

Hints

First, take the two values from the input fields:

 

Then, write a function that generates the result:

  • First, convert all the letters to lowercase
  • Depending on the command, make the input either Pascal Case or Camel Case