Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // /**** 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,'/')+'/');
- }
- else if(e.which == 13){
- if(isNaN(input)){
- result[0] = input;
- result[1] = 0;
- check_operator = /(\*|\+|\*|\/|\-)/g;
- operator_result = result[0].match(check_operator);
- return $(this).val(get_exploded(result,operator_result));
- }
- else{
- return $(this).val(input);
- }
- }
- }
- $('input[type=text], input[type=number]').keyup(get_input);
- // /**** end sample***/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement