Title: Sum of two numbers

Given an array of integers nums and a target value target, find the two integers in the array and the target values and return their array subscripts.

You can assume that there is only one answer for each type of input. However, you cannot reuse the same elements in this array.

Example:

Given nums = [2, 7, 11, 15], target = 9Copy the code

Using the language JavaScript:

/ * * *@param {number[]} nums
 * @param {number} target
 * @return {number[]}* /
var nums = [2.7.11.15], target = 9;
var twoSum = function(nums, target) {
    var er=[];
    for(var i=0; i<nums.length; i++){for(var j=i+1; j<nums.length; j++){if(nums[i]+nums[j]==target)
            {
                er[0]=i;
                er[1]=j; }}}return er;  
};

console.log(twoSum(nums,target));
Copy the code

Analysis:

JavaScript gives a function method twoSum. Given two variables nums, target. Nums is an array variable and target is a numeric variable. Define an array er (note: defining an array in JavaScript is var array name =[]). Nested with a for loop that adds a value in the NUMS array to its subsequent value to see if it is equal to target. If they are equal, pass the values of I and j to the er array. Finally, return the ER array