📖 Scenario: You are helping a friend who wants to understand how to make money by buying and selling stocks. They have a list of stock prices for several days and want to find the best way to buy and sell to maximize profit.
🎯 Goal: Build a simple program that finds the best day to buy and sell stocks to get the maximum profit. You will start by creating the stock prices list, then set up a variable to track profit, write the logic to find the best profit, and finally print the result.
📋 What You'll Learn
Create an array called prices with the exact values: [7, 1, 5, 3, 6, 4]
Create a variable called maxProfit and set it to 0
Use a for loop with variable i to iterate over prices
Inside the loop, use another for loop with variable j to check all days after i
Calculate profit as prices[j] - prices[i] and update maxProfit if this profit is higher
Print the value of maxProfit
💡 Why This Matters
🌍 Real World
This project helps understand how to analyze stock prices to make smart buy and sell decisions for profit.
💼 Career
Knowing how to work with arrays and loops to solve problems is essential for software development and data analysis jobs.
Progress0 / 4 steps
1
Create the stock prices array
Create an array called prices with these exact values: [7, 1, 5, 3, 6, 4]
DSA Javascript
Hint
Use const to create the array named prices with the exact numbers inside square brackets.
2
Set up the maxProfit variable
Create a variable called maxProfit and set it to 0
DSA Javascript
Hint
Use let to create a variable named maxProfit and set it to zero.
3
Find the maximum profit with nested loops
Use a for loop with variable i to iterate over prices. Inside it, use another for loop with variable j to check all days after i. Calculate profit as prices[j] - prices[i] and update maxProfit if this profit is higher.
DSA Javascript
Hint
Use two loops: the outer loop with i from 0 to prices.length, and the inner loop with j from i + 1 to prices.length. Calculate profit and update maxProfit if profit is bigger.
4
Print the maximum profit
Write console.log(maxProfit) to display the maximum profit found.
DSA Javascript
Hint
Use console.log(maxProfit) to print the final answer.
Practice
(1/5)
1. What is the main goal when solving the Buy and Sell Stocks problem variants?
easy
A. Maximize the profit by choosing the best days to buy and sell stocks
B. Minimize the number of transactions regardless of profit
C. Find the day with the highest stock price only
D. Calculate the average stock price over all days
Solution
Step 1: Understand the problem goal
The problem asks to find the best days to buy and sell stocks to get the highest profit.
Step 2: Identify the objective
Maximizing profit means buying low and selling high at the right times.
Final Answer:
Maximize the profit by choosing the best days to buy and sell stocks -> Option A
Quick Check:
Goal = Maximize profit [OK]
Hint: Focus on profit maximization, not just prices or counts [OK]
Common Mistakes:
Thinking the goal is to minimize transactions
Only looking for highest price day
Calculating average price instead of profit
2. Which of the following is the correct syntax to initialize a variable maxProfit to 0 in JavaScript?