Converting an equation [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a equation in the form of string like this 1+(x*10) = 21 . I have to covert it like this x = (21-1)/10 in any language . I do not want an exact solution but Please give me some hint .

If a hint is enough for you check out this library:
http://algebra.js.org/
It is capable of taking definitions of equations (as string or step by step), solve them and make the result available as string.

Easiest way - call some CAS (computer algebra system) program from command line passing equation to solve. Like:
maxima --very-quiet -r 'solve ([1+(x*10) = 21], [x]);'
And get results back from standart output. But of course this should be done server-side,
i.e. if from PHP - can be executed with shell_exec() function.

To me, this problem can be divided into 2 stages, finding x's side (left or right of the "="), and scanning and converting a literal along with the appropriate operation.
For the first stage, you can find which part of the equation x lies in by simply comparing the indexes of "=" and "x" in the String.
There are 2 types of components you need to worry about now i.e. operands (values) and operations (+, - etc) .
You can try shifting operands from the "x-side" to the other, by simply converting the operation associated with the value (+ becomes - and vice versa, same for * and /). For example, 1 + x = 7. 1 is the operand and '+' is the operation associated with 1, which gets converted to '-' when it moves to the other side.
Do make sure to add a bracket at the start and end of the right hand side every time you move an operand and operation to the other side, so that the equation maintains the order of calculations needed to be done.
Hope this helps!

Related

Convert 17digit number string to a integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert a 17digit number string into a number
this is the number "76561197962169398".I tried using parseInt()
The result of using parseInt is :-
76561197962169390
I am loosing the last digit.I also tried BigInt() 'n' is getting appended to the number.
I m thinking of using replace() with a regex for only digits.
Is there any other way I can achieve this without loosing precision.
Please any help regarding this is really appriciated.THANK YOU
in chrome 83 devtools:
x=76561197962169398n
76561197962169398n
++x
76561197962169399n
typeof x
"bigint"
y=BigInt("76561197962169398")
76561197962169398n
++y
76561197962169399n
x+y
153122395924338798n
x + 1
VM342:1 Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
at <anonymous>:1:2
(anonymous) # VM342:1
x + 1n
76561197962169400n
[5n, 3n, 9n, 7n].sort()
[3n, 5n, 7n, 9n]
The n suffix is for display - and in code it's needed to say a literal value needs to be treated as bigint instead of number - think of it like quotes for strings - without quotes a sequence of characters is not a string - similarly a number without n suffix is not a bigint - it's a number that has limited precision and simply cannot be used for large values

convert any string like "10", "-0.129894", "12.02102" to number without adding or loosing anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
As you can see from the title I have various cases for strings that can contain numbers in them. I found out that using parseInt() and parseFloat() didn't work for me as parseInt will convert number like 10.28 to just 10, but parseFloat will make number like 10 into 10.0, I want to somehow convert string into number so it stays exactly like it was in the string without anything removed or added.
Per MDN Number ( MSDN page also, but not so much info ).
At the top of the page:
The primary uses for the Number object are:
If the argument cannot be converted into a number, it returns NaN.
In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.
At the bottom of the page, there are some examples:
Convert numeric strings to numbers
Number("123") // 123
Number("") // 0
Number("0x11") // 17
Number("0b11") // 3
Number("0o11") // 9
Number("foo") // NaN
Number("100a") // NaN
Demo https://jsfiddle.net/hxkfafdw/
More on the topic - Number("foo") is NaN Number("f00") - same. Number("0xf000") - this is a hex number.

How do I tell what index a letter is in a string? (In Python, JS, Ruby, PHP etc...) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know that:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
print alphabet[0]
# prints a
print alphabet[25]
#prints z
and so on, but how do I find out the opposite, ie. :
alphabet = 'abcdefghijklmnopqrstuvwxyz'
's' = alphabet[?]
""" The question mark represents that I want to know
what index the letter is in the string."""
In python you could use the find method:
>>> alphabet = 'abcdefghijklmnopqrstuvwxyz'
>>> alphabet.find('a')
0
>>> alphabet.find('b')
1
>>> alphabet.find('c')
>>> alphabet.find('z')
25
Edit to add: Like Warren pointed out you can also use index, the difference being that find will return -1 for the position if not found, and index raises a ValueError when not found.
In javascript, use indexOf:
> "abc".indexOf("b")
1
In javascript, you would use alphabet.indexOf('a').
In Python, to get the position of a certain character inside a string, it would be:
alphabet.index('s')
If you just want to get alphabet indices, this works (Python):
import string
for c in string.ascii_lowercase:
print(ord(c)-97)
This will print the numbers 0-25. The idea here is to make use of unicode points of the char using ord. lowercase chars starts at 97. Another example:
>>> ord('k')-97
10

Phone Number Validation with Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a web form and want to accept the following phone numbers in the following format:
1234567890
123-456-7890
123.456.7890
123-4567890
The first number cannot be a 0 or a 1.
How can I do this with regex/javascript? I found a few regex formulas online but none are specific to my needs
null !== thenumber.match(/^[2-9][0-9]{2}[.-]?[0-9]{3}[.-]?[0-9]{4}$/);
(Edited to give slightly better answer with boolean result)
Consider the following Regex...
[\d-[01]]\d{2}[-\.]?\d{3}[-\.]?\d{4}
Note: You examples start with a 1 which will not satisfy the above regex.
The user-provided format should be irrelevant. It makes more sense to store phone numbers as, well, numbers, i.e. digits only, and add uniform formatting when displaying them back. Otherwise you end up wit a mess in your database and you're going to have a hard time searching for numbers (if you wanted to find if given number is already in your DB then how'd you know the format it was typed in?), and you will have inconsistent formatting of numbers when displaying them.
So you'd store any of your example numbers as 1234567890, no matter what the user has typed into the form. Which means you can validate your input by stripping any non-digits, then checking the length and other conditions, like this:
function validPhone( num ){
var digits = num.replace(/\D/g,'');
// assuming 10 digits is a rule you want to enforce and the first digit shouldn't be 0 or 1
return (parseInt(digits[0],10) > 1 && digits.length == 10);
}
You could also use return parseInt(digits, 10) >= 2000000000 to validate that the number doesn't start with 0 nor 1 and has at least 10 digits.

why no parseInt for if statements [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 9 years ago.
Improve this question
var a = $('.box').width();
var b = $(document).height();
Now, let's say 124px, is an int or string ?
So far I have done this through trial and error, but now I'm curious to understand why if (b < 20), however parseInt is needed to calculate var c = a + b. why is that?
Note: I'm assuming that a and b are strings like "10" and "20", for the purposes of this question. $('.box').width() will actually return 123 (if your element was 123 pixels wide, for example), so in this case parseInt is irrelevant.
Because in
a + b
If you don't use parseInt, then JavaScript will assume you want to concatenate the strings (because + is overloaded for concatenation). However, when you use
b < 20
JS knows that you can't have "a string less than 20," because that makes no sense, so it casts to a number automatically.
We don't.
.width() and height() return numbers, not strings, and so don't need to be converted using parseInt(...,10). I don't know who the "we" is you refer to, but it does not include people who use jQuery correctly.
Try it out on this very website - open up a console, and type var d = jQuery("div"); console.log(typeof d.height(), typeof d.width(), d.height() + d.width());, and you'll see [number], [number], and the result of a normal numeric addition.
because when you use the < operators it only makes sense for numbers to be bigger or smaller, however for the "+", in javascript it could be used to concatenate strings as well so:
"30"+"20"="3020"
and 30+20=50
NOTE: #Mike Poxman Kamerman is right, for the most part it will work without parseInt because the width and height jquery functions return floats but it is the correct practice as javascript used to be very strict and in browsers like older IEs it could not work.
When you are comparing a string and a number, one has to be converted to the type of the other for the comparison to be possible. The string will implicitly be converted to a number.
When you are using the + operator with two strings, there is no conversion needed for the operation to be possible. It will just concatenate the strings, but if you want to use the operator for addition instead of concatenation, then you need to explicitly convert the strings to numbers first.

Categories