Skip to content

Instantly share code, notes, and snippets.

@SolarGeeks
Created April 29, 2017 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SolarGeeks/31f629678720ca8781d20792efe53500 to your computer and use it in GitHub Desktop.
Save SolarGeeks/31f629678720ca8781d20792efe53500 to your computer and use it in GitHub Desktop.
Javascript Logic and data (originally one file)
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
// Get the input and button values
var result = document.querySelector('#result');
var input = document.querySelector('.screen');
var inputVal = input.innerHTML;
var id = '#' + this.getAttribute('id');
var btnVal = this.innerHTML;
console.log(btnVal);
// Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result
// If clear key is pressed, erase everything
if (btnVal == 'C') {
input.innerHTML = '';
}
// If eval key is pressed, calculate and display the result
else if(btnVal == 'Calculate') {
result.innerHTML = Math.floor(list_of_val.reduce(function(a, b) { return a + b; }, 0) / 175) + " Panel(s)!";
}
// Now only the decimal problem is left. We can solve it easily using a flag 'decimalAdded' which we'll set once the decimal is added and prevent more decimals to be added once it's set. It will be reset when an operator, eval or clear key is pressed.
else if(btnVal == '.') {
if(!decimalAdded) {
input.innerHTML += data[inputVal];
decimalAdded = true;
}
}
// if any other key is pressed, just append it
else {
if (data[btnVal] == -1) {
list_of_val.pop();
str_sf.pop()
input.innerHTML = str_sf.join(" + ");
}
else if (data[btnVal] != 0) {
console.log(data[btnVal]);
list_of_val.push(data[btnVal]);
str_sf.push(btnVal);
console.log(list_of_val);
console.log(str_sf);
input.innerHTML = str_sf.join(" + ");
}
}
// prevent page jumps
e.preventDefault();
}
}
var data = {
"del": -1,
"Next": 0,
"Back": 0,
"Request Form": 0,
"Lights": 0,
"10 LED Bulb": 85,
"30 LED Bulb": 255,
"50 LED Bulb": 425,
"10 Fluor. Bulb": 4250,
"30 Fluor. Bulb": 12750,
"50 Fluor. Bulb": 21250,
"General": 0,
"Plasma TV": 300,
"(20in+) LED TV": 80,
"(60in+) LED TV": 150,
"(20in+) LCD TV": 120,
"(60in+) LCD TV": 250,
"Devices": 0,
"Xbox 360": 120,
"Xbox One": 90,
"Play Station 3": 800,
"Play Station 4": 90,
"Wii U": 32.6,
"Smart Phone": 0,
"Computers": 0,
"PC": 275,
"Laptop PC": 75,
"iMac": 230,
"Laptop Mac": 205,
"Printer": 25,
"Router": 10,
"Kitchen": 0,
"Blender": 350,
"Coffe Machine": 1100,
"Oven": 350,
"Microwave": 1150,
"Dish Washer": 275,
"Fridge / Freezer": 275,
"Washroom": 0,
"Electronic Razor": 17.5,
"Blow Dryer": 2150,
"Dryer": 2500,
"Washer": 500,
"Iron": 1000,
"Home": 0,
"A/C" :2500,
"Fan": 50,
"Water Pump": 00
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment