JSFiddle - React, Tailwind, and code Playground

by tnhu

JavaScript

var a = [1, 2, 3, 7, 9];
var target = 9;

function findTwoSum(arr, target) {
  var result = [];
  
  for (var i = 0, len = arr.length; i < len; i++) {
  	var op = target - arr[i];
    var opIndex = arr.indexOf(op);
    
    if (opIndex !== -1) {
    	result = [i, opIndex];
    }
  }
  
  return result;
}

alert(findTwoSum(a, target));