Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript / for-loop even and odd letters from text

I want to select even letters and odd letters from text, console.log show mi
(199) [‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘…. How can I fix it ?


btn.addEventListener('click', function() {
  //console.log(newText)  ok

  let evenletters = []

  for (let i = 0; i < newText.length; i++) {
    if(newText[i] % 2 === 0) {
      evenletters.push(newText[i])
    }
  }
  console.log(evenletters)
})


>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m guessing you are coming from C or another language that can treat a char as an int. What you want to do in JS is use .charCodeAt

let evenletters = []
let newText = 'Hello there how are you?'
  for (let i = 0; i < newText.length; i++) {
    if(newText.charCodeAt(i) % 2 === 0) {
      evenletters.push(newText[i])
    }
  }
  console.log(evenletters)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading