Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save worstn8mare/aba3aaaa2207eb3a251e560d6d2b6d54 to your computer and use it in GitHub Desktop.
Save worstn8mare/aba3aaaa2207eb3a251e560d6d2b6d54 to your computer and use it in GitHub Desktop.
/**** sample *****/
function addition(first = 0, second = 0) {
var a = parseFloat(first);
var b = parseFloat(second);
var total = 0;
total = a + b;
return total;
}
function subtraction(first = 0, second = 0) {
var a = parseFloat(first);
var b = parseFloat(second);
var sub = a - b;
return sub;
}
function multiplication(first = 0, second = 0) {
var a = parseFloat(first);
var b = parseFloat(second);
var product = a * b;
return product;
}
function division(first = 0, second = 0) {
var a = parseFloat(first);
var b = parseFloat(second);
var divi = a / b;
return divi;
}
function get_exploded(result,operator){
var holder = 0;
if(result.length <= 1 ){
holder = 0;
}
else if(result.length == 2){
r1 = ((result[0]) ? result[0] : 0);
if(isNaN(r1)){
finder = /(\*|\+|\*|\/|\-)/g;
finder_result = result[0].match(finder);
if(finder_result.length > 0){
exploded_new = result[0].split(""+finder_result+"");
r1_new = ((exploded_new[0]) ? exploded_new[0] : 0);
r2_new = ((exploded_new[1]) ? exploded_new[1] : 0);
r1 = process_operator(finder_result, r1_new, r2_new);
}
}
holder = process_operator(operator, r1, 0);
}
else{
r1 = ((result[0]) ? result[0] : 0);
r2 = ((result[1]) ? result[1] : 0);
holder = process_operator(operator, r1, r2);
}
return holder;
}
function process_operator(operator, r1, r2){
var holder = 0;
if(operator == '+'){
holder = addition(r1, r2);
}
else if(operator == '-'){
holder = subtraction(r1, r2);
}
else if(operator == '*'){
if(r1 == 0 && r2 == 0){
holder = 0;
}
else if(r1 > 0 && r2 == 0){
holder = r1;
}
else if(r1 > 0 && r2 > 0){
holder = multiplication(r1, r2);
}
else{
holder = 0;
}
}
else if(operator == '/'){
if(r1 == 0 && r2 == 0){
holder = 0;
}
else if(r1 > 0 && r2 == 0){
holder = r1;
}
else if(r1 > 0 && r2 > 0){
holder = division(r1, r2);
}
else{
holder = 0;
}
}
return holder;
}
function get_input(e){
var input = $(this).val();
if(e.which == 107){
result = input.split("+");
return $(this).val(get_exploded(result,'+')+'+');
}
else if(e.which == 106){
result = input.split("*");
return $(this).val(get_exploded(result,'*')+'*');
}
else if(e.which == 109){
result = input.split("-");
return $(this).val(get_exploded(result,'-')+'-');
}
else if(e.which == 111){
result = input.split("/");
return $(this).val(get_exploded(result,'/')+'/');
}
}
$('input[type=text], input[type=number]').keyup(get_input);
/**** end sample***/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment