How do you multiply negative numbers in javascript? [closed] - javascript

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
This may sound stupid, but if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Please help.

You can use both code in the javascript.
8*-5 == -40
8*(-5) == -40
As multiply symbol is greater precedence than minus symbol, it will be multiplied in normal math and do so in JavaScript.

if you are trying to do something like 8*-5 (eight times negative five), you can't exactly.
Of course you can. Open your JavaScript console and type 8*-5. You get -40.
In order of operations, you could do something like this: 8*(-5). But in javascript (and in most other languages), parentheses are the condition symbol.
Not sure what gave you that idea, but parenthesis can be used just fine in math operations and for setting order of operations. Again, open your JavaScript console, type 8*(-5) and you will see -40.

Related

Finding file name with variables in between [closed]

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 3 months ago.
Improve this question
Having a hard time with Regex.
What would be the regex for finding a file name with variable in between them?
For eg:
File name : DON_2010_JOE_1222022.txt
In the above file name the words DON, JOE and the format .txt will remain constant. Rest numbers might change for every file. There could be characters as well instead of numbers in those two places.
What im looking for is basically something like DON_*_JOE_*.txt with * being whatever it could be.
Can someone please help me with this?
I tried DON_*_JOE_*.txt and obviously it did not work.
DON_(?<firstString>.*)_JOE_(?<secondString>.*).txt
You can use this. To access the specific group, you can use matcher.group("firstString").
In JavaScript:
"DON_2010_JOE_1222022.txt".match(/DON_.+_JOE_.+\.txt/)
whatever it could be
It is .+ except new lines.

Parameters between text [closed]

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 last month.
Improve this question
Why isn't there a way to have functions with the parameters in between text? For example:
function _(param1)plus(param2){
return param1+param2;
}
This is just an idea I had on my mind and I didn't try anything. However, if I did this I'd get an error
I don't really understand the question, but you can do this instead:
function plus(param1, param2){
return param1 + param2
}
Hope this helps!
Because each programming language has its rules (syntax enforced by the parser). And a common rule for almost all languages (the same for JavaScript) is that functions can have parameters, and you specify these parameters in the brackets ().
So you can not continue to build function's name after the brackets, because the JavaScript will assume that everything before the brackets is the name of the function.
Why would you want this ? If you need to add text for some reason, that's what comments are for.
Having text between parameters will make code hard to read and maintain. It could even be difficult to determine what is a parameter or not. The compiler / interpreter will certainly need more time and resources to determine what's a parameter and what is just text.

replicate javascript unsafe numbers in golang [closed]

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 1 year ago.
Improve this question
So in go, I do a calculation which gives "5726718050568503296"
In JS it gives "5726718050568503000"
I would like to replicate this behavior in go
As discussed in the comments of Why is 5726718050568503296 truncated in JS as others have mentioned, this is caused by the way toString() is implemented in JavaScript which appears to use the minimium number of significant digits to return the same represented number, rather than returning the mathematically closest number. You can however replicate this behaviour in Go by using the special -1 precision with strconv.FormatFloat:
package main
import (
"fmt"
"strconv"
)
func main() {
n, _ := strconv.ParseInt(strconv.FormatFloat(5726718050568503296.0, 'f', -1, 64), 10, 64)
fmt.Println(n)
}
Playground link: https://play.golang.org/p/9ZObcB3so4o
Seems like there is no easy way around it.
JS has its way of representing the Number type and the integer you presented basically overflows the capacity of it.
Please refer to: https://stackoverflow.com/a/1379973/10316247 for more information about how JS handles this and why this wierd behavior happens.
Replicate that in go would be very very trick as you'd need to look at the byte representation of this int and try to assume how JS would handle it.
I would (as the link above suggests) use it as and string.

is javascript Math.sin() O(1)? [closed]

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 5 years ago.
Improve this question
I am wondering if Math.sin(), Math.cos() are O(1), and how is it translated to the compiler.
We can create a match table for it.
Or we can create a not so detailed match table and use a linear formula to calculate the value.
Or we can break it down.
I guess in any sense Math.sin() is sorta O(1) as the input is bounded, but how exactly is it done in the compiler and how complex it is?
It might be impossible to give a definite answer, so I will try to provide you with as much information as I can instead.
The Math object implementations can differ between browsers and operating systems ("Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.[1]").
There are a few major ways in which a sine function can be implemented:
Taylor series approximation
CORDIC algorithm
Complexity will depend on implementation. As far as I know, CORDIC algorithm is more common.

How Does Javascript if statement indentation work? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
How many spaces should be used for each line of if statement at the beginning of the line?
suppose we have 10 lines of nested if statements, what is the formula to determine how many lines to be used? Also can a programmer use the tab key instead to obtain proper indentation for a statement.
if (a !== 0) {
if (b > 1) {
if (c < 1) {
Indentations and spaces is not a requirement it just helps your code to be more readable.
Unlike languages like Python in which whitespace is considered semantic by the compiler, the Javascript compiler ignores runs of whitespace altogether. In my experience, though, the most common indentation (tab) lengths are 2 spaces and 4. My personal preference is 4, but plenty of other people will say 2.
I will say, though, that if you're using more than 4, you're either going to need to break your more nested logic up into multiple lines, or you're going to end up with a lot of overruns -- even with only 4 spaces, I often find myself running over 80 characters.

Categories