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
Related
function checker (num){
for(i = 1; i <= num; ++i){
if(i % 3 === 0 ||
i === 13 ||
i === 23 ||
i === 33 ||
i === 43 ||
i === 53 ||
i === 63 ||
i === 73 ||
i === 83 ||
i === 93){
console.log('boom')
}
else{
console.log(i)
}
}
}
checker(100)
so i made this , but thats looks ugly , how to do it correct ? like any number that contains 3 replace it with some text. thank you.
You could check the string, too.
if (i % 3 === 0 || i.toString().includes('3')) ...
If your loop only counts to 100, you only need to check the tens and ones place to determine if it's a 3. You can use the JavaScript modulo operator % and the Math.floor() static method:
if (Math.floor(i / 10) === 3 || i % 10 === 3 || i % 3 === 0) // ...
The Math.floor method will return true if dividing the number by 10 and rounding down returns 3 (true for the numbers 30 to 39 inclusive), and the % modulo operator will return true when dividing i by 10 has a remainder of 3 (true for 3, 13, 23, 33, etc.) i % 3 === 0 is from your original function.
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
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));
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.
I am newbie here in more way than one, so please go easy on me :)
Here is a problem I was tasked with solving using javascript:
Print out the numbers from 1 - 20.
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.
Here was my first attempt at approaching it:
var numberArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for(var i = 0; i < numberArray.length; i++){
if(i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}
else if(i % 3 === 0 && i % 5 !== 0){
console.log("Fizz");
}
else if(i % 3 !== 0 && i % 5 === 0){
console.log("Buzz");
}
else {
console.log(numberArray[i]);
}
}
This returned the following incorrect values:
FizzBuzz
2
3
Fizz
5
Buzz
Fizz
8
9
Fizz
Buzz
12
Fizz
14
15
FizzBuzz
17
18
Fizz
20
I then took a different approach which DID result in the correct answer:
var fizBuzz = function() {
for (i = 1; i < 21; i++) {
if (i % 3 === 0 && i%5 === 0) {
console.log ("FizzBuzz");
} else if (i % 3 === 0) {
console.log ("Fizz");
} else if (i % 5 === 0) {
console.log ("Buzz");
} else {
console.log (i);
}
}
};
fizBuzz();
Would someone be willing to help me understand what was wrong about the first approach? This is really bothering me :)
The problem is you were checking the divisibility of i (which starts at 0) rather than of numberArray[i] (which starts at 1).