Simple JavaScript Solution
Solution\njs\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n    // set to first element in array by default\n 
### Solution\n```js\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n    // set to first element in array by default\n    let minprice = prices[0];\n    let profit = 0;\n    for (let i = 0; i < prices.length; i++) {\n        if (prices[i] < minprice) {\n            minprice = prices[i];\n        } else if (prices[i] - minprice > profit) {\n            profit = prices[i] - minprice;\n        }\n    }\n     return profit\n};\n```\n\n### LeetCode Output\nRuntime: **89 ms**, faster than **93.23%** of JavaScript online submissions for Valid Parentheses.\nMemory Usage: **52 MB**, less than **24.52%** of JavaScript online submissions for Valid Parentheses.\n\n[Link to Submission](https://leetcode.com/submissions/detail/799297619/)
['JavaScript']