Supply and Demand in Code!

By Hussain Syed, this will talk about how to make you very own supply and demand machine! its pretty cool

Supply and Demand in Code!

Hello Welcome this is the first time I make an article on hashnode. I'm very new and was a coder for 1 year I am 11 years old and my name is Hussain Syed! I will explain how to make my supply and demand machine!

in supply and demand you want the right numbers, you want it balanced you don't want to much supply and a very low demand or a high demand and a low supply we want some good numbers and this machine calculates random supply and demand problems and calculates how much you earned, the price, supply, and demand (also refunds if there is one!) first we need some variables, the variables that we are gonna make are gonna be supply, demand, and price we will also put earned and refund as well!

when you done it should look like this

  supply = int(random(0, 10))
  demand = int(random(0, 10))
  price = int(random(0, 10))
  leftover = supply - demand
  refund = leftover * price
  earned = price * demand

there we have it the variables you can take a look to see how it works for a second if you want... done? ok good now I will explain why there is this variable called leftover, leftover calculates the supply thats left we will use this in refund because we need the amount of orders that were not given for example when there is a refund the leftover variable is negative resulting in missing orders!

lets say the price is 3 and there is 6 orders missing (-6) we will multiply these 2 numbers to get how much money needs to be refunded!

you might be asking well Hussain why is the variable earned not including the refund in the equation? well we are gonna use if statements!

console.log("results of the day!")
  console.log(supply + " supply")
  console.log(demand + " demand")
  console.log(price + " price")
  console.log("supply left for next day! " + leftovers) 


  if (supply > demand) {
    console.log("inflation!")
    console.log("profit")
    console.log(earned + " earned")
  }

  if (supply < demand) {
    console.log("deflation!")
    console.log("refund")
    console.log(earned + refund + " earned")
  }

you might see a pattern here and you would be right first I printed out the results and then there is 2 if statements one for inflation which is totally normal and one is deflation which is interesting, when we said refund is negative it was! and here we are adding it? well if you don't know yet - and + make - so this code would do it like : earned + - refund + "earned"! the +- is equal to + theres no difference so it will take away the money that needs to be refunded from earned!

thank you and hoped you enjoyed putting either draw or setup functions are optional you can pick which and you can experiment but please don't copy and I hoped you learned something!