Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, December 16, 2016

Codefights additionWithoutCarrying(param1, param2)


//sample solution
function additionWithoutCarrying(param1, param2) {
    var param1Array = param1.toString().split("").reverse();
    //document.writeln("param1Array: ", param1Array, "<br/>");
    var param2Array = param2.toString().split("").reverse();
    //document.writeln("param2Array: ", param2Array, "<br/>");
    var arrayToModify = [];
    var otherArray = [];
    if(param1Array.length>=param2Array.length){
    arrayToModify=param1Array;
        otherArray=param2Array;
    } else {
    arrayToModify=param2Array;
        otherArray=param1Array;
    }
    //document.writeln("arrayToModify: ", arrayToModify, "<br/>");
    //document.writeln("otherArray: ", otherArray, "<br/>");
    for(i=0;i<arrayToModify.length;i++){
    if(otherArray[i]){
    var temp=columnSum(parseInt(arrayToModify[i]),parseInt(otherArray[i]));
            //document.writeln("arrayToModify[i] ", arrayToModify[i], "<br/>");
            //document.writeln("otherArray[i] ", otherArray[i], "<br/>");
            //document.writeln("columnSum ", temp, "<br/>");
            arrayToModify[i]=temp;
        }
    }
    //document.writeln("arrayToModify: ", arrayToModify, "<br/>");
    //document.writeln(arrayToModify.reverse().join(""),"<br/>");
    return parseInt(arrayToModify.reverse().join(""));
}

function columnSum(a,b){
if(a+b>=10){
    return (a+b)%10;
    } else {
    return (a+b);
    }
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts