JavaScript Regular Expressions in JavaScript Reformatting User Input Reformatting Text

Wybe van den bosch
Wybe van den bosch
3,045 Points

Stuck on this challenge? got your solution.

You are stuck on this challenge because : const newText = "$2,$1"; Does exactly what the challenge asks you todo. However it doesnt mention that you need a SPACE after the comma.... i was stuck on this for 10 minutes before i figured it out : /

So for everyone else that was stuck too, here you go : const newText = "$2, $1";

app.js
function reformatName(text) {
  const rawName = /^(\w+)\s(\w+)$/;

  // Type your answer on line 5, below:
  const newText = "$2, $1";

  return text.replace(rawName, newText);
}

const form = document.querySelector("form");
const input = form.querySelector("input");
const reformatted = document.getElementById("reformatted");

form.addEventListener("submit", e => {
  e.preventDefault();
  reformatted.textContent = reformatName(input.value);
});
index.html
<!DOCTYPE html>
<html>

<head>
    <title>DOM Manipulation</title>
</head>
<link rel="stylesheet" href="style.css" />

<body>
    <div id="content">
        <form>
            <label for="name">Enter your first and last name, please.</label>
            <br />
            <input type="text" id="name" name="name">
            <button type="submit">Reformat</button>
        </form>
        <div>
            <h2>Reformatted name:</h2>
                <p id="reformatted"></p>
        </div>
    </div>
    <script src="app.js"></script>
</body>

</html>