The question of the challenge is :
Modify the function to return a copy of the given array sorted in ascending order (1, 2, 3, etc). Do not modify the original array.
I've tried doing a normal for loop and an if statement :
let original = [1, 7, 3, 5];
let sortedCopy = []
for (i = 0; i < original.length; i++){
if (original[i] > original[i+1]){
sortedCopy.push.([1])
}
}
console.log(sortedCopy);
Now I'm trying to use a .map method since it automatically loops and passes each number through the callback
function copyAndSortNumbers(numbers) {
this.numArray = numbers;
numArray.map(sortingArray)
function sortingArray (numbers){
if (numbers[i] > numbers[i+1]){
return numbers;
}
}
}
const original = [1, 7, 3, 5];
const sortedCopy = copyAndSortNumbers(original);
I should get a new ordered array but I don't see what I'm missing
Edit:
here is an updated version, it's now returning an array but still not sorting it
function copyAndSortNumbers(numbers) {
numArray = numbers;
numArray.map(sortingArray)
function sortingArray (numbers){
if (numbers > numbers + 1){
return numbers;
}
}
return numArray;
}
.map()should be used for 2. Have a look at:Array.prototype.map()on how.map()works and what the arguments of the callback are.map() !== .sort().map()things it's not supposed to. And by this I mean as a replacement to.forEach(). I'm not sure where that misconception is coming from.