forum Posted March 16 Share Posted March 16 ``` const str = 'HELLO'; let result = ''; let i = 0; while (i < str.length) { const current = str; // console.log(current) // console.log(i) ++i; result = `${result}${current.toLowerCase()}` i++; } console.log(result) ``` why does hlo display? Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/ Share on other sites More sharing options...
Barand Posted March 16 Share Posted March 16 Because you increment "i" twice inside the loop. Remove ++i. When I run the script it gives "hellohellohello" try <script type='text/javascript'> const str = 'HELLO'; let i = 0 let result = "" while (i < str.length) { result = result + str[i].toLowerCase() console.log(result) i++ } </script> Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617953 Share on other sites More sharing options...
forum Posted March 16 Author Share Posted March 16 I still don’t understand why the cycle goes through letters and not numbers? Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617954 Share on other sites More sharing options...
Barand Posted March 16 Share Posted March 16 It will if you declare the number as a string eg const str = '98765'; gives Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617955 Share on other sites More sharing options...
forum Posted March 16 Author Share Posted March 16 yes, but here the line is const str = 'HELLO'; And in the loop there is also a comparison of numbers, then where do the letters come from? str.length I see the number 5 in the console, why do the letters appear? Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617956 Share on other sites More sharing options...
Barand Posted March 16 Share Posted March 16 A string can be treated as an array of characters, so str = 'HELLO' console.log(str[0]) //==> H console.log(str[2]) //==> L console.log(str[4]) //==> O Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617957 Share on other sites More sharing options...
forum Posted March 16 Author Share Posted March 16 Damn, how difficult it is to understand all this)) Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617958 Share on other sites More sharing options...
gizmola Posted March 16 Share Posted March 16 2 hours ago, forum said: Damn, how difficult it is to understand all this)) In the future a couple of things that will help. use the code tags feature of the forum, as Barand did. It's easier for you and for us to read your code. What was missing from your code was an explanation of what the code was "supposed to do", and thus what did not work the way you expected it to. The whole of computer science is not understood in a short period of time. Oftentimes people start learning a computer language, and once they are part way into the syntax, they find themselves lost and confused, because they don't have the fundamental understanding of how computers work. In this case the fundamental idea is "What is a string?" Most computer languages have strings, and in general, they share at least one similarity, and that is that they are a series of characters in a contiguous area of memory. The underlying values in computer memory are always numbers, so you need some sort of scheme (typically called a charset) that determines how a value in a charset table maps to an actual character. Javascript actually uses the UTF-16 charset, which will use either 2 bytes or 4 bytes to represent a particular character, but you don't have to really think of it in that way. Rather you can thing of a string as a sequence of characters in memory (or an array). Your code appears to be an exploration of this. Of course in javascript strings are objects, which is not the case in many other languages. Javascript as a language is also odd and quirky, and was purpose built originally to run inside a browser, so it's hard to separate it from html and css, and http and internet networking, all things that can have a depth to them that in combination is confusing. I often advise people to get good with a note taking app like notion or evernote or onenote and to make sure you note when there's a topic you don't understand. Then go through these in your spare time and research and study them. There are good resources and poor ones, so you may have to dig into a particular topic using a few different resources until you get one that sticks. For example, here is a very detailed blog post explaining what UTF-16 is and how it works: https://dmitripavlutin.com/what-every-javascript-developer-should-know-about-unicode/ or this more general introduction to character sets with some javascript specific code examples: https://www.honeybadger.io/blog/encode-javascript/#:~:text=js%2C text data is typically,working with other character encodings. Quote Link to comment https://forums.phpfreaks.com/topic/319142-why-does-the-code-output-three-letters/#findComment-1617973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.