JSFiddle - React, Tailwind, and code Playground

JavaScript

function twoSum(nums, target) {
	let map = {}
  
  for (let i = 0; i < nums.length; i++) {
  	// Find the difference between target and curent index	
  	let a = target - nums[i];
    
    // if difference key exists in map, return it and the current index
    if (a in map) {
			return [map[a], i]
    }
    
    // otherwise store the difference as a key
    map[nums[i]] = i
  }
}

console.log(twoSum([1, 2, 7, 4], 5))