This a shot code helping to find out the length of digits without converting into String : const nbr = 3214; console.log('===> length of a number: ', Math.floor( Math.log10( nbr) ) + 1 );
Amazing! this is how I would do. no need to search function count(N){ let count = 1; let result = N; let i = 10; while(result>10){ result = N/i; i = i*10; count++; } return count; } console.log(count(232));
I did this and it works function lengthInteger(num){ let count = 0; while(num!==0){ let temp = parseInt(num/10); num = temp; count +=1; } return count }
For the 2nd problem can't you just do this? function integerLength(nr){ return Math.floor(Math.log10(nr))+1 } For me this works for any integer between 1 and 9e15
For Second problem this is my solution function getLength(x){ let num = x; let counter =0; while(num>0){ num = num/10; if(num >=1)counter++; else{counter++;return counter;} } }
Hah thought an easier way for the number length problem. Times the number by 0.1 and floor it. If it’s not zero then repeat. Great vids keep up the good work!
My solution to maximum number, inspired by a solution on a C++ forum: let x = 1234; function numDigits(num) { let digits = num === 0 ? 1 : Math.log10(Math.abs(num)) + 1 return Math.floor(digits); } console.log("Result=", numDigits(x) )
This is how i did the count number question without changing it to string function intLength(int){ let counter=0; while(int>=1){ int=parseInt(int/10); counter++; } return counter; } console.log(intLength(12347687658));
my approach for the first question : let tab = [1, 2, 3, 4] let result = [] tab.forEach(el => { result.push(tab.filter(val => val !== el).reduce((a, b) => a + b)) }) console.log(result.sort((a, b) => b - a)[0], result.sort((a, b) => b - a)[result.sort((a, b) => b - a).length - 1])
for length of the given Integer. i did something like this rather than using while loop and it works though! var tn=-1; function countInt(x){ tn++; return (parseInt(x)/10===0)?tn : countInt(parseInt(x)/10) } countInt(1234567);
function findNumDigits(x) { let ln = 0; while(x !== 0) { ln++; x = Math.floor(x/10); } return ln; } Isn't this simpler if we know that the number is an integer?
Around ~22 a quick 'binary-esque' implementation would be to use the Math.log() function to compute the power of a number, then round up to get a numerical expression for the number of digits in the number. If the test value is a perfect power of the base, just add one. Also, logs break for x
Question #2 function findNumDigits(x) { if (x < 0) return 0; if (x < 10) return 1; return 1 + findNumDigits(x/10); } worked for me..Is there any corner cases which I missed?
Here's a version that I find slightly easier to read (uses es6 'fat arrow' function syntax, though): // ------------------------------- const countDigitsRecursively = num => { if (num / 10 < 1) { return 1; } return countDigitsRecursively(num / 10) + 1; }; console.log(countDigitsRecursively(1234)); // ------------------------------- Basically it takes the input, divides it by 10, and if the result is less than 1, it immediately returns 1-easy peasy, right? OTOH, if the initial result is greater than 1, it takes the current value of the input, divides it by 10, and feeds THAT value back into the function, repeating the process until the result IS less than 1, at which point it hits that initial `return` statement (called the "base case" in recursion-talk) and then (and this is the tricky part) it takes that return value of 1, and "backs out", adding 1 to the return result each time. In the example above, that looks like this: call #1: 1234 / 10 = 123.4 // result not < 1; divide by 10 and keep trying call #2: 123.4 / 10 = 12.34 // still not there... call #3: 12.34 / 10 = 1.234 // getting close... call #4: 1.234 / 10 = .1234 // base case reached! return 1 to the previous function call #3: // received return value of 1; return 1 + 1 to previous function call #2: // received return value of 2; return 2 + 1 to previous function call #1: // received return value of 3; return 3 + 1 to previous function console.log(): // received return value of 4; print it to the console!
Wow thank you for the explanation, it make a little more sense now! I'm still a bit confused on the 'received return value' ie: the second #3. How are the returned values "stored" and added if there is no placeholder variable for them? I hope that makes sense. I consoled the num value above the if so I can see the base case being reached so that makes more sense.
Yeah, that's why I noted that that was the "tricky" part. :-) Think of it this way: when you call a function that returns a value, you can use that value directly, without having to store it a variable, right? (That's why we could do something like, e.g., `console.log(countDigitsRecursively(1234) * 2);`, and expect it to print '8' to the console.) It's the same thing /inside/ the recursive function: once the base case is reached, it passes back the specified return value to the function that called it (which, in the case of a recursive function is itself, but it helps here to imagine different instances of the function, which is why I labelled them 'call #1, call #2, etc.). When the calling function receives a return value, it can then use that value and manipulate it in any way it wants without having to store it in a named variable. Does that help?
// 1. Find the min and max sums of the remaining numbers in an array, after removing one number from the array. const maxMinSums = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); const sum = arr.reduce((acc, curr) => acc + curr); return [sum - max, sum - min]; } // 2. Find the length of an integer without converting it to a string. const intLength = (num) => { if (num === 0) { return 1; } let length = 0; let int = num; while (int > 0) { int = Math.floor(int/10); length++; } return length; }
For Q2, I would follow the same approach only, but a little less complicated: function lengthOfNumber(num) { let count = 0; while (true) { num = num / 10; count++; if (num / 10
I thought for Q1 this approach would be better: function getSumRange(arr) { let max = arr[0]; let min = arr[0] let total = 0; arr.forEach(elem => { total = total + elem; max = max < elem ? elem : max; min = min > elem ? elem : min; }); return [(total-max), (total-min)]; } As in this case the time complexity would be O(n) only.
let num = 131333; const findLengthOfNumber = (num) => { let dec = 1; let con = true; let counter = 0; while (con) { let n = num / dec; counter += 1; dec *= 10; if (n < 1) con = false; continue; } return counter - 1; }; console.log("Length is :", findLengthOfNumber(num));
Third Question function findLength(num) { let len = 0; for(let i = 0; i < 17; i++) { if(Math.ceil(Math.pow(10, i)/num) == 1) { len = i + 1; } } return len; } let num = 13243546; console.log(findLength(num));
I do not understand how maximum summary of numbers from an array after taking one number out is equal to summary -max. I would think that for maximum you have to minus smallest number in an array and for minimum summary you would need to minus largest number of the array.
Simple solution is using parseInt var number = 1234; var count = 0; while(number !=0) { number = number/10; number = parseInt(number); count++ } alert(count);
here is my solution: let x = 1234; const findLength = n => { let results = 1; while( n > 1){ results++; n = n / 10; } return results; } console.log(findLength(x))
Here’s my solution to the last problem. Hopefully, it will help anyone who struggled implementing the binary search part. function findLengthOfN(n) { // MAX_SAFE_INTEGER is the variable discussed in the video const max = Number.MAX_SAFE_INTEGER; if (n > max) return new Error("Javascript can't perform math with numbers this large :("); if (isSingleDigit(n) || n === 0) return 1; let maxLength = 16; // > StopIndex let minLength = 2; // > StartIndex - start at 2 because of the 2nd if statement above let currentLength = Math.floor((minLength + maxLength) / 2); // > Middle // Base case - When divided by the correct power of 10 AND rounded down: // the number should be a single digit while (!isSingleDigit(Math.floor(n/getCurrentPowerOfTen())) && currentLength < maxLength) { if (Math.floor(n/getCurrentPowerOfTen()) > minLength) { minLength = currentLength + 1; } else { maxLength = currentLength - 1; } currentLength = Math.floor((minLength + maxLength) / 2); } return currentLength; function getCurrentPowerOfTen() { return Math.pow(10, currentLength-1); } function isSingleDigit(number) { return (number >= 1 && number
Just wonder and please help. What is wrong with this approach let x = 120312; function findLength(num){ let length = 0; if (x == 0) length = 1; while(x !=0){ x=Math.floor(x/10); length++; } }
is this correct?? var no = 1231234; function nolen(no) { var count = 0; while(no != 0){ no = parseInt(no/10); count++; } // return count; console.log(count); } // alert(nolen(no)); nolen(no);
Hi, Is it right for your first question? // Find min and max no. let arryVal4 = [1,2,3,4,5]; (function minMax(arry) { let lng = arry.length; let copyary = [...arryVal4]; for(let i = 0; i< lng; i++){ copyary.push(removeAndTotal(i)); } let min = Math.min(...copyary); let max = Math.max(...copyary); console.log("min:",min," min:", max); })(arryVal4); function removeAndTotal(index){ let copyary = [...arryVal4]; copyary.splice(index,1); let total = copyary.reduce((ac,val)=> ac+val,0); console.log(total, copyary); return total; }
var x = 143531; var incrementer = 1; while(x>=10){ x = x/10; incrementer++; } if(!x){ incrementer = 0 } console.log(incrementer); Why is it so complicated....
For 2nd problem can we use the below logic?, any suggestions ? this is more easy i guess without using any built-in function var abc=13233 var counter=0 while(abc>=1) { c++ abc=abc/10 } console.log(counter);
His first approach would work had he done it like this: function getLength(n) { if (n < 10) return 1; var c = 0; while (n > 1) { c++; n = n / 10; } return c; } alert(getLength(2345)) ;
something like this I coded gave me the same result. I am wondering am I doing too much overthinking here let arr = [1,2,3,4]; let originalArr = [...arr]; let addedArr = []; for(let i = 0; i
Good one tech! Regarding the 'Get the length of the number quiz' I took another approach using units, tens, hundreds, etc. You can check it out here jsfiddle.net/brewed100/rb4d01xs/25/ . I don't know that I would have properly solved this in the actual mock interview but I would have at least mentioned 'units, tens, hundreds, thousands'. That's a tricky one!
I coundnt find any good content outside to prepare for the javaScript interviews. So I am planning to create my own on udemy. It might take a month or so . Will let you know when its released.
Hi Techsith, I am facing one interview question that Tell me about your project architect and how you start? plase give some suggestion how to give proper answer
That is a good questions. You would explain first about the technology stack that you are using. explain the tooling around it like i18n, unit testing, end to end testing, deployment, devops, also any back-end that is involved. You can explain certain decision you or your team made and why.
Why the second is so complicated solution? It seems like this guy never solve algorithmic tasks. Like reverse integer without convert, or just swap values from var a = 5, var b = 7; with no extra vars. The best logic is. You have mod operator. So, do next. while yoo have value at number just find mod and substract it with deviding by 10 and count the operation. First solution: ----------------------- const findIntLenghtInterate = (num) => { if(num === 0) return 1; let counter = 0; while (num > 0) { num = (num - num % 10) / 10; counter++; } return counter; }; ------------------------ Second: -------------------------- const findNumLength = (num) => { return num === 0 ? 1 : Math.ceil(Math.log10(x)); };
you can divide the number by 10 and make it integer by flooring it. so let's say the number is 109 1) n = floor(109/10) = 10 2) n = floor(10/10) = 1 3) n = floor(1/10) = 0 it took three steps so the length is 3.
Here's my preferred solution to the second question: countDigitsFinal = num => { if (num === 0 || num === 1) { return 1; }; return Math.ceil(Math.log10(num)); }; I recorded a little bit of my progression and thought process here: codepen.io/phoward8020/pen/qJjyer?editors=0012
Oops. Realized moments after posting that this results in an off-by-one error when the input is an actual power of ten. Updated: const countDigitsFinal = num => { if (num === 0 || num === 1) { return 1; } let logVal = Math.log10(num); if (logVal - Math.floor(logVal) === 0 ) { return logVal + 1; } return Math.ceil(logVal); }; codepen.io/phoward8020/pen/oawPGE/
lot of people take that path . And its fine and good enough to do react. Interviews are completely different world . these days, interviewer can ask anything.
let number = 15487; function getNumberLength(numberParameter) { let numberLength = 0; while (numberParameter >= 1) { numberParameter /= 10; numberLength++; } return numberLength; } console.log(getNumberLength(number));
I think this solution more easy let number = 3423234; let count = 0, result = number; while(result !== 0){ result = Math.floor(result / 10); count++; } console.log(`${number} Number length is: ${count}`) //Output: 3423234 Number length is: 7