Sign up to the neoG letters to know what's cooking in the camp.

profit or loss

Welcome to this guide which will be a companion with the pair programming workshop. This guide needs to be used alongside the youtube class

Problem Statement

  1. Take the user's stock price for one stock when he bought it.
  2. Take the stocks quantity as well.
  3. Now take today's price of that Stock.
  4. Tell the Profit / Loss, the user's making -
    • In Percentage
    • In Absolute

Tech Stack -

  1. HTML
  2. CSS
  3. JavaScript

What are we building -

Stocks Practice

Step 1: Building the UI

Ex-01: Elements we need in our HTML


  • Create 3 Inputs for Initial Price, Quantity, and the Current Price
  • Create 1 Button Tell me! for Getting the Profile / Loss Output.
  • Create a Div for Displaying the Output.

Ex-02: Adding id to Elements


  • Add id="..." to different elements, so they can be later used in our js file.
<input type='number' id='initial-price' placeholder='Add Initial Price Here...' />

Step 2: Building the Logic

Ex-03: Breaking the problem into pieces


  • Taking Input - Selecting all the elements from HTML.
  • Processing Part 1 - Adding Event Listeners.
  • Processing Part 2 - Creating functions for the logic part.
  • Output - Displaying the Output to the user.
  • Post Completion - Checking bugs and corner cases.

Ex-04: Selecting Elements using querySelector


  • Select Elements that we created in Ex-01, using querySelector
const initialPrice = document.querySelector('#ip_input');

Ex-05: Create Function for Calculating Loss and Profit

We need a function for Calculating -

  • Profit
  • Profit Percentage
  • Loss
  • Loss Percentage


  • Profit = (SellingPrice - CostPrice)
  • Profit Percentage = (Profit/CostPrice)*100
  • Loss = (CostPrice - SellingPrice)
  • Loss Percentage = (Loss/CostPrice)*100


  • Create a Function calculateProfitAndLoss to calculate Loss and Profit.
  • Use Conditional Branching to check if Loss is to be calculated or Profit.
// Conditional Branching
if(sellingPrice > CostPrice){
	// Profit Logic
}else if(costPrice > sellingPrice){
	// Loss Logic
  • Add console.log to check if the function is working properly.

Ex-06: Adding Event Listeners

  • To run the program, we need to add a click event listener to the Submit Button.
submitBtn.addEventListener('click', submitHandler);


  • Create a function submitHandler to handle the button Click.
  • Get the values of inputs using inputName.value, and store them in a variable.
  • Call the function calculateProfitAndLoss, created in Ex-05, after adding some checks.

Bonus (Optional) Ex-07: Handling the corner cases


  • Limit the Decimal points to 2 digits, using toFixed(digits)
  • Restrict the user to enter only numbers, by using type='numbers' as an attribute in the input tag.
  • Add a check to see if the user has no empty inputs. Show alert('Please fill out all Fields') if validation fails.



The Input (Form Input) element

Ex-08: Bonus (Optional)


  • Change the theme of application according to profit and loss, accessing the styles of the element using<cssPropertyName> = 'green';