Skip to content

Commit ceceaee

Browse files
authored
Add solution in JS
1 parent 7641311 commit ceceaee

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ Because `nums[0] + nums[1] == 9`, we return `[0, 1]`.
2424

2525
**Only one valid answer exists.**
2626

27+
## Solution in JavaScript:
28+
29+
```
30+
var twoSum = function(nums, target) {
31+
let targetFirstIndex, targetLastIndex;
32+
33+
for (let i = 0; i < nums.length; i++) {
34+
for (let j = i + 1; j < nums.length; j++) {
35+
if (nums[i] + nums[j] === target) {
36+
targetFirstIndex = i;
37+
targetLastIndex = j;
38+
break;
39+
}
40+
}
41+
}
42+
43+
return [targetFirstIndex, targetLastIndex];
44+
};
45+
```

0 commit comments

Comments
 (0)