Build a Markdown to HTML Converter - Build a Markdown to HTML Converter

Tell us what’s happening:

My test is not passing even though I am getting the desired result.
Please guide me if there is a problem in my regex or anything else.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown to HTML Converter</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <h1>Markdown to HTML Converter</h1>
    <div id="container">
        <div class="container">
            <h2>Markdown Input:</h2>
            <textarea id="markdown-input" placeholder="Enter your markdown here..."></textarea>
        </div>
        <div class="container">
            <h2>Raw HTML Output:</h2>
            <div id="html-output"></div>
        </div>
        <div class="container">
            <h2>HTML Preview:</h2>
            <div id="preview"></div>
        </div>
    </div>
    <script src="script.js"></script>
</body>

</html>
/* file: styles.css */
* {
     box-sizing: border-box;
}
 body {
     
     font-family: Arial, sans-serif;
     padding: 20px;
}
 #markdown-input {
     width: 100%;
     height: 100px;
}
 #html-output, #preview {
     height: 100px;
     display: inline-block;
     width: 100%;
     border: 1px solid #ccc;
     padding: 10px;
     margin: auto;
     white-space: pre-wrap;
     background-color: #f9f9f9;
}
 @media (min-width: 600px) {
     #markdown-input, #html-output, #preview {
         height: 200px;
         margin: 0;
    }
     #container {
         display: flex;
         justify-content: space-evenly;
         gap: 10px;
    }
}
/* file: script.js */
const userInput = document.getElementById("markdown-input");
const html = document.getElementById("html-output");
const preview = document.getElementById("preview");

const convertMarkdown = () => {
  let input= userInput.value;
  let regexh1 = /#\s+(\w+)(\s\d)/gi;
  let replacement = input.replace(regexh1, "$1 $2");
  html.innerText = `<h1>${replacement}</h1>`;
}

userInput.addEventListener("input", convertMarkdown);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Markdown to HTML Converter - Build a Markdown to HTML Converter

what tests are you failing? what have you tried to debug that?

The tests won’t pass until you handle all of the HTML elements the instructions are asking you to convert. You’ve only attempted to convert the h1 element.

yes I am doing it, this regex feels very tough to me :frowning:

I have done all tasks required till test 5, but test 2 and 4 not passing

updated code-

const userInput = document.getElementById("markdown-input");
const html = document.getElementById("html-output");
const preview = document.getElementById("preview");

const convertMarkdown = () => {
  let input= userInput.value;
  let regexh1 = /^#\s+(\w+)\s+(\d)/gi;
  let replacement = input.replace(regexh1, "$1 $2");
  let test = regexh1.test(input);
  if(test) {
    html.innerText = `<h1>${replacement}</h1>`;
  preview.innerText = `<h1>${replacement}</h1>`;
  }
  else {
    html.innerText = input;
    preview.innerText = input;
  }
}

userInput.addEventListener("input", convertMarkdown);

I did what it is said in test 2 but it is not passing and in test 4, it is said- When the value of #markdown-input is # title 1, an h1 element with the text of title 1 should be appended as a child of #preview.

How to append this like a child, in previous project I learnt I can append any element like a child inside a parent element like a div inside a div, but I don’t know what to do here ?

Just work on one problem at a time. Trying to explain 2 tests feedback in one sentence is too much.

Focus on one at a time.

What is the feedback on test 2? Errors, hints?

Can you show how your code fulfills the requirement of test 2?

I am very sorry :frowning:
I am feeling very confused and overwhelmed, I was away for few weeks due to my exams and I forgot many things. I will be back to this lesson after some time.

I want to revise previous topics once.

Ok, but remember to just focus on one problem at a time. This can help when you are feeling overwhelmed.

It’s easier to focus your energy and fix one problem.

Worry about the next problem only after the first one is done.

1 Like