Unique In Order
Instructions: Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements. For example: uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] uniqueInOrder([1,2,2,3,3]) == [1,2,3] 1st Attempt: var uniqueInOrder = function (iterable) { const newSet = new Set(iterable.split('')); return newSet; }; This was my first attempt in solving the problem, thinking that all the elements should be unique. After re-reading the requests and seeing the examples I realize that I would not be able to use Set as I do not need all the elements to be unique. 2nd Attempt: var uniqueInOrder = function (iterable) { const arr = iterable.split(''); let newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] !== arr[i + 1]) newArr.push(arr[i]); } return newArr; }; This time the output with strings is the one desired, but the code doesn't work with arrays. So I had to modify the code into the one below: let arr = typeof iterable === String ? iterable.split("") : iterable; Thoughts: 1.I check if iterable is a String and define arr based on that: let arr = typeof iterable === String ? iterable.split("") : iterable; 2.Then I use the for loop to compare the arr at position i with the one in the next position and function of that to push it into the returned array. Solution: var uniqueInOrder = function (iterable) { let arr = typeof iterable === String ? iterable.split("") : iterable; let newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] !== arr[i + 1]) newArr.push(arr[i]); } return newArr; }; This is a CodeWars Challenge of 6kyu Rank (https://www.codewars.com/kata/54e6533c92449cc251001667/train/javascript)

Instructions:
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1,2,2,3,3]) == [1,2,3]
1st Attempt:
var uniqueInOrder = function (iterable) {
const newSet = new Set(iterable.split(''));
return newSet;
};
This was my first attempt in solving the problem, thinking that all the elements should be unique. After re-reading the requests and seeing the examples I realize that I would not be able to use Set as I do not need all the elements to be unique.
2nd Attempt:
var uniqueInOrder = function (iterable) {
const arr = iterable.split('');
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i + 1]) newArr.push(arr[i]);
}
return newArr;
};
This time the output with strings is the one desired, but the code doesn't work with arrays. So I had to modify the code into the one below:
let arr = typeof iterable === String ? iterable.split("") : iterable;
Thoughts:
1.I check if iterable is a String and define arr based on that:
let arr = typeof iterable === String ? iterable.split("") : iterable;
2.Then I use the for loop to compare the arr at position i with the one in the next position and function of that to push it into the returned array.
Solution:
var uniqueInOrder = function (iterable) {
let arr = typeof iterable === String ? iterable.split("") : iterable;
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i + 1]) newArr.push(arr[i]);
}
return newArr;
};
This is a CodeWars Challenge of 6kyu Rank (https://www.codewars.com/kata/54e6533c92449cc251001667/train/javascript)