Jump to content

Javascript puzzle

I've been practicing a lot of html and javascript recently, and I came across this challenge question. I can't for the life of me figure out why it works and I feel like the answer is very simple.

Here it is:

What is the output of this code:

<html>
<head>
<title>Test</title>
</head>
<body>
 <script type="text/javascript">
  var arr = [1,2,3,4];
  arr[arr[1]]=5;
  alert(arr);
 </script>
</body>
</html>

The answer is 1,2,5,4 but I can't figure out why. arr[1] is the second spot in the array, so the output should be 1,5,3,4 but its not. Does anybody know how this works? It's been bugging me for hours. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

It's because programming arrays start with 0, so arr[1] is the second value in the array.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Minibois said:

It's because programming arrays start with 0, so arr[1] is the second value in the array.

It's not changing the second number, its changing the third number. It should change the second but it doesn't I ran it myself its changing the 3 to a 5.

Link to comment
Share on other sites

Link to post
Share on other sites

so arr[1] gives back 2 -->  this value is inserted into the second arr[] so it selects the 3 position of the original array which is the 3 and you override it with 5

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, SgtBot said:

It's not changing the second number, its changing the third number. It should change the second but it doesn't I ran it myself its changing the 3 to a 5.

Yea, just re-read your post and noticed I missed a dot, which caused me to misinterpret what you said. Sorry about that!

But I see someone else correctly answered your question here.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Helibert said:

so arr[1] gives back 2 -->  this value is inserted into the second arr[] so it selects the 3 position of the original array which is the 3 and you override it with 5

Oh my god I fking knew it was simple idk why I couldn't see that.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×