🔥💻 Accepted 92.6%✅ || JavaScript & TS 🔊 || Easy solution
Best Time To Buy and Sell Stock\n### Firstly, from the question, we can easily realize that we need to have three different functions such as maxProfit(). Now, 
# Best Time To Buy and Sell Stock\n### Firstly, from the question, we can easily realize that we need to have three different functions such as `maxProfit()`. Now, let\'s see the result, Sure! Here\'s the JavaScript code that solves the problem:\n\n# 1. Traditional Function way:\n> ###### A traditional function is defined using the function keyword. It can be a named function or an anonymous function assigned to a variable.\n- In this code, the `maxProfit` function takes an array prices as input. It initializes the `maxProfit` variable to 0 and the `minPrice` variable to the maximum possible value. This ensures that any price encountered will be smaller than `minPrice` initially.\n\n- Then, it iterates through the `prices` array using a loop. For each element, it checks if the current price is smaller than `minPrice`. If it is, the `minPrice` is updated to the current price.\n\n- If the current price minus `minPrice` is greater than `maxProfit`, it updates `maxProfit` to the new value.\n\n- After iterating through all the prices, the function returns the final `maxProfit`.\n```\n/**\n * @param {number[]} prices\n * @return {number}\n */\nfunction maxProfit(prices) {\n  let maxProfit = 0;\n  let minPrice = Number.MAX_VALUE;\n\n  for (let i = 0; i < prices.length; i++) {\n    if (prices[i] < minPrice) {\n      minPrice = prices[i];\n    } else if (prices[i] - minPrice > maxProfit) {\n      maxProfit = prices[i] - minPrice;\n    }\n  }\n\n  return maxProfit;\n}\n```\n- In this example, the array `prices` represents the stock prices on different days. The function `maxProfit` is called with prices as an argument, and it returns the maximum profit that can be achieved by buying and selling the stock. The expected output is `5`.\n```\n    const prices = [7, 1, 5, 3, 6, 4];\n    console.log(maxProfit(prices)); // Output: 5\n\n```\n![7abc56.jpg](https://assets.leetcode.com/users/images/add9a7b7-1050-4d7f-8976-6c284ce97d1c_1699174266.1705523.jpeg)\n\n
['TypeScript', 'JavaScript']