Can someone solve this in Javascript?
Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
Example 1:
Input: num = 14
Output: 6
Explanation:
Step 1) 14 is even; divide by 2 and obtain 7.
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3.
Step 4) 3 is odd; subtract 1 and obtain 2.
Step 5) 2 is even; divide by 2 and obtain 1.
Step 6) 1 is odd; subtract 1 and obtain 0.
Example 2:
Input: num = 8
Output: 4
Explanation:
Step 1) 8 is even; divide by 2 and obtain 4.
Step 2) 4 is even; divide by 2 and obtain 2.
Step 3) 2 is even; divide by 2 and obtain 1.
Step 4) 1 is odd; subtract 1 and obtain 0.
Example 3:
Input: num = 123
Output: 12
Also is this an example of a recursion question?
var numberOfSteps = function(num) {
};
///What I have tried
var numberOfSteps = function(num) {
if(Math.sign(num) === -1){
console.log('Number has to be positive')
}
else if(num % 2 === 0){
num = num/2
if(num % 2 !== 0){
num = num - 1
if(num !== 0){
num = num / 2
if(num % 2 !== 0){
num = num - 1
if(num % 2 === 0)
}
}
}
}
};
console.log(numberOfSteps(14))
You can simply simulate the process by continuously halving and decrementing the number (as appropriate) until it reaches 0 while counting the number of iterations.
var numberOfSteps = function(num) {
let steps = 0;
while(num > 0){
if(num % 2 === 0) num /= 2;
else --num;
++steps;
}
return steps;
};
console.log(numberOfSteps(123));
Related
Could anyone explain to me the 2nd and 3rd statement in the for loop
What is the 1st and 2nd i in the middle part and the last one (i = i + 6)
function prime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false
for (let i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true
}
console.log(prime(11))
console.log(prime(25))
console.log(prime(29))
There are a couple of hacks in this function to determine primality of a number n.
Every non-prime number has at-least 1 factor less than or equal to its square root.
Every 6th number from 3 onwards is a multiple of 3. So, if we have already checked that 3 does not divide n, we can safely say numbers like 9,15,21... will not divide n.
The if inside the loop checks for divisibility of n by i or i + 2. Since we know, 3 | i+4 (-- from 2 above), we don't check divisibility there as it is not required. And since this loop runs from i = 5 upto i = sqrt(n), if we find no factors, we can exit the loop and return true for number's primality (-- from 1 above)
This question has 2 test cases, the inputs of 10 & 15.
function threesFives(num){
var sum = 0;
for(let i = 1; num >= i; i++){
if (i % 3 == 0 || i % 5 == 0){
sum += i;
}
}
return sum
}
I don't see why only Test case 2 (input of 15) is passed, but Test Case 1 is not (gives me a sum of 33, which needs to be 22).
You say "Return sum of added values from 1 and n only if that value is not divisible by 3 or 5". So if the number is divisible by 3 or 5, you skip it in the loop. You're doing the opposite right now. Your code passes the first case by pure coincidence
Your code should be somewhat like this
function threesFives(num){
var sum = 0;
for(let i = 1; num >= i; i++){
if (i % 3 == 0 || i % 5 == 0){
continue;
}
sum += i;
}
return sum
}
console.log(threesFives(10))
Since you need to sum only those values that are not divisible by both 3 and 5, the condition in if statement should have been an AND && condition instead of an OR || one.
I was trying to solve this problem on a leet code called Rotate digits.
This is the working solution of the problem in Javascript :
var IsGood = function(n){
let result = false;
while(n > 0){
let digit = n % 10; // 2 % 10
// ignore 0, 1, 8; by themselves, no change
if( (digit === 3) || (digit === 4) || (digit === 7) ) return false;
if( (digit === 2) || (digit === 5) || (digit === 6) || (digit === 9) ) result = true;
n /= 10;
}
return result;
}
var rotatedDigits = function(N) {
let count = 0;
for(let i = 1; i <= N; i++){
if(IsGood(i)) count++;
}
return count;
}
My question inside the IsGood function why are we taking n % 10 and then dividing n /=10. I have seen this implementation in almost all solutions to this problem. Can someone please explain the logic behind this?
Take the number 123
The remainder of that number when divided by 10 is 3 (123 % 10) = 3.
So n%10 is giving you the last digit of the number
If you want to test the next digit in the number you have to remove the 3. The way to do that is to divide by 10 and only take the integer part (12.3 lose the .3 part) which you get by 123 / 10
You can keep doing that until you get to a number less than 10 which must be the final digit! this algorithm is a way to examine each digit in a number using math rather than string manipulation
123 % 10 = 3 first digit
123 / 10 = 12
12 % 10 = 2 next digit
12 / 10 = 1
1 < 10 so final digit
Write a javascript program that displays the numbers from 10 to 100. But for multiples of 4 print "Penny" instead of the number and for multiples of 6 print "Leonard". For numbers which are multiples of both 4 and 6 print "Bazzinga"
I know how to do two parts struggling to print 6 and 4;
function baZzinga (number) {
for (var number = 10; number <= 101; number++)
if(number % 4 == 0) {
console.log("penny");
}
else if (number % 6 == 0) {
console.log("Leonard");
} else if ( not sure what goes here) {
help help help
} else {
console.log(number");
}
You want the and condition first. Try this
var result = document.getElementById("result");
function baZzinga (number) {
for (var number = 10; number <= 101; number++) {
if (number % 4 == 0 && number % 6 == 0) {
result.innerHTML += "Bazinga";
}
else if(number % 4 == 0) {
result.innerHTML += "penny";
}
else if (number % 6 == 0) {
result.innerHTML += "Leonard";
}
else {
result.innerHTML += number;
}
}
}
baZzinga()
<p id="result"></p>
I changed console.log to result.innerHTML because I wanted to demonstrate it in a snippet.
I have a few comments on your code -- constructive criticism, I hope! First, you don't need the number parameter in your bazzinga function. Next, the indentation of the code you posted makes it hard to read. Finally, you should almost always use === instead of ==. The === tests for strict equality, whereas == tries to do some type conversions first (and can therefore produce unexpected results). See the official docs.
To answer you question: check for divisibility by 6 AND 8 first. That way, it will override the individual cases. I believe you want something like this:
function bazzinga() {
for (var number = 10; number <= 100; number++) {
if (number % 4 === 0 && number % 6 === 0) {
console.log("Bazzinga");
} else if (number % 4 === 0) {
console.log("Penny");
} else if (number % 6 === 0) {
console.log("Leonard");
}
}
}
Here is a solution using the format you posted:
for (var number = 10; number <= 100; number++) {
if(number % 4 === 0 && number % 6 === 0){
console.log("bazzinga");
} else if(number % 4 === 0) {
console.log("penny");
} else if (number % 6 === 0) {
console.log("Leonard");
} else {
console.log(number);
}
}
Or use the ternary operator to be even more succinct!
for (var i = 10; i <= 100; i++){
var penny = i % 4 === 0;
var leonard = i % 6 === 0;
console.log(penny ? (leonard ? "bazzinga" : "penny"): leonard ? "leonard" : i);
}
function process_num(num) {
return num % 4 == 0 ? num % 6 == 0 ? "Bazzinga" : "Penny" : num % 6 == 0 ? "Leonard" : num;
}
for (x = 10; x <= 100; x++) { console.log( x + ': is ', process_num(x)) }
Nested Ternary operator for conciseness
If it passes outer ternary test it is divisible by 4:
Enter into nested termary one to test if num is also divisible by 6 for the BaZzinga prize!!
If it fails the BaZzinga challenge, we know it previously passed the divisible by 4 test so print "penny"
Failing the outer ternary condition, we know it's not divisible by 4:
Enter nested ternary two to consider if divisible by 6. If so print "Leonard".
If not it's failed both the outer (div by 4) and inner (div by 6) so return the number unchanged.
Now that the logic is contained in the function, we can just create a for loop to iterate over the required numbers printing out the correct values.
Is there a built-in function in javascript to reverse a number depending on the total number? if no how to implement it?
i.e.
number = 2;
total_number = 10;
__________________
answer = 9;
reverse(2, 10) // returns 9
since:
1 == 10
[2 == 9]
3 == 8
4 == 7
5 == 6
6 == 5
7 == 4
8 == 3
9 == 2
10 == 1
function (num, total) {
return total - num + 1;
}
Shouldn't this work:
total - number + 1
Why not:
Total_number - number + 1