How to sort elements in array without changing other elements indexes? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
How to sort an array of odd numbers in ascending order, but keep even numbers at their position?
6 answers
I have this array:
var arr = [5, 3, 2, 8, 1, 4];
I'm trying to sort ONLY the elements that are odd values so I want this
output:
[1, 3, 2, 8, 5, 4]
As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.
javascript
marked as duplicate by Nina Scholz
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 19 at 9:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to sort an array of odd numbers in ascending order, but keep even numbers at their position?
6 answers
I have this array:
var arr = [5, 3, 2, 8, 1, 4];
I'm trying to sort ONLY the elements that are odd values so I want this
output:
[1, 3, 2, 8, 5, 4]
As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.
javascript
marked as duplicate by Nina Scholz
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 19 at 9:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to sort an array of odd numbers in ascending order, but keep even numbers at their position?
6 answers
I have this array:
var arr = [5, 3, 2, 8, 1, 4];
I'm trying to sort ONLY the elements that are odd values so I want this
output:
[1, 3, 2, 8, 5, 4]
As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.
javascript
This question already has an answer here:
How to sort an array of odd numbers in ascending order, but keep even numbers at their position?
6 answers
I have this array:
var arr = [5, 3, 2, 8, 1, 4];
I'm trying to sort ONLY the elements that are odd values so I want this
output:
[1, 3, 2, 8, 5, 4]
As you can see the even elements don't change their position. Can anyone tell me what I'm missing? Here's my code:
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
I know I can use slice to add elements to array, but I'm stuck on how to get the indexes and put the elements in the array.
This question already has an answer here:
How to sort an array of odd numbers in ascending order, but keep even numbers at their position?
6 answers
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort();
, );
return oddElements;
console.log(myFunction([5, 3, 2, 8, 1, 4]));
javascript
javascript
edited Jan 19 at 6:00
progx
asked Jan 19 at 5:55
progxprogx
328522
328522
marked as duplicate by Nina Scholz
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 19 at 9:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nina Scholz
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 19 at 9:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
One option is to keep track of the indicies of the odd numbers in the original array, and after .reduce
ing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since.sort
sorts lexiographically - use a custom.sort
function instead, see edit
– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
add a comment |
First sort only the odd numbers and put it in an array oddSorted
. Then map
through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted
array.
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
easy-to-follow answer. now, can you think of a way to do this without having to do the% 2 != 0
calculation twice per element? :D
– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in theoddSorted
array, if yes then replace the current number with the first number from theoddSorted
array. This will also work as if the current element is included in theoddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?
– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clonearr
. Filter will return a new array anyway.
– kremerd
Jan 19 at 8:32
add a comment |
I modified your code a little bit to fulfill your objective. Take a look below
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
One option is to keep track of the indicies of the odd numbers in the original array, and after .reduce
ing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since.sort
sorts lexiographically - use a custom.sort
function instead, see edit
– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
add a comment |
One option is to keep track of the indicies of the odd numbers in the original array, and after .reduce
ing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since.sort
sorts lexiographically - use a custom.sort
function instead, see edit
– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
add a comment |
One option is to keep track of the indicies of the odd numbers in the original array, and after .reduce
ing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
One option is to keep track of the indicies of the odd numbers in the original array, and after .reduce
ing and sorting, then iterate through the original odd indicies and reassign, taking from the sorted odd array:
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
function oddSort(array)
const oddIndicies = ;
const newArr = array.slice();
const sortedOdd = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
oddIndicies.push(index);
return arr;
, )
.sort((a, b) => a - b);
while (oddIndicies.length > 0)
newArr[oddIndicies.shift()] = sortedOdd.shift();
return newArr;
console.log(oddSort([5, 3, 2, 8, 1, 4]));
console.log(oddSort([5, 3, 2, 8, 1, 4, 11 ]));
edited Jan 19 at 6:09
answered Jan 19 at 6:00
CertainPerformanceCertainPerformance
84.6k154169
84.6k154169
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since.sort
sorts lexiographically - use a custom.sort
function instead, see edit
– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
add a comment |
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since.sort
sorts lexiographically - use a custom.sort
function instead, see edit
– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:06
Ah, the 11 was throwing things off, since
.sort
sorts lexiographically - use a custom .sort
function instead, see edit– CertainPerformance
Jan 19 at 6:10
Ah, the 11 was throwing things off, since
.sort
sorts lexiographically - use a custom .sort
function instead, see edit– CertainPerformance
Jan 19 at 6:10
Thanks a lot bro!
– progx
Jan 19 at 21:21
Thanks a lot bro!
– progx
Jan 19 at 21:21
add a comment |
First sort only the odd numbers and put it in an array oddSorted
. Then map
through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted
array.
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
easy-to-follow answer. now, can you think of a way to do this without having to do the% 2 != 0
calculation twice per element? :D
– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in theoddSorted
array, if yes then replace the current number with the first number from theoddSorted
array. This will also work as if the current element is included in theoddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?
– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clonearr
. Filter will return a new array anyway.
– kremerd
Jan 19 at 8:32
add a comment |
First sort only the odd numbers and put it in an array oddSorted
. Then map
through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted
array.
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
easy-to-follow answer. now, can you think of a way to do this without having to do the% 2 != 0
calculation twice per element? :D
– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in theoddSorted
array, if yes then replace the current number with the first number from theoddSorted
array. This will also work as if the current element is included in theoddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?
– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clonearr
. Filter will return a new array anyway.
– kremerd
Jan 19 at 8:32
add a comment |
First sort only the odd numbers and put it in an array oddSorted
. Then map
through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted
array.
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
First sort only the odd numbers and put it in an array oddSorted
. Then map
through each element in the original array and check if the current element is odd, if odd replace it with the corresponding sorted number from the oddSorted
array.
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
function sortOddElements(arr)
var oddSorted = arr.filter(ele => ele %2 != 0).sort((a, b) => a - b);
var evenNotSorted = arr.map((ele, idx) =>
if(ele % 2 != 0)
return oddSorted.shift();
return ele;
);
return evenNotSorted;
var arr = [5, 3, 2, 8, 1, 4];
console.log(sortOddElements(arr));
arr = [5, 3, 2, 8, 1, 4, 11 ];
console.log(sortOddElements(arr));
edited Jan 19 at 8:43
answered Jan 19 at 6:30
Amardeep BhowmickAmardeep Bhowmick
2,56511022
2,56511022
easy-to-follow answer. now, can you think of a way to do this without having to do the% 2 != 0
calculation twice per element? :D
– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in theoddSorted
array, if yes then replace the current number with the first number from theoddSorted
array. This will also work as if the current element is included in theoddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?
– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clonearr
. Filter will return a new array anyway.
– kremerd
Jan 19 at 8:32
add a comment |
easy-to-follow answer. now, can you think of a way to do this without having to do the% 2 != 0
calculation twice per element? :D
– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in theoddSorted
array, if yes then replace the current number with the first number from theoddSorted
array. This will also work as if the current element is included in theoddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?
– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clonearr
. Filter will return a new array anyway.
– kremerd
Jan 19 at 8:32
easy-to-follow answer. now, can you think of a way to do this without having to do the
% 2 != 0
calculation twice per element? :D– user633183
Jan 19 at 6:35
easy-to-follow answer. now, can you think of a way to do this without having to do the
% 2 != 0
calculation twice per element? :D– user633183
Jan 19 at 6:35
One thing that comes to my mind is checking whether the current element is included in the
oddSorted
array, if yes then replace the current number with the first number from the oddSorted
array. This will also work as if the current element is included in the oddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?– Amardeep Bhowmick
Jan 19 at 6:44
One thing that comes to my mind is checking whether the current element is included in the
oddSorted
array, if yes then replace the current number with the first number from the oddSorted
array. This will also work as if the current element is included in the oddSorted
array then it implies that the number is odd and hence there is no need to check it again. But do you think this will improve the performance?– Amardeep Bhowmick
Jan 19 at 6:44
You don't need to clone
arr
. Filter will return a new array anyway.– kremerd
Jan 19 at 8:32
You don't need to clone
arr
. Filter will return a new array anyway.– kremerd
Jan 19 at 8:32
add a comment |
I modified your code a little bit to fulfill your objective. Take a look below
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
add a comment |
I modified your code a little bit to fulfill your objective. Take a look below
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
add a comment |
I modified your code a little bit to fulfill your objective. Take a look below
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()
I modified your code a little bit to fulfill your objective. Take a look below
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
Remember, the default sort function sorts the values alphabetically. That's why you can't just use arr.sort()
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
function myFunction(array)
var oddElements = array.reduce((arr, val, index) =>
if (val % 2 !== 0)
arr.push(val);
return arr.sort(function(a, b)return a - b);
, );
var index = 0;
var finalElements = ;
for(var i=0; i<array.length; i++)
var element = array[i];
if(element %2 !==0)
finalElements.push(oddElements[index]);
index++;
else
finalElements.push(element);
return finalElements;
console.log(myFunction([5, 3, 2, 8, 1, 4, 11]));
edited Jan 19 at 6:17
answered Jan 19 at 6:08
Tanmoy Krishna DasTanmoy Krishna Das
625414
625414
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
add a comment |
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Nope, your solution doesn't work when I pass this array: [5, 3, 2, 8, 1, 4, 11 ], it supposes to return [1, 3, 2, 8, 5, 4, 11], but it returns [1, 11, 2, 8, 3, 4, 5]
– progx
Jan 19 at 6:09
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
Okay... I'm taking a look at it
– Tanmoy Krishna Das
Jan 19 at 6:10
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
The problem was with the sort function. The default sort function sorts the values alphabetically. That's why you can't just use arr.sort(). I modified the code to reflect your needs.
– Tanmoy Krishna Das
Jan 19 at 6:18
add a comment |