I have a javascript that checks if a certain 4 number code is put in :
verify = function (num, success) {
if (true && (num & Math.pow(2, 0)) == 0 && (num & Math.pow(2, 1)) != 0 && (num & Math.pow(2, 2)) == 0 && (num & Math.pow(2, 3)) != 0 && (num & Math.pow(2, 4)) == 0 && (num & Math.pow(2, 5)) == 0 && (num & Math.pow(2, 6)) == 0 && (num & Math.pow(2, 7)) != 0 && (num & Math.pow(2, 8)) == 0 && (num & Math.pow(2, 9)) == 0 && (num & Math.pow(2, 10)) != 0 && (num & Math.pow(2, 11)) != 0 && (num & Math.pow(2, 12)) != 0 && (num & Math.pow(2, 13)) == 0 && (num & Math.pow(2, 14)) == 0 && (num & Math.pow(2, 15)) == 0) {
setTimeout(success, 1000);
} else {
location.reload();
}
}
How do i reverse this to find out what number it wants ?
No worries, its for a hacking game at my work :)
Just do the math. The code is an extremely ugly way to create a bitmask:
0b0001110010001010 is the target number, which is 7306.
EDIT: Then again, the code would accept 0b10001110010001010 too, which is 72842. Any number with the specified lower 15 bits is acceptable to this algorithm.
Just take all the mathematical operations and reverse them, taking care to keep the order of operations correct.
Note though that in some cases there are trapdoor algorithms that have multiple potential inputs for one output. In that case it is actually impossible to reverse them, although you can still find a potential solution.
Related
i got the next exercise in Javascript:
Receive whole numbers from the user, until he enters a number that % 7 with no remainder.
For each number received, state whether it is positive, negative or 0.
when inserting a number % by 7 with no remainder, end the program.
here is my code:
let num = +prompt("give us a number")
while (num % 7 != 0) {
if (num > 0){
document.write("positive ")
num = +prompt("give us a number" )
} else if (num < 0) {
document.write("negative ")
num = +prompt("give us a number" )
} else if (num === 0) {
document.write("zero ")
num = +prompt("give us a number" )
}
The thing is, when the user enter 0 ,obviously i wont get output "zero", cause 0 % 7 is 0 remainder, so it dosent even get into the loop..so how can i output "zero" when the user enter 0?
let num = +prompt("give us a number")
while (num % 7 != 0) {
if (num > 0){
document.write("positive ")
if(num % 7 == 0) {
break;
} else {
num = +prompt("give us a number")
}
} else if (num < 0) {
document.write("negative ")
if(num % 7 == 0) {
break;
} else {
num = +prompt("give us a number")
}
} else if (num === 0) {
document.write("zero ")
if(num % 7 == 0) {
break;
} else {
num = +prompt("give us a number")
}
}
}
Either bring out the else if (num == 0) part before while loop
since 0 % 7 == 0 is true it should satisfy the excersise even if it's not in the loop.
if (num == 0) {
// do stuff
}
while (/* condition */) {
// do other stuff
}
Or add num == 0 constraint in while condition.
while (num == 0 || num % 7 != 0) {
// do stuff
}
The former will not get into the loop, the latter will cause the loop to exit on num == 0 because both implies that it's a correct modulo division.
You can do both if you want to optimize memory usage (obviously it wouldn't be a problem in this scale).
This may be one possible way to achieve the desired objective.
Code Sample
This sample uses alert to pop-up the result, instead of document.write.
let num = 1;
while (!num || num % 7) {
num = +prompt('enter a number');
if (num && num % 7) {
alert(
`${num} is ${num > 0
? 'positive'
: 'negative'
}`
);
} else if (!num) alert('num is zero');
};
Explanation
Set an initial value of num to 1
Set a while loop which will execute as long as num is not 0 (falsy, technically) and num % 7 is not 0.
Get the user-input using prompt into the variable num
If num is truthy (ie, non-zero) and num % 7 is truthy (ie, non-zero), pop an alert whether num is positive (if > 0) or negative (otherwise). This is achieved by using ` backtick symbol / template literal and ?: ternary-operator.
Else, if num is zero, then pop-up corresponding alert
Code Snippet
let num = 1;
while (!num || num % 7) {
num = +prompt('enter a number');
if (num && num % 7) {
alert(
`${num} is ${num > 0
? 'positive'
: 'negative'
}`
);
} else if (!num) alert('num is zero');
};
it turned out, this this the simplest answer:
let num = +prompt("give us a number")
while (**num == 0| num % 7 != 0**) {
if (num > 0){
document.write("positive ")
num = +prompt("give us a number" )
} else if (num < 0) {
document.write("negative ")
num = +prompt("give us a number" )
} else if (num === 0) {
document.write("zero ")
num = +prompt("give us a number" )
}
}
I got stuck on this exercise. I don't understand why they need to have the first condition. Can I ignore that part?
The exercise is from https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-46.php:
Write a JavaScript program to check two given non-negative integers that whether one of the number (not both) is multiple of 7 or 11.
function valCheck(a, b) {
if (!((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0))) {
return ((a % 7 == 0 || a % 11 == 0) || (b % 7 == 0 || b % 11 == 0));
} else
return false;
}
console.log(valCheck(14, 21));
console.log(valCheck(14, 20));
console.log(valCheck(16, 20));
TL;DR – No, it's not redundant!
Write a JavaScript program to check whether exactly one of the two given non-negative integer numbers is a multiple of 7 or 11.
To explain why it's not redundant, let's break this solution function down.
function valCheck(a, b) {
if (!((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0))) {
return ((a % 7 == 0 || a % 11 == 0) || (b % 7 == 0 || b % 11 == 0));
} else
return false;
}
First Conditional
The first condition is !((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0)). The x % n == 0 expressions are checking whether the given x is divisible by (a multiple of) either 7 or 11, but the expression is written in an overly verbose way that makes it a little difficult to understand.
Using De Morgan's law, we can rewrite it:
!((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0))
⇔
!(a % 7 == 0 || a % 11 == 0) || !(b % 7 == 0 || b % 11 == 0)
⇔
(a % 7 != 0 && a % 11 != 0) || (b % 7 != 0 && b % 11 != 0)
Now, we can tell that this condition is checking for the case where either a or b isn't divisible by 7 nor 11.
When this condition is true, we return the result of a second condition. If it is false, then we know neither input is a multiple of 7 or 11, so we return false outright.
Second Conditional
The second condition is:
(a % 7 == 0 || a % 11 == 0) || (b % 7 == 0 || b % 11 == 0)
This one is much less opaque than the first, and checks whether any of the 4 sub-conditions are true (the parenthesis are irrelevant here, since they're all "or" || operators).
Since we already know from the first condition that at least one of the inputs isn't a multiple of 7 or 11, we can now simply return whether at least of the inputs is. Put another way, if we know that this second condition is true, then we can be certain that exactly one of the numbers is a multiple of 7 or 11.
Are the Conditions Redundant?
So to answer your question, no, the first conditional isn't redundant, since it checks for whether at least one of the inputs isn't a multiple or 7 or 11. We need both of the conditional expressions to properly check that exactly one of the inputs is a proper multiple or 7 or 11.
If the "but not both" clause wasn't there, then we could use just the second conditional expression, but since it is, we need both pieces of information.
Wanted to note, I lied a little in saying we need both expressions, since it is indeed possible to solve this exercise with a single expression!
function valCheck(a, b) {
return (a % 7 == 0 || a % 11 == 0) != (b % 7 == 0 || b % 11 == 0);
}
console.log(valCheck(14, 21)); // false
console.log(valCheck(14, 20)); // true
console.log(valCheck(16, 20)); // false
I'll leave it to the reader to figure out why this works!
There are many repeating functions, let's refactor for clarity:
function isMultipleOf7Or11(num) {
return num % 7 == 0 || num % 11 == 0;
}
function valCheck(a, b) {
var isAVarMultiple = isMultipleOf7Or11(a);
var isBVarMultiple = isMultipleOf7Or11(b);
if(!(isAVarMultiple && isBVarMultiple) {
return isAVarMultiple || isBVarMultiple;
else
return false;
}
}
Now to simplify the condition:
!(isAVarMultiple && isBVarMultiple) is also equal to !isAVarMultiple || !isBVarMultiple according to De Morgan's negation of a conjuction, which returns true if at least one of them is not a multiple of 7 or 11
and in the return value the condition isAVarMultiple || isBVarMultiple returns true if at least one of them is a multiple of 7 or 11
We can conclude that it is a XOR operation on two values, which would return true only if one and only one of the variables is a multiple of 7 or 11
Outputs:
console.log(valCheck(14, 21));
returns false
console.log(valCheck(14, 20));
returns true
console.log(valCheck(16, 20));
returns false
I recommend you look at the code by the debugger, not for this example only, but when you will be stuck in difficult situations, you can split your code in a small and readable pieces and see what is happening in there.
For example this can be transformed to this
Not Sure If I can make this more readable :)
function valCheck (a, b) {
debugger;
const canADividedTo7 = a % 7 == 0;
const canADividedTo11 = a % 11 == 0;
const canADividedTo7Or11 = canADividedTo7 || canADividedTo11;
const canBDividedTo7 = b % 7 == 0;
const canBDividedTo11 = b % 11 == 0;
const canBDividedTo7Or11 = canBDividedTo7 || canBDividedTo11;
// true when both both side of expression are true
const canAAndBDividedTo7Or11 = canADividedTo7Or11 && canBDividedTo7Or11;
// true when at least one of the expression is true
const canAOrBDividedTo7Or11 = canADividedTo7Or11 || canBDividedTo7Or11;
//
if (!canAAndBDividedTo7Or11) {
return canAOrBDividedTo7Or11;
} else {
return false;
}
}
console.log(valCheck(14, 21));
console.log(valCheck(14, 20));
console.log(valCheck(16, 20));
Who would write anything that inane and complicated?
Let's take this mess:
function valCheck(a, b) {
if (!((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0))) {
return ((a % 7 == 0 || a % 11 == 0) || (b % 7 == 0 || b % 11 == 0));
} else
return false;
}
and refactor it into something readable:
function valChec( a, b ) {
const aIsDivisible = a % 7 === 0 || a % 11 === 0 ;
const bIsDivisible = b % 7 === 0 || b % 11 === 0 ;
return aIsDivisible ? !bIsDivisible : bIsDivisible;
}
You could even make it a one-liner with a ternary expression (at the expense of a few more division ops... but given the original code, that's not an issue):
const isMagic = x => x % 7 === 0 || x % 11 === 0;
const valCheck = (a,b) => isMagic(a) ? !isMagic(b) : isMagic(b);
Yes, you're right in finding the answer to that question redundant; also, I believe the ! is incorrect and makes the code always return false.
A more reasonable solution to the problem would be to just return the boolean expression:
function valCheck(a, b) {
return ((a % 7 == 0 || a % 11 == 0) || (b % 7 == 0 || b % 11 == 0));
}
Alternatively, if you really wanted to use an if/else construct, it would be better to just return true in the first case:
function valCheck(a, b) {
if ((a % 7 == 0 || a % 11 == 0) && (b % 7 == 0 || b % 11 == 0))
return true;
else
return false;
}
Hope this helps with your understanding of JavaScript! But as suggested in the comments, you might want to learn JavaScript from other sources.
I know there are easier and quicker ways to write this program. However, I'm having trouble understanding why the equal-to operator is needed here? Referring to the == 0 instances below.
for(let x=1;x<101;x++) {
if(x % 3 == 0 && x % 5 == 0){
console.log('fizzbuzz')
} else if(x % 3 == 0) {
console.log('fizz')
} else if(x % 5 == 0) {
console.log('buzz')
} else {
console.log(x)
}
}
x % 3 == 0 is checking to see if x is evenly divisible by three. If it isn't, then there will be a non-zero remainder. (The x % 3 part of that expression uses the % operator to get the remainder after division.)
I just figured out how to test for certain conditions and modify output within a loop. But I noticed that testing for two conditionals with the && operator only works in an if/else if/else if/else chain if it's the first one tested for.
Can someone explain why this works:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
if (number % 3 == 0 && number % 5 == 0)
console.log ("FizzBuzz");
else if (number % 3 == 0)
console.log("Fizz");
else if (number % 5 == 0)
console.log("Buzz");
else
console.log(number);
}
But this does not?:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
if (number % 3 == 0)
console.log("Fizz");
else if (number % 5 == 0)
console.log("Buzz");
else if (number % 3 == 0 && number % 5 == 0)
console.log ("FizzBuzz");
else
console.log(number);
}
An else if, as the name suggests, will only execute when a previous if fails. So the statement else if (number % 3 == 0 && number % 5 == 0) will execute only when if (number % 3 == 0) and else if (number % 5 == 0) fail. If a number is a multiple of 3 and 5 both, then the first if gets successfully executed, and the rest ifs and else-ifs are ignored.
However, in code 1, the ordering of ifs and else-ifs is such that, if a number is divisible by both 3 & 5, then first if is executed, if it is divisible by the only 3, then first if is not executed, only else if (number % 3 == 0) is executed.
Let's make an example using the numbers 6, 10, 15.
The number 6 will execute - in your first example (the working example) - the second if block because in the first one the condition will not be satisfied while the third and fourth block will be ignored, and - in your second example (the not-working example) - will execute the first if block and ignore the other blocks that follow.
The number 10 will execute - in your first example - the third block because the first's and second's condition is not satisfied while the fourth block will be ignored, and - in your second example - will execute the second block, because the condition in the first block is not satisfied, while the blocks that follow will be ignored.
The number 15 will execute - in your first example - the first block and ignore the blocks that follow, and - in your second example - will also execute the first block because the condition is satisfied while the blocks that follow will be ignored.
So, to recap, in your second example, the third if block will never be executed because the condition for its execution is made up of an and of the first and second if block's conditions. In order for the third block to be executed you would need a case where the first if block's condition (let's say c1) and the second if block's condition (let's say c2) are false and c1 && c2 is true, but in order to have c1 && c2 to true you need c1 and c2 to be true, which leads to the execution of the first block and skipping of the rest.
You want to test for if the the number is divisible by three and five, but before you do that you test if it is just divisible by three.
It is, so it follows that branch of logic and never attempts to test if it is divisible by three and five.
Because in your test if the number is a multiple of 3 or 5 then the corresponding if statemetn will get executed before the number % 3 == 0 && number % 5 == 0 statement is reached so it will never get executed.
Let us assume the number is 33, the the first test will become success which is correct, but if the number if 15 then again the first if is success because 15 is a multiple of 3 so even though it is a multiple of 5 also the 3rd condition will not get a chance to execute
To get it correct you may need something like below, where if the number is a multiple of both the versions we skip first 2 conditions
var number = 0;
var counter = 0;
while (counter < 100) {
number++;
counter++;
if (number % 3 == 0 && number % 5 != 0) {
console.log("Fizz");
} else if (number % 5 == 0 && number % 3 != 0) {
console.log("Buzz");
} else if (number % 3 == 0 && number % 5 == 0) {
console.log("FizzBuzz");
} else {
console.log(number);
}
}
Everything that is either evenly divisible by 3 or evenly divisible by 5 has been removed in the second version. By the time it checks to see if a number is divisible by 3 and divisible by 5 there is no chance of it being true because one of the first two clauses already evaluated to be true.
Consider this pseudo code
if( A || B ) return;
if( A && B ) //this code will never execute
and then consider A to be number % 3 == 0 and B to be number % 5 == 0. This is essentially what is happening, and why the last if statement never executes.
What you actually want to test is
if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 != 0) …
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
if you'd write out the four cases.
Only you don't need to be that explicit, because when the previous conditions already did not match (and you are in the else branch), then those != 0 are implied and you can omit them. However, order matters, as the conditions are tested consecutively.
So if you have the fully qualified conditions, you can shuffle their order as you want:
if (number % 3 == 0 && number % 5 != 0) … // move to front
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
and then continue to simplify conditions, omitting the parts that are already implied by their parent cases:
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 != 0 && number % 5 == 0) // (an == instead of the && would suffice)
console.log("Buzz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
Other permutations of the condition let us use as few as in your original example, like
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else if (number % 5 == 0) // as it didn't match the first condition, we know that % 3 != 0
console.log("Buzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
And the minimum number of tests would be achievable by nesting them:
if (number % 3 == 0)
if (number % 5 == 0)
console.log("FizzBuzz");
else
console.log("Fizz");
else
if (number % 5 == 0)
console.log("Buzz");
else
console.log(numer);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have seen this question earlier, but i did not understand the answer.
function (num, success) {
if (true && (num & Math.pow(2, 0)) != 0 && (num & Math.pow(2, 1)) != 0 && (num & Math.pow(2, 2)) == 0 && (num & Math.pow(2, 3)) != 0 && (num & Math.pow(2, 4)) != 0 && (num & Math.pow(2, 5)) == 0 && (num & Math.pow(2, 6)) == 0 && (num & Math.pow(2, 7)) != 0 && (num & Math.pow(2, 8)) == 0 && (num & Math.pow(2, 9)) != 0 && (num & Math.pow(2, 10)) != 0 && (num & Math.pow(2, 11)) == 0 && (num & Math.pow(2, 12)) == 0 && (num & Math.pow(2, 13)) != 0 && (num & Math.pow(2, 14)) == 0 && (num & Math.pow(2, 15)) == 0)
Can anyone explain to me how to get the binary value out of this code?
So I can proceed to turn it into a 4-digit number.
That's just a code snippet, not a full function, as you're not returning anything and this is just a conditional in the if statement, however, the spirit of the question is how can you get a binary number from that conditional. So...
First you need to understand what the Math.pow function does.Math.pow(2,0) will yield the number 1, because that is 2 to the power of 0, or 2^0. Math.pow(2,1) is 2, Math.pow(2,2) is 4 and so on.
Ok, so how do you know a binary number? Binary 101 is the number 5, because it is 1*(2^2) + 0*(2^1) + 1*(2^0). So look at the conditional:
num & Math.pow(2,0) will be either one or zero. So you compare it with == and != to find out, and use the AND operator (&&) to make sure it's comparing the true/false results. In the example above of 101, you'd need true as a result of the following conditional:
(num & Math.pow(2, 2)) != 0 ---> meaning 1
&&
(num & Math.pow(2, 1)) == 0 ---> meaning 0
&&
(num & Math.pow(2, 0)) != 0 ---> meaning 1