Day 22A of Scrimba JavaScriptmas — Sum of Two

From my PoV (Point of View), this challenge is not fit for beginner, this is for intermediate and advanced, but yeah, once beginner do it. His/her level will grow up.

So this challenge is to make sure that inside two array if we added simultaneously will give value equal to 42

const nums1 = [1, 2, 3];
const nums2 = [10, 20, 30, 40];
const value = 42;

This is very new for me, so i can’t solve it all by myself. So after do several research i found two nice article that really help for this. The first one is a medium article and the other is a youtube of web dev simplified. Both of them teach about add two sum algorithm.

I will explain my code in simply way, i hope you will understand. This is my answer

function sumOfTwo(nums1, nums2, value) {
// write code here.
const values = nums1.concat(nums2);
const lastValue = value;

let hashTable = {};
values.forEach((value, index) => (hashTable[value] = index));

let twoSum = function(nums, target){
numsIndexes = {};

for(let i=0; i<nums.length; i++){
let currentDiffernce = target - nums[i];

if(numsIndexes[currentDiffernce] !== undefined && numsIndexes[currentDiffernce !== 1]){
return false
}else{
return true
}
}
}

return twoSum(values, lastValue)
}

This question and add two sum algorithm has same similarities. I will use ATS for add two sum until the rest of this article. So in this ATS, the question is to make sure that inside the one array if being sum simultaneously by iterating inside it, will give the value of two index same with the target.

Question

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

The question for this challenge, is to make sure that array of nums1 + nums2 == value. So the first thing i do, is to concatenate nums1 and nums2 into one array named nums. After that, using forEach to looping inside two array i push the value of it into an empty object named hashTable.

From web dev simplified i understand that this question can be finished by using this algorithm

Using two times of iterating inside array. But he says that one is bad idea. It takes many memory to process it. So he tell the algorithm to simplified the calculation. His answer his almost same with my solution, but not all, i follow one of medium tutorial. I’m sorry forget the source link. So in his medium, he build a code, that very efficient in memory usage. And his answer for solving ATS algorithm is 99%++ different from another developer.

The first thing he build a arrow function that use two parameter nums and target. After that, he declared an empty object named numsIndexes. Then, using for statement to iterate inside the array, he make a new variable named currentDifference that run inside the for statement to subtract target and nums[i]. After that using if statements, to check the indexes from currentDifference is not equal to undefined and not equal to 1. So the function will return an array that has the value same with target if using addition on them.

By using the algorithm, i just change the output into return true and false. And yeah the challenge is done. Here is the link of my solution.

Thanks

--

--

--

A Stoic Developer || Junior Data Engineer || Dreamer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How I wanted to improve Laravel but only made it worse

How we achieved green builds in an enterprise Ember.js app

Freeing JavaScript from JavaScript.

Outer Space Star Wars Bedroom Ideas

Outer Space Star Wars Bedroom Ideas featured by top Orange County lifestyle blog, Dress Me Blonde

Create an Avalanche dApp with Ethers, Metamask and React

How to make Mongo schema from pure Typescript classes ?

#CodePenChallenge 1st week April 2022: Little Details

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Handhika Yanuar Pratama

Handhika Yanuar Pratama

A Stoic Developer || Junior Data Engineer || Dreamer

More from Medium

Coding Data Structures: Stacks

Stacks are like a stack of plates.

OBJECT ORIENTED PROGRAMMING

Object Oriented Concepts

Why Data Structures and Algorithms?