-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniqueInOrder.js
24 lines (20 loc) · 929 Bytes
/
UniqueInOrder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var uniqueInOrder=function(iterable){
//Convert incoming data into string to ensure either a string or an array can be input into the function
let stringifiedIterable = String(iterable)
//.replace with the below regex eliminates consecutive duplicates
.replace(/,/g, '').replace(/(.)\1+/g, '$1')
//.split returns an array of each character in the incoming string
.split('')
//.map here is taking the string array and testing it to see if its contents are integers using isNaN()
.map(function (item) {
if(!isNaN(item) && !isNaN(item - 0)) {
//if item is integer parse to return integer
return(parseInt(item))
} else {
//if item is string, return the item to the map function
return(item)
}
});
// return the newly formated array
return(stringifiedIterable)
}