Jump to content

Why does the code output three letters?


forum

Recommended Posts

```
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?

Link to comment
Share on other sites

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>  

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.