-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeeptheOrder.js
More file actions
23 lines (18 loc) · 820 Bytes
/
Copy pathKeeptheOrder.js
File metadata and controls
23 lines (18 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Task:
// Your job here is to write a function (keepOrder in JS/CoffeeScript, keep_order in Ruby/Crystal/Python, keeporder in Julia), which takes a sorted array ary and a value val, and returns the lowest index where you could insert val to maintain the sorted-ness of the array. The input array will always be sorted in ascending order. It may contain duplicates.
// Do not modify the input.
// Some examples:
// keepOrder([1, 2, 3, 4, 7], 5) //=> 4
// ^(index 4)
// keepOrder([1, 2, 3, 4, 7], 0) //=> 0
// ^(index 0)
// keepOrder([1, 1, 2, 2, 2], 2) //=> 2
// ^(index 2)
function keepOrder(ary, val) {
ary.push(val)
return ary.sort((a,b) => a - b).indexOf(val)
}
//different approach
function keepOrder(ary, val) {
return ary.filter(a => a < val).length;
}