neoG Camp 2023 waitlist is open

Need Help?

lessonTen: profit and loss app

This guide needs to be used alongside the lesson video.

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 used: Html, CSS, and Javascript

what are we building -

Stocks Practice


step 1: building the UI

ex01: elements we need in our HTML

challenge

  • 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.

ex02: adding id to Elements

challenge

  • 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

ex03: breaking the problem into pieces

challenge

  • 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.

ex04: selecting Elements using querySelector

challenge

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

ex05: create Function for Calculating Loss and Profit

We need a function for Calculating -

  • Profit
  • Profit Percentage
  • Loss
  • Loss Percentage

formula

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

challenge

  • 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.

ex06: adding event listeners

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

challenge

  • 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

challenge

  • 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.

references:

Number.prototype.toFixed()

The Input (Form Input) element

ex08: bonus (Optional)

challenge

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

references:

HTMLElement.style


**assignments for markFourteen**

Use this guide to build a "profit or loss" app in VanillaJS or React and this would be your markFourteen project from the mark15 checklist.


stuck? need help?

If you have doubts anywhere during levelZero, we have a 21k+ strong developer community on Discord.

Go to Discord Server and ask doubts in the general-coding channel.