variables inside the function and order of Precedence in JS
The variable fahrenheit has function scope i understand but it is still not declared (as a let or const )but works why so??
function convertToFahrenheit(celsius)
{
fahrenheit = (celsius*9/5)+32;
return fahrenheit;
}
console.log(convertToFahrenheit(25));
37 Replies
because javascript is very forgiving for bad code in some ways
just don't do this, it's bad form and a bad habbit to get into
Ok. One another thing F=(C9/5)+32;;;here i do understand that order of precedence is higher for multiplication and division (following left to right as a direction) and then addition,so only the round brackets are used at first. But to calculate celcius the formula is;C=F-32x5/9
Where can we put the brackets for C=F-(325/9) is this a right way?
(F-32) * 5 / 9
afaicr at least
you have to subtract the 32 first, but if you put the brackets like you suggested it will subtract the total of (32*5)/9
Then here we are not following the order of Precedence ?How do we understand where to put brackets,because brackets have the highest priority.
we are though.
F-(32*5/9)
gets reduced to F-(160/9)
then F-17.77778
when what you want is to have F - 32 done firstwhen u don't declare with a const let or var, js automatically sets is as a global variable. but yah, VERY BAD. don't do it.
That is what to say how will we get to know that we need to calculate F-32 first??
Algebra?
it's literally algebra
not algebra .When not to use order of precedence
when you need to override it to perform the correct calculation
there's really nothing more to it than that. If the calculation you need to perform requires you to use a different order of operations than the order of presedence, you have to use brackets to override it
Basically we dont know whether our calculation is right or wrong nwe just follow the order of precedence .If we are overriding to perform F-32 first then another guy can override to perform 32*5/9 and then subtract.
where to put the brackets matters because that will be solved first
You have to do calculations in a certain order, that's part of algebra. The way it's written down varies, some people write without order of precedence, some do, it's up to you to figure out which one but the only way will be to try it out or to compare different sources. JavaScript and all other languages I know of do honor order of precedence, so you will have to add brackets whenever you need to override that order of precedence
I feel like you're asking for some rule of thumb or trick to get it to work all the time, but there isn't one. You have to think about the order you want the operations to go in, and then ensure the programming language does them in that order. There's no one solution to get that thinking step out of the way, it will always be dependent on the source of the equation you're implementing, and the way it's written
this is from the college notebook
it shows how the formula is derived
try to understand the equations here
in the end u can see
c/5 = (f-32)/9
multiply both sides with 9
c / 5 * 9 = f - 32
(c / 5 * 9) + 32 = f
because the operations of multiplication and divisions come first, we isolate it within a bracket
idk which one u r having issue with, understanding how mathematical expressions are evaluated in order or how languages will handle it
I do understand the maths here.But then language will handle as per our instructions only. Consider if a person from non mathametical background is solving this issue he knows the lang that is the order of precedence so he will follow F-(32*5/9 ) blindly,Which is true because the lang itself states order and we will not be given direct accurate formula.
And we will not be sure what we are gonna overide is correct or not
Consider another example;
let isHoliday = true; age<=6||age>=65 && !isHoliday
here as per order of precedence !operator is evaluated then AND operator is evaluated.So age<=6||age>=65 && False
Now as per the order of precedence AND get the priority so tell me where can we use the Brackets????Can you fix the code block?
I dont know why is it highlighting
but if we click on it the content is shown.
You can use brackets wherever you want
I don't rely on order of precedence for logic
end result will be wrong.
not if you put the brackets in the right place depending on the needs of the logic
i mean if it's confusing and feels risky then just part into multiple expressions for safety
according to the order
like instead of
a + b * c / d
do
x = c / d
y = b * x
z = a + y
Yes i am getting this.Only confusing thing is where do i put brackets
this has to be kept in mind then.
I'm really confused what you're asking here. The brackets go where the brackets need to go to make the operation work
there's no one answer
you can put as many as you want to make it clear to yourself as much as to the programming language
To make it very simple
let isHoliday = true; age<=6||age>=65 && !isHoliday
where do you think you will put brackets??that depends on what you want the logic to do, but if you want code to run when someone is very young or very old, and only when it's not a holiday:
Exactly i want this pbm statement only.
My doubt here is why you put brackets for age, as per precedence it will be
if(age<=6||(age>=65 && !isHoliday)) { }
the issue is though, you could also have a situation where you want the code to run when someone is under 6, or over 65 and it's not a holiday
is equally valid. It depends on the logic
because you want to override the precedence
you put the brackets there because you want to change the logic
u put brackets depending on which calculation u want to do first
1 + 2 * 12 - 6 / 3 ---> 23follows the BDMAS rule, division / multiplication followed by addition / subtraction.
1 + 2 * (12 - 6) / 3 ---> 3STILL follows the BDMAS rule. outputs 5 i mean this is just the same ol' BDMAS rule Bracket Division Multiplication Addition Subtraction when it comes to conditionals normally AND operation (&&) is evaluated before an OR (||) but u can override it by using brackets
and honestly, in my opinion, you always should override it with brackets. It's much easier to read a conditional that has some clarifying brackets in it, than it is to try to remember order of precedence
Make sure a human and a computer can only interpret it one way
ya i agree
Yea i am getting the difference is in the sentence:when someone is under 6, or over 65 and it's not a holiday use your given condition mentioned here.
when someone is very young or very old, and only when it's not a holiday:this code here because of the sentence i mean comma it gives a lot meaning..
btw u can also break up conditions if u want
like?
like this is something i also do when i have a bunch of conditions
like for example u r trying to check for box collision for your game, that requires 4 conditions. on top if u need to add extra conditions like is the item red coloured that's 5 total. instead of having all 5 conditions at once u can break it down
this condition fails over here becuase what if age is less than 6 and is a holiday where is the condition check for that?
i broke down this code, and here it doesn't check if age <= and if it's a holiday or not
but if u wanna do more check just add
break it further down if u want
oh this seems easy !
Great.