0

I am learning/refreshing/playing with data. In this example, I am looking for even and odd length string in sentence. But character for new line is not working. Can you show me what am I doing wrong? THANK YOU.

const evenOddLength = (t: string) => {
     let text = strings.split(' ')
      const even: string [] = []
     const odd: string [] = []

     for(let i = 0; i < text.length; i++) {
      if(text[i].length % 2 === 0) {
         even.push(text[i])
      } else {
        odd.push(text[i])
      }
     }

    setOutput(`Even string: [ ${even} ],` + "\n" + ` Odd string: [ ${odd} ]`) 

  }

Expected output should be:

Even string: [ String,be,iterated,loop ],
Odd string: [ The,Array,can,using,the,for ]

but I am getting :

Even string: [ String,be,iterated,loop ], Odd string: [ The,Array,can,using,the,for ]
erza
  • 107
  • 8

1 Answers1

0

To do a line break in HTML, you should use the <br> tag instead of \n

let even = [];
let odd = [];
function evenOddLength(t) { 
   let text = t.split(' ');

   for(let i = 0; i < text.length; i++) {
      if(text[i].length % 2 === 0) {
        even.push(text[i]);
      } else {
        odd.push(text[i]);
      }
   } 
} 

evenOddLength('The Array can using the for String be iterated loop');
document.getElementById("useJsNewLine").innerHTML = `* Using Javascript line break (\\n): Even string: [ ${even} ],` + "\n" + ` Odd string: [ ${odd} ]`;
document.getElementById("useHtmlBrTag").innerHTML = `* Using HTML line break (br tag):<br/>Even string: [ ${even} ],` + "<br/>" + ` Odd string: [ ${odd} ]`;
<p id="useJsNewLine">Using Javascript line break (\n):</p>
<p id="useHtmlBrTag">Using HTML line break (br tag):</p>
Trung Duong
  • 3,475
  • 2
  • 8
  • 9