Ok, near as I can tell from the console.log, this approach should work just fine. Running the arr argument through.toString().split(",")
creates a string, then splits the string into an array (tested using console.log(Array.isArray(strArray))
, which returns true.
Then, cycle through the elements in strArray, checking to see if a new array, newArr, includes the value. If not, push it. Everything looks 5x5, but the test still fails. Console.logs all check out, but the test still fails.
function uniteUnique(arr) {
var strArr = arr.toString().split(",");
console.log(strArr);
var newArr = [];
for(var i = 0; i < strArr.length; i++) {
var currentValue = strArr[i];
if(newArr.includes(currentValue)) {
} else {
newArr.push(currentValue);
}
}
console.log(arr); // returns 1,3,2,5,2,1,4,2,1
console.log(newArr); // returns 1,3,2,5,4 <-- the correct answer!
console.log(Array.isArray(strArr)); // returns TRUE
console.log(Array.isArray(newArr)); // returns TRUE
return newArr;
}
uniteUnique([[1, 3, 2], [5, 2, 1, 4], [2, 1]]);
Console:
1,3,2,5,2,1,4,2,1
1,3,2,5,4
true
true
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union