Lol, it sounded like ternary. I just wonder how it’s possible to write a boolean in along code so it’s still makes one line of it. Weird. Happens, when you don’t know all the fundamentals or at least approaches of this language
What about practical excercises for this language? Can you recommend any web page?
Join repl.it and log in. In your profile menu select Learn/Teach and choose JavaScript. That should give you a number of exercises to solve.
The rudiments should be learned up front, not in a project. Programs are essentially a bundle of smaller programs, each with simple purpose roles that contribute to the overall program. Learn to write small, purpose based functions and prograrms so that when it comes to a larger project you can visualize it as the smaller parts and build each component in a stand alone environment. Write, write and re-write until the function is error free and resistant to errors.
There was just the parameter radius as an argument. The test must have needed to define the radius when calling the function. Somehow it showed undefined…
As all variables must be defined before we poll them. The computer does not know what a radius is and cannot assign arbitrary values to an undefined variable.
Unlike C or Java, variables in JavaScript are typecast on the fly, and determined by the value.
let radius = Math.PI;
The type will be float since PI is a float.
let radius = 5;
The type will be integer since 5 is an integer. That is how we assign type, by simply assigning a value. That also means we can change the data type on the fly just by changing the value the variable points to.
Bottom line, we must define variables before using them in our code.
A variable can be defined inside the parameter of a function.
function areaOfCircle(radius) {
return Math.pow(radius, 2) * Math.PI;
}
Now whatever type we pass to the function will be the type on the parameter.
What is the problem you are working on? Clearly it’s a geometry question involving area of a circle for which we have one known, the radius, one unknown, the area, and a constant, PI. The area varies directly with the square of the radius times PI.
It’s just an excercise I was messing with, there is no real life point with it. Developing the logical side and function writing perhaps. I just wonder why it’s so illogical
Can we see the problem? In geometry there is very little that is illogical. It’s an analysis tool that for centuries we have been able to rely upon in all number realms, including imaginary numbers.
y = f(x)
describes a dependent variable (y) as it relates to an independent variable (x). y is said to be a function of x. In our case the function is,
f(x) = x**2 * c
where x is the radius of a circle, and c is a constant, PI. So,
y = r**2 * PI
Here we know that y represents the area of a circle, so we could write,
Programming it can be illogical and non life-oriented, when I have to sign a random value to make the piece of code work til a test overwrites the value with it’s own
Math.pow() if the power function, and in this expression it is a method of the Math object which is dedicated to working with mathematical functions and constants.
Given a value (a base) and a power (the exponent, 2), the function will raise the value to the power of the exponent.
Yeah, but why the h… it does give me a result of undefined? Do I need to declare the radius as a local variable or do I need to use console.log radius smth or is there any 3rd option?
Yeah. That’s what I thought too. Somehow the environment there told me undefined after the test had launched. In my logic, it should have worked, when the test environment will pass in a value to radius argument. Somehow it still said undefined. What to do in that case? Declare the radius in local level somehow?