Counting in javascript from math.pow - javascript

I got this little piece of code which is used for a google maps.
To 'link' the markers to a filter there is a count.
Each marker has a props which holds a number so it will be able to filter.
at the minute the math.pow is used.
You understand the props for the markers go from 1-2-4-8-16-32 untill you get over millions and billions. What i'd like to do is alter the code so it does 1+1=2 1+2=3 4, 5 ,6 ,7 and so on so i don't have to fill in these HUGE numbers.
The code is here:
$.each(sizer,function(i,b){
props+=($(b).is(':checked'))?Math.pow(2,i):0;
});

If I understand correctly, what you want is this:
$.each(sizer,function(i,b){
props+=($(b).is(':checked')) ? i : 0;
});
There are two things for you to understand:
the syntax of the ternary operator is as follows: condition ? expr1 : expr2. Basicly it is a one-line if/else statement - if the condition is true - execute the first expression, otherwise execute the second expression. You can read more about it on the MDN docs
Math.pow is a built-in javascript function that accepts two numbers - a number and a power of this number. So Math.pow(2, i) means 2 to the power of i. This is something you probably guessed by now.
It is very hard to help more than that, because I don't have the rest of the code at my disposal. For example, how can I know what are the values of sizer? Etc...

Related

ECMAScript: What does `status` mean in Syntax-Directed Operations?

In https://tc39.es/ecma262/#sec-algorithm-conventions-syntax-directed-operations, There is a description like this
Syntax-directed operations are invoked with a parse node and, optionally, other parameters by using the conventions on steps 1, 3, and 4 in the following algorithm:
Let status be SyntaxDirectedOperation of SomeNonTerminal.
Let someParseNode be the parse of some source text.
Perform SyntaxDirectedOperation of someParseNode.
Perform SyntaxDirectedOperation of someParseNode with argument "value".
What does status in this algorithm mean? It's not used in the algorithm anymore.
What does the algorithm mean? It seems weird.
That's just an example, it's not a real algorithm, and it's not necessarily a complete example (hence not using status after the first step). For a real algorithm, look at the section containing actual Syntax-Directed Operations, such as function name inference:
8.4.1 Static Semantics: HasName
...
Let expr be the ParenthesizedExpression that is covered by CoverParenthesizedExpressionAndArrowParameterList.
If IsFunctionDefinition of expr is false, return false.
Return HasName of expr.
The status in the example is like expr in the above, a temporary variable (if you will) for the algorithm. It's just that the example doesn't use status again (it probably should, it would be a better example).
What does status in this algorithm mean?
It's just a variable introduced by that step, like in any other "Let x be …".
What does the algorithm mean? It seems weird.
It means nothing. It's an example algorithm in the "algorithm conventions" section explaining how algorithm steps are meant to be understood in the rest of the spec.

while(i) loop in JavaScript

I ran the below code in JavaScript
let i = 3;
while (i) {
console.log(i--);
}
since the while(i) is not like while(i>0) then I expected the result as 3,2,1,0,-1,-2,...
but the actual result is 3,2,1. Could anyone explain this case to me? I am confused.
The while loop runs until the check condition is false.
In this case, it is the value of i.
Since Javascript is dynamically typed(ie - we don't define the types when defining the variables) the value of i is converted into a boolean from the type it is currently in.
In this case, you are setting numerical values to i. And the number 0 is considered to be falsely. Therefore, breaking the while loop.
You can refer here for a full list of falsely value.
While loops run until its condition is set false. Note that all statements such as while, if and ternaries all handle conditions in the same way. To have a better understanding on how the simplest and quickest way is to test them with ternaries.
I usually run something such as the following on a js console such as chrome (ctrl + j)
1?2:3;
0?2:3;
5?2:3;
"Hello"?2:3;
""?2:3;
And so on. These are conditional statements, the first number is taken as a condition, the second (2) is what will be returned if it were true, and the third (3) is what it will return if it were false. Note that 2 and 3 are just random numbers.
In the example you have shown, i is an integer. For an integer, only 0 is taken as a false.

What does it mean to create a function that "accepts a positive integer"?

I'm in the process of creating a program in Javascript that is telling me to:
"Create a function "sum" that accepts a positive integer, uses a FOR loop to compute the summation of 1 to that parameter, and then has the function return it".
I'm not looking for someone to do the work for me, I would like to be able to complete the assignment on my own, but the wording in the instructions given are sometimes hard to understand. Anyone able to help?
It simply means that you do not have to support the case where a negative number is passed to the function.

AE JavaScript finding Layer Object when none is given

Attempting to create a 3D shape with children that rotates at an accelerating pace from 0 to 3 seconds, I used the following script
if (time < 3)
Math.pow(time, 2)*30;
That gave me the following error
After Effects warning: Object of type Layer found where a Number,
Array, or Property is needed
Expression disabled.
Error occured at line 0.
Comp: 'Main'
Layer: 15 ('Blue')
Property: 'Y Rotation'
Comp name, layer name and property name are all valid. They point to the property I was trying to edit.
However, what puzzles me is that I fixed that by using the following code.
ctime = time;
if (ctime < 3)
Math.pow(ctime, 2)*30;
The code is now working as intended, and I have no idea why.
If the condition is false, the two would be different since there would be no last statement to use.Try: (time < 3)?Math.pow(time, 2)*30:time;
Math.pow(time^2*30); is simply bad code. Math.pow expects two arguments and you only gave it one. Also ^ is a bitwise operator, there is no power raising operator in JavaScript, you need to use Math.pow.

Compact way to add new values to a map of maps?

I've written a tool to count frequency of word pairs in a text, such that for each time word B follows word A, the count of words[A][B] is incremented.
In perl, the hash and hash of hashes gets automatically instantiated the first time you try to access it, which makes the code simple. In Javascript, it seems that you must first create the innards, which makes the code longer.
In Coffeescript, this functions:
class Adder
...
addPair: (word1, word2) ->
#count[word1] = {} if not #count[word1]?
#count[word1][word2] = 0 if not #count[word1][word2]?
++#count[word1][word2]
but it's two extra lines of 'defensive' code. Is there a way to do this more compactly, so I can maintain less code?
(Putting it in a trinary statement doesn't really make it more compact, just fewer characters for the same amount of logic.)
You can reduce the verbosity of those lines by using the or= operator (or ?=, but it's not necessary in these case):
addPair: (word1, word2) ->
#count[word1] or= {}
#count[word1][word2] or= 0
#count[word1][word2] += 1
You can also golf those two initialization lines into one (though i think this results in less readable code):
addPair: (word1, word2) ->
(#count[word1] or= {})[word2] or= 0
#count[word1][word2] += 1
By the way, supporting this kind of automatic object creation is known as autovivification, and has been discussed on the CoffeeScript issues. Also, Coco, a CS-derived language, has it :)

Categories

Resources