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

I want to return undefined but instead got "{"

In this code I want to return the alphabet that’s missing in a string of continuous alphabets, and if all alphabets are present I want to return undefined.
However, instead returning defined, it returns "{" and I can’t find similar cases anywhere online.

function fearNotLetter(str) {

  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) !== str.charCodeAt(i + 1) - 1) {
      return String.fromCharCode(str.charCodeAt(i) + 1)
    }
  }
  return undefined
}

console.log(fearNotLetter("abcdefghijklmnopqrstuvwxyz"))

>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

here in your example inside for loop you are returning

String.fromCharCode(str.charCodeAt(i)+1)

in this return value when charecter is z , where has ascii value of ‘z’ is 122 and you are returning 122+1 that is asci value of ‘{‘

thats why you are getting ‘{‘ in your example

you can test by removing ‘z’ from passed string in your function

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