I have an integer array
let arr=[7,1,5,3,6,4]
i am using this array to sort it in ascending order and extract the position of the first smallest element in the original array.My function is defined as
const maxProfit = function(prices) {
let org=prices
prices.sort((a,b)=>{return a-b})
console.log(org)
let ind=org.indexOf(prices[0])
console.log(ind)
};
console.log(maxProfit([7,1,5,3,6,4]))
i was interested in the value of org which is suppose to be [7,1,5,3,6,4] instead i got it as [1,3,4,5,6,7] why the assigning of the original array not working here?
let org=pricesdoesn't clone the array. It's the same (now sorted) array.let org = prices.slice().