DEV Community

Ashish Prajapati
Ashish Prajapati

Posted on

Day 7 of #100DaysOfCode

What I learned?

I learned the following topics:

  • use of Static keyword in javascript
  • bind keyword in javascript

What I developed/solved?

  • Solved one leetcode problem called Two sum

Code snippet/Screenshots/notes

  1. Leetcode problem Two sum
  • Problem statement: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
  • Example.
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Enter fullscreen mode Exit fullscreen mode
Input: nums = [3,2,4], target = 6
Output: [1,2]
Enter fullscreen mode Exit fullscreen mode
  • Brute force approach
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int size = nums.size();
        vector<int> ans;
        for (int i = 0; i < size; i++) {
            int flag = 0;
            for (int j = i+1; j < size; j++) {
            /* when the summation of these two becomes equal or up to target
               return the indexes of both
            */
                int sum = nums[j] + nums[i];
                if (sum == target && i != j){
                   ans.push_back(i);
                   ans.push_back(j);
                   /* flag used to determine that we have found 
                   that element whose summmation is equal to target
                    */
                   flag = 1;
                }
            }
            //flag = 1 meaning we got our answer so no need to go further
            if(flag == 1){
                break;
            }
        }
        return ans;
    }
};
// Time complexity: O(n)*O(n)=O(n^2)
// Space comlexity: O(1) as we aren't using extra space
Enter fullscreen mode Exit fullscreen mode
  • optimal approach using hashmap
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        map<int, int> m;
        vector<int> v;
        for (int i = 0; i < nums.size(); i++) {
            int first = nums[i];
            int second = target - first;

            if (m.find(second) != m.end()) {
                v.push_back(m[second]);
                v.push_back(i);
            }

            m[first] = i;
        }

        return v;
    }
};
/* Time complexity: O(N * logN), where n = number of elements in an array
logN = time takes for insertion in map
   Space complexity: O(N)
*/ 
Enter fullscreen mode Exit fullscreen mode

Static keyword in JavaScript

  • static keyword is used to define a static method for a class.
  • static methods aren't called on instances of the class. Instead, they're called on the class itself.
  • You call it on the class, but on the instances of class

Bind keyword

  • bind() method in JavaScript is a method that allows you to set the this value in a function.
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bind</title>
</head>

<body>
    <button>Click me</button>
</body>

<script>
    class React {
        constructor() {
            this.library = "React"
            this.server = "https://localhost:3000"

            document.querySelector('button')
                .addEventListener('click', this.handleClick.bind(this)) //this for current context
        }

        handleClick() {
            console.log("button clicked successfully!")
            console.log(this.server)
        }
    }

    const my_app = new React();
</script>

</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
ekwoka profile image
Eric Kwoka

Bind doesn't just allow setting the this.

It's full on partial application.

Each time it's called it returns a new function with the new bound args appended to the previously bound args.

If something takes multiple arguments, you can use bind to only provide some of them, and later call it with the remaining ones which can be useful for scoping data, or for having functions that accept infinite arguments that you want to be able to apply from different places so that the end user just calls with the ones they are adding every time.

Collapse
 
codingashish_dev profile image
Ashish Prajapati

Thank you for the information