0

Hello everyone, tell me why it doesn't multiply? After all, js automatically translates a string into numbers

function calculate(str) { str.match(/\D/g) return str.match(/\d/g).join(str.match(/\D/g)); } console.log(calculate('7*3'))

15th May 2021, 2:31 PM
Тимур Завьялов
Тимур Завьялов - avatar
6 Respostas
+ 2
you must first adapt the regular expression to match the operators (what you named 'sign') you want to support, and capture it as I capture the numbers (by enclosing them in parenthesis): /^\s*(\d+)\s*([*/])\s*(\d+)\s*$/ in regexp: 1) you match spaces char with \s (as \d match digit) 2) + is interpreted as modifier wich mean "one or more of the previous char match" 3) * is modifier for "zero or more" 4) round brackets (parenthesis) capture the match (and modifiers apply to whole match if immediatly after the closing parenthesis) 5) square brackets define class char (modifiers are not allowed inside, but could be put just after, so '*' doesn't need to be escaped with '\' -- but '-' has special meaning of 'range' -- alternatively, you could use the OR operator | without square brackets...) 6) ^ and $ are 'anchors', respectively meaning start and end of string... ... that's only a few quick explanation of the complex regexp grammar ^^ to be continued in next post ;)
17th May 2021, 6:27 AM
visph
visph - avatar
+ 2
no, js doesn't "automatically translate a string into numbers" ^^ js does implicit casting in very specific cases/context... both of your codes just split a string and then concatenate them to the same string ;P even by doing explicit casting to numbers of the number parts of your string, you will end with an equivalent string, as the operator in the expression is also a string (a char), and any number added to a string will be casted to a string before ! to compute the result of a string expression, you roughly have two ways: 1> the simpler one, use of eval() function (but considered as evil because could introduce security holes): console.log(eval('7*3'); 2> parse your expression and compute the result: this means complexity if you want to handle any kind of expression, but could be quite easily achieved if you only need to parse multiplication of two (whole) numbers: https://code.sololearn.com/Wn1OYkIiOBVq/?ref=app
16th May 2021, 3:52 PM
visph
visph - avatar
+ 2
(2nd part) then, the numbers are now at indices 1 and 3, and operator is at index 2. you need now to test for the operator and return the related expression: if (nums) { if (nums[2]=="*") return nums[1] * nums[3]; if (nums[2]=="/") return nums[1] / nums[3]; }
17th May 2021, 6:27 AM
visph
visph - avatar
0
I also tried so. And nothing came up. function calculate(str) { str.split('') n = str[0]+str[1]+str[2]; return parseInt(n) } console.log(calculate('7*3'))
15th May 2021, 6:05 PM
Тимур Завьялов
Тимур Завьялов - avatar
0
visph, tell me, can I make it so that I changed the action sign, for example, 6/3 and this sign was automatically put in this expression return nums[1] * nums[2]?
17th May 2021, 5:51 AM
Тимур Завьялов
Тимур Завьялов - avatar
0
visph, thank you for detailed answer)
17th May 2021, 11:16 AM
Тимур Завьялов
Тимур Завьялов - avatar