What is the code behind "(" and ")" | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 6

What is the code behind "(" and ")"

In programming a scientific calculator, what is the code behind the brackets button so that the code inside the brackets can be executed first? https://code.sololearn.com/WL79pjz3ike3/?ref=app

1st Jul 2019, 9:11 AM
eMBee
eMBee - avatar
24 Antworten
+ 3
Probably because the calculator on your phone does not just consist of one eval. It can probably be resolved with regular expressions
1st Jul 2019, 11:11 AM
Airree
Airree - avatar
+ 4
Why would it be the same?
1st Jul 2019, 10:09 AM
Airree
Airree - avatar
+ 4
Airree, I've never studied anything science before neither am I a professional in coding, I'm a business student and its the passion I have for coding that is helping me progress
1st Jul 2019, 10:11 AM
eMBee
eMBee - avatar
+ 3
I was expecting 4 but it outputs an error
1st Jul 2019, 9:22 AM
eMBee
eMBee - avatar
+ 3
Well, that's because the input is invalid. That is pretty obvious, I thought you would know that. But you can still catch the error though
1st Jul 2019, 9:24 AM
Airree
Airree - avatar
+ 3
So how can this be fixed?
1st Jul 2019, 9:25 AM
eMBee
eMBee - avatar
+ 3
try { calMo.value = eval(calMo.value); } catch (e) { calMo.value = "Invalid input!"; } Seriously, you don't know this?
1st Jul 2019, 9:29 AM
Airree
Airree - avatar
+ 3
Because 2(2 is the same as 2*2 @~ swim ~
1st Jul 2019, 10:08 AM
eMBee
eMBee - avatar
+ 3
~ swim ~ in Mathematical language Airree it would be the same because "2(2" is also the same as 2x(2 🤷‍♂️
1st Jul 2019, 10:14 AM
eMBee
eMBee - avatar
+ 3
Well, I have never heard of that. Maybe you could do it with regular expressions, but I don't think you need multiple possibilities for multiplication in a simple calculator.
1st Jul 2019, 10:17 AM
Airree
Airree - avatar
+ 3
~ swim ~ Yes, I heard of that, but I was talking about 2(2.
1st Jul 2019, 10:25 AM
Airree
Airree - avatar
+ 3
~ swim ~, I am talking about a(2+3) = a*5
1st Jul 2019, 10:52 AM
eMBee
eMBee - avatar
+ 3
Airree, The calculator preinstalled on my phone which I use gives an output of 4 whenever I type 2(2 or 10 when I type 2(2+3 without even closing the brackets
1st Jul 2019, 10:55 AM
eMBee
eMBee - avatar
+ 2
You start evaluating from there first. But I don't get why you are asking this, since it works perfectly. eval does that by default
1st Jul 2019, 9:20 AM
Airree
Airree - avatar
+ 2
Airree it doesn't. Why don't you try this on the calculator and see what it outputs "2(2"
1st Jul 2019, 9:22 AM
eMBee
eMBee - avatar
+ 2
So how is it possible that the calculator on my phone does not produce an error 🤔
1st Jul 2019, 11:03 AM
eMBee
eMBee - avatar
+ 2
I'll post a picture of how the calculator preinstalled on my new phone does it without producing an error and I'll tag you to the post
1st Jul 2019, 11:05 AM
eMBee
eMBee - avatar
+ 2
~ swim ~ that's exactly what I'm talking about, now we're on the same page. So what I'm asking is how do I write a program for those brackets in my code?
1st Jul 2019, 11:17 AM
eMBee
eMBee - avatar
+ 2
Or Airree maybe you can help me with the question plizzzzz
1st Jul 2019, 12:00 PM
eMBee
eMBee - avatar
+ 2
Airree Wait a minute, what are regular expressions?
1st Jul 2019, 12:29 PM
eMBee
eMBee - avatar