diff --git a/Solution.js b/Solution.js index b55091c..f1211e2 100644 --- a/Solution.js +++ b/Solution.js @@ -1 +1,157 @@ -// Write your code here +// +// Solution by: Akshath Kaushal +// GitHub: https://github.com/akshathkaushal +// LinkedIn: www.linkedin.com/in/akshath-kaushal-08277817b +// + + +// object class +class FixEquation { + + findMissingDigit(str) { + // str is of the form A * B + C = D + // in this either A, B, C or D has a '?' that has to be found + + + // step 1: separate the numeric values from the equation + let values = this.separateValues(str); + let mul1=values[0], mul2=values[1], add=values[2], res=values[3]; + + // step 2: find position (1/2/3/4) of the number(A/B/C/D) containing a '?' + let qnmarkPos = this.findQuestionMark(mul1, mul2, add, res); + + // step 3: based on the position of the found number, calculate the actual value of that number + let temp = -1, ans=-1, arg2=""; + if(qnmarkPos==1){ // '?' is found in A + temp = (parseFloat(res) - parseFloat(add))/parseFloat(mul2); + ans = parseInt(temp); + arg2=mul1; + } + else if(qnmarkPos==2){ // '?' is found in B + temp = (parseFloat(res) - parseFloat(add))/parseFloat(mul1); + ans = parseInt(temp); + arg2=mul2; + } + else if(qnmarkPos==3){ // '?' is found in C + temp = parseFloat(res) - (parseFloat(mul2)*parseFloat(mul1)); + ans = parseInt(temp); + arg2=add; + } + else if(qnmarkPos==4){ // '?' is found in D + temp = parseFloat(mul1)*parseFloat(mul2) + parseFloat(add); + ans = parseInt(temp); + arg2=res; + } + + // step 4: return the digit that should replace the '?' + if(ans < 0 || temp - ans != 0) { // if the ans is negative or not a whole number + return -1; + } + return this.findDigit(ans.toString(), arg2); + } + + // ================= helper functions ================= // + + // + // this function separates the differnt numbers by iterating through the string + // return type: a list of the numbers as strings + // + separateValues(str){ + + let mul1="", mul2="", add="", res=""; + let i = 0, sz = str.length; + + while(str[i] != '*'){ + if(str[i]!=' ') mul1+=str[i]; + i++; + } + i+=2; + while(str[i] != '+'){ + if(str[i]!=' ') mul2+=str[i]; + i++; + } + i+=2; + while(str[i] != '='){ + if(str[i]!=' ') add+=str[i]; + i++; + } + i+=2; + while(i