0

I am trying to do the below using HTML, CSS and JavaScript:

  1. Category (Coffee Appliance) drop down

  2. product (Kurig Coffe Maker) drop down

  3. Wattage : 1500kWh (pre polulated using Local Storage)

  4. Daily Energy Consumption (1,500 W × 1) ÷ 1,000 = 1.5 kWh

  5. Annual energy consumption: User enters no of Days used.(365) 1.5 kWh × 365 = 547.5 kWh

  6. Annual cost: The utility rate is 11 cents per kWh. (User enters utility rate as per his geography) 547.5 kWh × $0.11/kWh = $60.23/year

ALSO : Please find my attempt at writing the PSEUDO CODE in the JS file as comments. Can you please guide me if I am on the the right path.

What is working :Dropdowns

function configureDropDownLists(category, products) {
  var refrigerators = new Array('Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS');
  var dishWasher = new Array('Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana');

<!-- begin snippet: js hide: false -->
#leftColumn {

  width: 500px;

  float: left;

}

.placeholderText {

  font: bold 12px/30px Georgia, serif;

}

body {

  padding-left: 45px;

}

#annualEnergyConsumption {

  font: bold 25px arial, sans-serif;

  color: #00ff00;

}

#annualCostOperation {

  font: bold 40px arial, sans-serif;

  color: #00ff00;

}

.dailyButInline {

  display: inline;

}

/* mouse over link */

button:hover {

  background-color: #00ff00;

}

/* selected link */

button:active {

  background-color: #00ff00;

}
<h2>Annual Energy Consumption and Cost Calculator</h2>

<form id="costForm">

  <div id="leftColumn">

    <div>





      <span class="placeholderText">Choose Category</span>
      </br>
      <span>
<select id="ddl" onchange="configureDropDownLists(this,document.getElementById('products'))">
<option value="">Select a Category</option>
<option value="refrigerators">Refrigerators</option>
<option value="dishWasher">DishWasher</option>
</select>
 </span>
      </br>
      </br>
    </div>

    <div>
      <span class="placeholderText">Select a Product</span>
      </br>
      <span>
 <select id="products">
   <option selected>--------------------------</option>
 </select>
 </span>
      </br>
      </br>
    </div>



    <div>
      <span class="placeholderText">Wattage</span>
      </br>
      <span id="wattage">1500</span>
      </br>
      </br>
    </div>

    <div id="buttonBoundary">
      <div class="placeholderText">Estimated Daily Use</div>

      <div class="dailyButInline">
        <button id="h1">Not a Lot</br>1 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h3">Average</br>3 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h6">A Lot</br>6 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h24">Always On</br>24 hours per day</button>
      </div>

      </br>
      </br>

    </div>
    <div>
      <span class="placeholderText">Daily Energy Consumption</span>
      </br>
      <div id="annualEnergyConsumption">0.268 kWh</div>
      </br>
    </div>


    <div>
      <span class="placeholderText">Approximate Days used per year</span>
      </br>
      <div id="daysUsed">
        <input type="number" id="hour" maxlength="2" min="1">
        </br>
        <input type="checkbox" id="allYear" />
        <label for="allYear">All year</label>
      </div>
      </br>
    </div>

    <div>
      <span class="placeholderText">Annual Energy Consumption</span>
      </br>
      <div id="annualEnergyConsumption">547.5 kWh</div>
      </br>
    </div>




  </div>


  <div id="right">


    <div>
      <span class="placeholderText">Enter your Utility Rate per Kw/h</span>
      </br>
      <span><input type="number" id="utilityRate" /></span>
      </br>
      </br>
    </div>


    <div>
      <span class="placeholderText"><button id="annualCost">Annual Cost to Operate</button></span>
      </br>
      <span id="annualCostOperation" /></span>
    </div>

  </div>

</form>

</body>
  switch (category.value) {
    case 'refrigerators':
      products.options.length = 0;
      for (i = 0; i < refrigerators.length; i++) {
        createOption(products, refrigerators[i], refrigerators[i]);
      }
      break;
    case 'dishWasher':
      products.options.length = 0;
      for (i = 0; i < dishWasher.length; i++) {
        createOption(products, dishWasher[i], dishWasher[i]);
      }
      break;
    default:
      products.options.length = 0;
      break;
  }

}

function createOption(ddl, text, value) {
  var opt = document.createElement('option');
  opt.value = value;
  opt.text = text;
  ddl.options.add(opt);
}

/*  

    dailyEnergyConsumption = function() 

initialize dailyEnergyConsumption = null; Check what the user has selected from the products drop down and display that unique wattage. Wattage is stored is stored in Local Storage. it will be called based on what product is selected.

Store and Call the selected wattage as we need that to calculate Daily Energy Consumption

Track the 4 buttons with an Event Listener (Estimated Daily Use)

Based on the above pass wattage to the formula (1,500 W × 1) ÷ 1,000 = 1.5 kWh Display and store 1.5 Kwh Make sure the Category and Products drop down don’t get reset. }

annualEnergyConsumption = function() { take the variable from dailyEnergyConsumption and multiply that by no of days the user enters 1.5 kWh × 365 (days) = 547.5 kWh Store it do not display this Pass it to annual Cost function }

annualCost = function() { take 547.5 Kwh from annualEnergyConsumption function capture what the user enters in Utility Rate textbox (11 cents per K Wh) So ( Calculate 547.5 kWh × $0.11/kWh = $60.23/year) Display 60.23 /year }

*/
#leftColumn {

  width: 500px;

  float: left;

}

.placeholderText {

  font: bold 12px/30px Georgia, serif;

}

body {

  padding-left: 45px;

}

#annualEnergyConsumption {

  font: bold 25px arial, sans-serif;

  color: #00ff00;

}

#annualCostOperation {

  font: bold 40px arial, sans-serif;

  color: #00ff00;

}

.dailyButInline {

  display: inline;

}

/* mouse over link */

button:hover {

  background-color: #00ff00;

}

/* selected link */

button:active {

  background-color: #00ff00;

}
<h2>Annual Energy Consumption and Cost Calculator</h2>

<form>

  <div id="leftColumn">

    <div>





      <span class="placeholderText">Choose Category</span>
      </br>
      <span>
<select id="ddl" onchange="configureDropDownLists(this,document.getElementById('products'))">
<option value="">Select a Category</option>
<option value="refrigerators">Refrigerators</option>
<option value="dishWasher">DishWasher</option>
</select>
    </span>
      </br>
      </br>
    </div>

    <div>
      <span class="placeholderText">Select a Product</span>
      </br>
      <span>
    <select id="products">
            <option selected>--------------------------</option>
    </select>
    </span>
      </br>
      </br>
    </div>



    <div>
      <span class="placeholderText">Wattage</span>
      </br>
      <span id="wattage">1500</span>
      </br>
      </br>
    </div>

    <div id="buttonBoundary">
      <div class="placeholderText">Estimated Daily Use</div>

      <div class="dailyButInline">
        <button id="h1">Not a Lot</br>1 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h3">Average</br>3 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h6">A Lot</br>6 hour per day</button>
      </div>
      <div class="dailyButInline">
        <button id="h24">Always On</br>24 hours per day</button>
      </div>

      </br>
      </br>

    </div>



    <div>
      <span class="placeholderText">Approximate Days used per year</span>
      </br>
      <div id="daysUsed">
        <input type="number" id="hour" maxlength="2" min="1">
        </br>
        <input type="checkbox" id="allYear" />
        <label for="allYear">All year</label>
      </div>
      </br>
    </div>

    <div>
      <span class="placeholderText">Enter your Utility Rate per Kw/h</span>
      </br>
      <span><input type="number" id="utilityRate" /></span>
      </br>
      </br>
    </div>


  </div>


  <div id="right">



    <div>
      <span class="placeholderText">Annual Energy Consumption</span>
      </br>
      <div id="annualEnergyConsumption">547.5 kWh</div>
      </br>
    </div>


    <div>
      <span class="placeholderText">Annual Cost to Operate</span>
      </br>
      <span id="annualCostOperation" />$10.76 / year</span>
    </div>

  </div>

</form>

1 Answers1

0

You seem to be on the right track. I would make two corrections.

Instead of this:

var refrigerators = new Array('Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS');
var dishWasher = new Array('Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana');

I would write:

var appliances = {
 refrigerators:  [ 
      'Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS'
 ],
 dishwashers:    [ 
      'Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana' 
 ]
};

That was I could later do this:

var appliance = appliances[category.value];

And not have to duplicate any code.

Also, every time you change the category, you have to clear out the existing options in the product pull-down

Finally, Javascript is case-sensitive, so if you write both "dishWasher" and "dishwasher", you are going to have a bad time.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
  • thanks a lot. I had some few follow up questions: Once I get this going 1) I plan to give the users a Save button to save 3 products and Compare using a simple bar chart. X axis will have 3 products Y axis will have Cost. Can I achieve this later with this idea or do I need to make changes to my structure. I plan to learn about chart libraries. – Accidental_Everything May 02 '15 at 17:40
  • You can do what you are proposing from this starting point, but if you are going to make anything elaborate, I would suggest you learn jQuery, it is the easiest framework for doing things like this. Look into D3 for your charting library. Also "Save" might be tricky: saving is not something that browsers do well. – Michael Lorton May 02 '15 at 19:59