I have a percentage, it ranges from 50% to 0%.
I need the values to be mirrored, so:
0% now equals 50%
1% = 49%
25% = 25%
48% = 2%
50% = 0%
etc.
Thanks for any help!
You can use j = max_i - i + min_i where the two constants min_i and max_i are the lower and upper limit of the range.
If i is always between 0 and 50 then you can just write j = 50 - i.
It looks like you want to define a function like this:
(x) f(x)
0 50
1 49
2 48
: :
48 2
49 1
50 0
Then the function is simply:
f(x) = 50 - x
More generally, if x is between low and high inclusive, then:
f(x) = (high + low) - x
Other functions of interest
Here are some other common functions:
(x) f(x)___
0 0 |
1 0 3
2 0 ___|
3 1 |
4 1 3 f(x) = x / 3
5 1 ___| where / is integer division
6 2 |
7 2 3
: : ___|
(x) f(x)___
0 0 |
1 1 3
2 2 ___|
3 0 |
4 1 3 f(x) = x % 3
5 2 ___| where % is integer remainder
6 0 |
7 1 3
: : ___|
Both of the above are sometimes combined when indexing a 2-dimensional table:
______4 columns______
/ \
_______________________ (x) row(x) col(x)
| | | | | 0 0 0
| 0 | 1 | 2 | 3 | 1 0 1
|_____|_____|_____|_____| 2 0 2 row(x) = x / 4
| | | | | 3 0 3 col(x) = x % 4
| 4 | 5 | 6 | 7 | 4 1 0
|_____|_____|_____|_____| 5 1 1 x = row(x) * 4 + col(x)
| | | | 6 1 2
| 8 | 9 | ... | 7 1 3
|_____|_____|_____| : : :
If i'm reading that correctly, the only way for the pcntAnimationComplt to go down is if your currImgWidth is decreasing. If that is so, then just do this:
pcntAnimationComplt = 50 - Math.round((parseFloat(currImgWidth / pageWidth) * 100) / 2);
This should go from 0 to 50, as per your requirements.
var min=1;
var max=50;
for(var i=min;i<=max;i++){document.writeln(i + "<br>");}
for(var i=max;i>=min;i--){document.writeln(i + "<br>");}
Related
+---+---+---+
| 1 | 0 | 0 |
+---+---+---+
| 2 | 1 | 0 |
+---+---+---+
| 3 | 2 | 0 |
+---+---+---+
| 4 | 0 | 1 |
+---+---+---+
| 5 | 1 | 1 |
+---+---+---+
| 6 | 2 | 1 |
+---+---+---+
| 7 | 0 | 2 |
+---+---+---+
| 8 | 1 | 2 |
+---+---+---+
| 9 | 2 | 2 |
+---+---+---+
The code I am trying
var loop = 1;
while(loop < 10) {
console.log(loop, loop%3, "I can't calculate this")
loop++;
}
I have a loop increment variable loop and it counting 1,2,3,4,5....
I need to calculate 2 number from incremental variable: one is rounding 0,1,2 (loop % 3) I can do this but other 0,0,0 or 1,1,1
I mean I need to hold a number according to my round number.
Fixed your code ((loop-1)%3) and you just need a division with rounding down:
var loop = 1;
while(loop < 10) {
console.log(loop, (loop-1)%3, Math.floor((loop-1)/3))
loop++;
}
Some other languages support "integer division", where the Math.floor thing would not be necessary.
Use bellow code snippet:
var loop = 1;
var round = 3;
while (loop < 10) {
console.log(loop, (loop - 1) % round, Math.floor((loop - 1) / round));
loop++;
}
So easy
Actually you have an need for getting a number in a different decimal system, here, you want to get a number to base of three with the slight complication of a one based starting value, where the number usually starts at zero and a reversed result by taking the lower indicators at left and the higer indicator at right.
Now you get first the straight conversion.
var i;
for (i = 0; i < 9; i++) {
console.log(i, i.toString(3).padStart(2, '0'));
}
And now the shifted result by adding one to the output and by reversing the result.
var i;
for (i = 0; i < 9; i++) {
console.log(i + 1, Array.from(i.toString(3).padStart(2, '0')).reverse().join(''));
}
console.log(0.5 | 0); // 0
console.log(-1 | 0); // -1
console.log(1 | 0); // 1
Why does 0.5 | 0 return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?
This is a bitwise or.
Since bitwise operations only make sense on integers, 0.5 is truncated.
x | 0 is x, if x is an integer.
Bit comparison is so simple it's almost incomprehensible ;) Check out this "nybble"
8 4 2 1
-------
0 1 1 0 = 6 (4 + 2)
1 0 1 0 = 10 (8 + 2)
=======
1 1 1 0 = 14 (8 + 4 + 2)
Bitwise ORing 6 and 10 will give you 14:
alert(6 | 10); // should show 14
Terribly confusing!
A single pipe is a bit-wise OR.
Performs the OR operation on each pair
of bits. a OR b yields 1 if either a
or b is 1.
JavaScript truncates any non-integer numbers in bitwise operations, so its computed as 0|0, which is 0.
This example will help you.
var testPipe = function(input) {
console.log('input => ' + input);
console.log('single pipe | => ' + (input | 'fallback'));
console.log('double pipe || => ' + (input || 'fallback'));
console.log('-------------------------');
};
testPipe();
testPipe('something');
testPipe(50);
testPipe(0);
testPipe(-1);
testPipe(true);
testPipe(false);
This is a Bitwsie OR (|).
The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded.
So, in our case decimal number is converted to interger 0.5 to 0.
= 0.5 | 0
= 0 | 0
= 0
I have
let f = x => x % 4 === 0 ? 0 : 4 - x % 4
But that's a piece of garbage function. Help.
x is never going to be negative.
Here's a sort of Table of Truth, or something.
x x % 4 4 - (x % 4) f(x)
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
I'm trying to find some correlations here, but it's late and I don't think my brain is working correctly. zzz
What I'm seeing in the f(x) column is a sort of reverse modulus, whereby the outputs cycle from 032103210... instead of 01230123...
I'm sensing some use of Math.max or Math.min in combination with Math.abs might help … There's probably an x * -1 in there somewhere too …
Can you help me write f such that it doesn't suck so badly ?
Moving Redu's use of bitwise operators a bit ahead:
f(x) = -x & 3
Table of Truths™
x x -x 3 -x&3 -x&3
- ---- ----- ---- ---- ----
0 0000 0000 0011 0000 0
1 0001 -0001 0011 0011 3
2 0010 -0010 0011 0010 2
3 0011 -0011 0011 0001 1
4 0100 -0100 0011 0000 0
5 0101 -0101 0011 0011 3
6 0110 -0110 0011 0010 2
7 0111 -0111 0011 0001 1
8 1000 -1000 0011 0000 0
9 1001 -1001 0011 0011 3
var i,
f = x => -x & 3;
for (i = 0; i < 20; i++) {
console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Original solution with negative value and a negative offset of 3 then modulo and add the same offset again.
f(x) = (-x - 3) % 4 + 3
var i,
f = x => (-x - 3) % 4 + 3;
for (i = 0; i < 20; i++) {
console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Something like this will definitely do:
(4 - (x % 4)) % 4
Here's some more truth:
x x % 4 4 - (x % 4) (4 - (x % 4)) % 4
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
I thought you don't want to use modulo. Then here is your code.
var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)
x x % 4 4 - (x % 4) f(x)
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
Explanation: I just had to recall the back old days of truth tables which helped me to solve out this problem. So now we have inputs and output. Since we work in modulo 4 we are interested only in the last two bits.
Input Output
0 : 0 0 0 0
1 : 0 1 1 1
2 : 1 0 1 0
3 : 1 1 0 1
So if we look, the output 2^1 digit is XOR of input 2^0 and 2^1 hence 2 * (x & 2 ? ~x & 1 : x & 1) and the output 2^0 digit is in fact input 2^0 digit. Which is (x & 1) hence... var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)
Note: (foo XOR bar = foo ? !bar : bar)
u v
y z z w
x x & 2 ? ~x y & 1 x & 1 2 * z w + v f(x)
-- ------ ------ --- ------- ------ ------ ----- ----- ----
0 0000 0000 F -0001 0001 0000 0 0 0
1 0001 0000 F -0010 0000 0001 2 3 3
2 0010 0010 T -0011 0001 0000 2 2 2
3 0011 0010 T -0100 0000 0001 0 1 1
4 0100 0000 F -0101 0001 0000 0 0 0
5 0101 0000 F -0110 0000 0001 2 3 3
7 0110 0010 T -0111 0001 0000 2 2 2
8 0111 0010 T -1000 0000 0001 0 1 1
9 1000 0000 F -1001 0001 0000 0 0 0
Five friends are drinking magic cola in an line. When the first friend drinks the cola he disappears, and multiplies into two copies! After that, those new copies go to the end of the line and the next friend drinks the magic cola, repeating the proccess.
For example, imagine we have the following friends:
[Sheldon, Leonard, Penny, Rajesh, Howard]
After Sheldon drinking the first cola, the line will look like this:
[Leonard, Penny, Rajesh, Howard, Sheldon, Sheldon]
After Leonard drinking the cola, the line gets like this:
[Penny, Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard]
And so on...
My objective is to write a function in JavaScript, that given an array with the names of the people in the line, and a number N, it will return the the name of the N-ith person drinking the magic cola.
So, for example, doing console.log(whoIsNext([Sheldon, Leonard, Penny, Rajesh, Howard], 1)) should return Sheldon.
To achieve this, I made this code:
function whoIsNext(names, r){
var fistInLine;
if(r <= names.length){
return names[r-1];
}else{
while(r > names.length){
fistInLine = names.shift();
names.push(fistInLine, fistInLine);
}
return names[r-1];
}
}
This function works well for the following case:
names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"];
Test.assertEquals(whoIsNext(names, 1), "Sheldon");
But it fails for the test:
names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"];
Test.assertEquals(whoIsNext(names, 52), "Penny");
And if I try with a really big number, like:
names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"];
Test.assertEquals(whoIsNext(names, 7230702951), "Leonard");
It doesn't even stop running (takes forever).
So obviously, my solution is not only incorrect, it seems to be inneficient as well. How can I fix it?
A zero based recursive proposal which returns the index of the array, here with length base = 5.
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
number 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
index 0 1 2 3 4 0 0 1 1 2 2 3 3 4 4 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 0 0 0 0 0
It become visible, the pattern is based on 5 and goes up for every round by factor 2.
5 -> 10- > 20 -> 40
Example of calculation
Array after step
0 1 2 3 4 5 6 7 8 9
0: 0 Sheldon |
1: 1 Leonard | |
2: 2 Penny | | |
3: 3 Rajesh | | | |
4: 4 Howard | | | | |
5: 0 Sheldon | | | | |
6: 0 Sheldon | | | | | |
7: 1 Leonard | | | | | |
8: 1 Leonard | | | | | | |
9: 2 Penny | | | | | |
10: 2 Penny | | | | | |
11: 3 Rajesh | | | | |
12: 3 Rajesh | | | | |
13: 4 Howard | | | |
14: 4 Howard | | | |
15: 0 Sheldon | | |
16: 0 Sheldon | | |
17: 0 Sheldon | |
18: 0 Sheldon | |
19: 1 Leonard |
20: 1 Leonard |
21: 1 Leonard
22: 1 Leonard
var friends = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard'],
base = friends.length;
function getIndex(n, i) {
i = i || base;
if (n < i) {
return Math.floor(n * base / i);
}
return getIndex(n - i, 2 * i);
}
var i = 0, index;
document.write(friends[getIndex(1 - 1)] + '<br>'); // "Sheldon"
document.write(friends[getIndex(52 - 1)] + '<br>'); // "Penny"
document.write(friends[getIndex(7230702951 - 1)] + '<hr>'); // "Leonard"
for (i = 0; i < 200; i++) {
index = getIndex(i);
document.write(i + ': ' + index + ' ' + friends[index] + '<br>');
}
Okay, here we go, this is a really simplistic approach and I'll come up with a better one (it's half way through my mind, I'll just have to fit it together)
function whoIsNext(names, index, multiplyFactor)
{
var count = names.length;
var fullLoops = 0;
var currIndex = 0;
while(currIndex <= index)
{
for(var i = 0; i < count; i++)
{
currIndex += Math.pow(multiplyFactor, fullLoops);
if(currIndex > index)
{
return names[i];
}
}
fullLoops++;
}
}
The idea is that the amount the same person comes is doubled each time the people do a full loop (countPerson = Math.pow(2, countFullLoops)). If you then accumulate the amount of people before the set index until you reach the index, you then get the right person (I feel like I just explained a really easy thing really hard).
Also you can substitute any input (change the amount of people on start, change the multiplication factor (did someone say quadruple coke?)) as you want.
x <<= y (x = x << y)
x >>= y (x = x >> y)
x >>>= y (x = x >>> y)
x &= y (x = x & y)
x ^= y (x = x ^ y)
x |= y (x = x | y)
What do these different operators do?
<<, >>
Bit shift left and right, respectively. If you imagine the left operand as a binary sequence of bits, you are shifting those to the left or right by the number of bits indicated by the right operand.
&, ^, |
These are bitwise and, xor, and or, respectively. You can think of & and | as the counterparts to && and ||, except that they will treat their operands as bit vectors, and perform the logical operations on each of the bits. There is no ^^ operator, but this operation is "xor" or "exclusive or". You can think of "a xor b" as "a or b, but not both".
Here is an attempt to make things simple for the very beginner.
Prerequisites
You have to be familiar with the binary number system (numbers made of two digits). If you are not then check this link first: https://www.mathsisfun.com/binary-number-system.html. Just in case the previous link breaks, this answer may help a little: https://stackoverflow.com/a/32155850/1636522.
Indeed, in order to figure out how these operators work, you need to know which bit sequence is behind the numbers involved in the operation. After that you should be able to understand the following stuffs.
Reminder
Decimal digits and their binary notations:
0 0 | 5 101
1 1 | 6 110
2 10 | 7 111
3 11 | 8 1000
4 100 | 9 1001
What do >>>, >> and << do?
These operators shift a bit sequence to the left or to the right.
decimal | binary decimal | binary
---------|--------- ---------|---------
9 | 1001 2 | 10
>> 2 | >> 2 << 2 | << 2
= 2 | = 10 = 8 | = 1000
What do &, | and ^ do?
These operators combine the bits of two numbers to create a new number.
decimal | binary decimal | binary decimal | binary
---------|-------- ---------|-------- ---------|--------
5 | 101 5 | 101 5 | 101
& 6 | & 110 | 6 | | 110 ^ 6 | ^ 110
= 4 | = 100 = 7 | = 111 = 3 | = 011
How does & work?
For each pair of bits: If at least one of the two bits is 0, the resulting bit is 0. If none of the two bits is 0, the resulting bit is 1.
101 bit 3 | bit 2 | bit 1
& 110 -------|-------|-------
= 100 1 | 0 | 1
& | & | &
1 | 1 | 0
= | = | =
1 | 0 | 0
How does | work?
For each pair of bits: If at least one of the two bits is 1, the resulting bit is 1. If none of the two bits is 1, the resulting bit is 0.
101 bit 3 | bit 2 | bit 1
| 110 -------|-------|-------
= 111 1 | 0 | 1
| | | | |
1 | 1 | 0
= | = | =
1 | 1 | 1
How does ^ work?
For each pair of bits: If the two bits are different, the resulting bit is 1. If the two bits are the same, the resulting bit is 0.
101 bit 3 | bit 2 | bit 1
^ 110 -------|-------|-------
= 011 1 | 0 | 1
^ | ^ | ^
1 | 1 | 0
= | = | =
0 | 1 | 1
Bitwise Operators