# Understanding code

**URL:** <https://forum.freecodecamp.org/t/understanding-code/489008>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [December 16, 2021, 7:24pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008 "2021-12-16T19:24:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 7:24pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/1 "2021-12-16T19:24:38Z")

</div>

Learning JS. Given a solution for twoSum in leetcode as follows:

```javascript
var twoSum = function(nums, target) {
    let myObj = {}
    for (let i = 0; i < nums.length; i++) {
      let first = nums[i]
      let second = target - first
        if (myObj[second] !== undefined) {
            return [myObj[second], i]
        } else myObj[first] = i
     }
    }

```

I would like to understand what it means by myObj[second] and return [myObj[second], i]. thanks

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [December 16, 2021, 7:40pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/2 "2021-12-16T19:40:39Z")

</div>

For future reference, it will help if you provide a link to the source so we know what the function is supposed to do and the inputs that will be passed into it.

[leetcode - Two Sum](https://leetcode.com/problems/two-sum/)

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [December 16, 2021, 7:45pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/3 "2021-12-16T19:45:49Z")

</div>

> [@reydelcompas](#):
>
> I would like to understand what it means by myObj[second]

`myObj` is an object and `myObj[second]` is attempting to access the property name stored in the variable `second` on the `myObj` object.

> [@reydelcompas](#):
>
> return [myObj[second], i]

This returns a two element array, the first element is the value stored at `myObj[second]` and the second element is the current value of `i`.

---

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 8:02pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/4 "2021-12-16T20:02:18Z")

</div>

hmmm. what is the property name stored in variable second? target - first returns a number…right?

link to source: [Loading...](https://leetcode.com/problems/two-sum/)

my inputs are, for example: console.log(twoSum([9,4,8,1], 12));  
where the return should be: [1,2]

thanks.

---

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 8:10pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/5 "2021-12-16T20:10:47Z")

</div>

Hi,

Link: [Loading...](https://leetcode.com/problems/two-sum/)

Inputs: console.log(twoSum([9,4,8,1], 12));

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [December 16, 2021, 8:13pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/6 "2021-12-16T20:13:16Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 8:13pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/7 "2021-12-16T20:13:40Z")

</div>

what is the property name stored in the variable “second”? “target - first” is equal to a number?

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [December 16, 2021, 8:19pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/8 "2021-12-16T20:19:34Z")

</div>

> [@reydelcompas](#):
>
> what is the property name stored in the variable “second”?

```auto
let first = nums[i]
let second = target - first

```

We know that `nums` is an array of numbers, so `nums[i]` will be a number, which means that `first` is a number. We also know that `target` is a number, so `target - first` will be a number.

---

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 8:48pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/9 "2021-12-16T20:48:18Z")

</div>

got it. thanks.

quick question: could I have written myObj.second (to access properties of the object) instead of myObj[second]. One is dot notation and the other is with brackets.

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [December 16, 2021, 8:50pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/10 "2021-12-16T20:50:26Z")

</div>

> [@reydelcompas](#):
>
> could I have written myObj.second (to access properties of the object)

No, not in this case, because `second` is a variable that holds the name of the property you want to access. Whenever you use a variable to access a property on an object you must use brackets. Now if the actual property name on the object was “second” then you could use dot notation.

---

<div class="post-metadata">

**Author:** ![reydelcompas](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@reydelcompas](https://forum.freecodecamp.org/u/reydelcompas)\
**Post date:** [December 16, 2021, 9:14pm UTC](https://forum.freecodecamp.org/t/understanding-code/489008/11 "2021-12-16T21:14:33Z")

</div>

Thanks for the info. Will do so in the future.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 17, 2022, 9:15am UTC](https://forum.freecodecamp.org/t/understanding-code/489008/12 "2022-06-17T09:15:33Z")

</div>

Closed on June 17, 2022, 9:15am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
