How to find the value of exponent in Javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Is there a Math API in javascript to find the value of n (exponent), eg,
Find the value n such that 2**n=64.
i know there is Math.pow , but it requires base and exponent. In my case i have base and the result, so i dont think it would work.

I think you mean to find log of the value
let a = Math.pow(2, 5);
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}
console.log(a, getBaseLog(2, a));

Try this. Refer to this link .
Math.log2(x) // x=64
The link will also tell you about other related Math functions.
Also remember that for finding log of x to the base y, you can always use the formula ln(x)/ln(y) and for ln you already have an inbuilt function (Refer link above).

Related

Need help extracting string from a text file via regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm trying to extract a certain js object from a text/js file via regex.
The whole file contains a lot of code, but I'm only interested in getting a variable called cfg.
The file could look like this:
function A(){
}
var cfg = {
setting:1,
setting2: 5,
setting3:[],
setting4:{
setting5:"test"
}
};
function B(){
}
Could anyone help me to figure out the regex for this ? I'm terrible at regex and I've tried plenty of available solutions but some of them don't seem to be valid in C#.
Right now my solution is using:
string[] split = input.Split(new string[] { "var cfg = " }, StringSplitOptions.None);
if(split.Length > 2)
{
string[] split2 = split[1].Split(new string[] { ";" }, StringSplitOptions.None);
return JObject.Parse(split2[0]);
}
Which obviously doesn't cover different writings of the cfg object. Like var cfg={};
Thanks in advance!
I would recoment you use a JS parser such as Jint if your code is more complex or will change. But for your example this should work:
var r = new Regex(#"var cfg = (?<Content>\{[^;]*});");
var content = r.Match(text).Groups["Content"];
Note: If the cfg literal contains ; the regex will not work as expected.

Don't understand: "sort((a: Article, b: Article) => b.votes - a.votes);" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am not understanding the return statement in sortedArticles(). Can somone point me to a link, or explain how this sort function work?
sortedArticles(): Article[] {
return this.articles.sort((a: Article, b: Article) => b.votes - a.votes);
}
<div class="ui grid posts">
<app-article
*ngFor="let article of sortedArticles()"
[article]="article">
</app-article>
</div>
May be a trivial question, but for some reason could not find the answer online. Appreciate the help.
Okay, let's break that down for you...
sortedArticles(): Article[] {
return this.articles.sort((a: Article, b: Article) => b.votes - a.votes);
}
(a: Article, b: Article) => b.votes - a.votes - This is equivalent to
function(a, b) { return b.votes - a.votes }, and will return the proper values in order for sort() to be able to re-order the array elements. If the first article (a) has more votes, it'll return a negative value; if both articles have the same number of votes, it'll return 0; if the second article (b) has more votes, it'll return a positive number.
some_array.sort() takes a function as its parameter. This can be a named function, or an anonymous function like is done here. It'll loop through the array and pass in two elements into the function to determine if they should be swapped or stay in that order. If the function returns a negative value, the first item should go before the second; if the function returns a positive value, the second item should go before the first; if the function returns 0, then they're equally weighted, and their order doesn't matter -- the sort() usually puts them in their original order in this case.
So, calling sortedArticles() will return a list of articles, ordered by how many "votes" they each have.

Is it possible to convert java into java script via some online tool? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
This piece of java code needs to be converted to javascript ?
private void validatePartitionSum() throws ProfileUsageException {
int moSize = maxSizeMO == -1 ? 0 : maxSizeMO;
int mtSize = maxSizeMT == -1 ? 0 : maxSizeMT;
if (maxSize > 0 && moSize + mtSize > maxSize) {
//added to default limit type to Messages when error occurs
if(this.editPartitionLimitFlag == false) {
this.limitType = ThresholdType.MESSAGES;
}
String errorMessage =
getFormattedResourceValue(STORAGE_SUM_LARGER_THAN_TOTAL);
throw new ProfileUsageException(errorMessage,
MMException.INVALID_PARAMETER);
}
}
look into gwt, (not online tool)
quote: "Write browser applications in Java using the Java IDE of your choice"
http://www.gwtproject.org/doc/latest/tutorial/gettingstarted.html
http://www.gwtproject.org/
Have a look at the Google Web Toolkit

Auto-summarising JSON objects [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I want to summarise JSON data objects in a webpage. The only method I can think of is to read it into R, run summarise(), extract the report to json and read that into a webpage and write to DOM. Does anyone know a less idiotic method for getting an equivalent intelligent summary that doesn't involve R?
Is there anything equivalent in Javascript/JQuery for JSON objects?
The closest thing is probably Object.keys and Array#reduce, which visits each array entry (each property name — key — in this case) and calls the callback passing the accumulator for the reduce operation and the value of the entry. So for instance, a sum looks like this:
var obj = {
foo: 42,
bar: 27,
baz: 51
};
var sum = Object.keys(obj).reduce(function(acc, key) {
return acc + obj[key];
}, 0); // <== 0 = seed value for the accumulator
snippet.log(sum);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Other than that, you're into libraries.

Is this eval Evil? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Writing a function in JavaScript. The plan is the function creates an object, which requires boolean statements as parameters. Something like this ->
var foo = new fuzz("pie < squirrel", "monkey === banana");
My question is - Is this evil?
*Note - * Inside the function 'fuzz' I will run checks on the values of the parameters. (Check string.length etc). I think this is how one is meant to use eval, it just has such a bad reputation on t'up web.
Thanks
Summing up the conclusions in the comments: write a simple rule evaluation engine! E.g.:
var variables = { ... };
function niceEval(condition) {
var operands = condition.match(/(\w+)\s+(\S+)\s+(\w+)/);
switch (operands[2]) {
case '<' :
return variables[operands[1]] < variables[operands[3]];
...
}
}
This also gives you a lot more control over possibly occurring errors than blindly evaling a string.

Categories

Resources