10000 [ADD] Get index added · luisprooc/js-algorithms@2584972 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2584972

Browse files
committed
[ADD] Get index added
1 parent 44545d3 commit 2584972

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,47 @@ confirmEnding("If you want to save our world, you must hurry. We dont know how m
303303
confirmEnding("Abstraction", "action") should return true.
304304

305305
Your code should not use the built-in method .endsWith() to solve the challenge.
306+
```
307+
308+
309+
## Get Index
310+
311+
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
312+
313+
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
314+
315+
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
316+
317+
```javascript
318+
getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.
319+
320+
getIndexToIns([10, 20, 30, 40, 50], 35) should return a number.
321+
322+
getIndexToIns([10, 20, 30, 40, 50], 30) should return 2.
323+
324+
getIndexToIns([10, 20, 30, 40, 50], 30) should return a number.
325+
326+
getIndexToIns([40, 60], 50) should return 1.
327+
328+
getIndexToIns([40, 60], 50) should return a number.
329+
330+
getIndexToIns([3, 10, 5], 3) should return 0.
331+
332+
getIndexToIns([3, 10, 5], 3) should return a number.
333+
334+
getIndexToIns([5, 3, 20, 3], 5) should return 2.
335+
336+
getIndexToIns([5, 3, 20, 3], 5) should return a number.
337+
338+
getIndexToIns([2, 20, 10], 19) should return 2.
339+
340+
getIndexToIns([2, 20, 10], 19) should return a number.
341+
342+
getIndexToIns([2, 5, 10], 15) should return 3.
343+
344+
getIndexToIns([2, 5, 10], 15) should return a number.
345+
346+
getIndexToIns([], 1) should return 0.
347+
348+
getIndexToIns([], 1) should return a number.
306349
```

src/getIndex.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function getIndexToIns(arr, num) {
2+
arr.sort((a , b) => a-b);
3+
4+
for(let i = 0; i < arr.length; ++i){
5+
if(arr[i] >= num){
6+
return i;
7+
}
8+
}
9+
return arr.length;
10+
}
11+
12+
console.log(getIndexToIns([2, 60], 50));

0 commit comments

Comments
 (0)
0