33. Methods

javascript

#1

https://www.codecademy.com/en/courses/spencer-sandbox/5/4

When I run my code, it works beautifully. However, I receive the following error message:

Oops, try again. Your perimeter method fails on a Circle with radius 1 where it returns τ when it should return 6.283185307179586

Now, I know this is Codecademy's SCT thinking that I've returned the wrong value. But I haven't, 6.283185307179586... is the constant known as tau. The symbol for tau is τ. I returned the correct value and the SCT doesn't care. This code works, but because the SCT doesn't recognize π and/or τ as values, my code is considered incorrect. What's up with that?

var pi = Math.PI;
var tau = (pi * 2);
var circles = new Array();
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        var area = (pi * this.radius * this.radius);
        if (area == pi) {
            return "π";
        } else if (area == tau) {
            return "τ";
        } else {
            return area;
        };
    };
    this.perimeter = function () {
        var perimeter = (2 * pi * this.radius);
        if (perimeter == pi) {
            return "π";
        } else if (perimeter == tau) {
            return "τ";
        } else {
            return perimeter;
        };
    };
};
for (i = 1; i <= 10; i++) {
    circles[i - 1] = new Circle(i);
};
for (i = 0; i < circles.length; i++) {
    console.log("I am a circle with a radius of " + (i + 1) + ", a perimeter of " + circles[i].perimeter() + ", and an area of " + circles[i].area() + ".");
};

Pi and Tau in programming
Pi and Tau in programming
#2

such symbols are not recognized in the javascript language. Python just sees the symbols as strings


#3

I understand this, this is not my point. My point is JavaScript should recognize these symbols, especially when codecademy's SCT is essentially telling me that π ≠ pi and τ ≠ tau. Such statements are so false, that they make both the SCT and JavaScript seem ridiculous.


#4

SCT only validates what you are suppose to do in the exercise

that TAU should implemented in JS is not for codecademy to decide


#5

I did what was asked in the exercise, just not in the way it expected. What's wrong with being clever?

Look, I know what it wants me to do, It wants me to do this:

var circles = new Array();
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        var area = (Math.PI * this.radius * this.radius);
        return area;
        };
    };
    this.perimeter = function () {
        var perimeter = (2 * Math.PI * this.radius);
        return perimeter;
    };
};
for (i = 1; i <= 10; i++) {
    circles[i - 1] = new Circle(i);
};
for (i = 0; i < circles.length; i++) {
    console.log("I am a circle with a radius of " + (i + 1) + ", a perimeter of " + circles[i].perimeter() + ", and an area of " + circles[i].area() + ".");
};

This code is essentially the same thing as my code. I was just being clever, and if being clever is considered incorrect, then what is the point of learning? I'm not trying to be rude here. I'm just stating that it is really discouraging when I'm told my way of doing things is wrong, just because it is a little different.


#6

Unfortunately, the design of programming languages and Codecademy's SCTs are imperfect art forms. We can often think of improvements to both, and opinions will vary widely.

The best I was able to do with this exercise and @aquaphoenix17's comments was to come up a means of modifying the code from the original post to formulate a solution that satisfies both the JavaScript interpreter and Codecademy's SCT. Other remedies are possible.

See the following as a compromise solution, and feel free to refine ...

var pi = Math.PI;
var π = Math.PI;
var tau = pi * 2;
var τ = pi * 2;
var circles = new Array();
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        var area = π * this.radius * this.radius;
        if (area == π) {
            return π;
        } else if (area == τ) {
            return τ;
        } else {
            return area;
        };
    };
    this.perimeter = function () {
        var perimeter = 2 * π * this.radius;
        if (perimeter == pi) {
            return this.π;
        } else if (perimeter == τ) {
            return τ;
        } else {
            return perimeter;
        };
    };
};
for (i = 1; i <= 10; i++) {
    circles[i - 1] = new Circle(i);
};
for (i = 0; i < circles.length; i++) {
    console.log("I am a circle with a radius of " + (i + 1) + ", a perimeter of " + circles[i].perimeter() + ", and an area of " + circles[i].area() + ".");
};

Regarding whether Codecademy should modify the SCT, am not sure ... ¯\(ツ)


#7

I apologize if I at all sounded rude, It's just that it is really discouraging when you are told that your way of doing things is wrong, just because it is a little different. (This was the philosophy of my 7th grade math teacher and let's just say I may or may not have been responsible for her getting fired because of it...)

I appreciate you trying to find a compromise. Though, in the end, I ended up just doing this:

var circles = new Array();
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        var area = Math.PI * this.radius * this.radius;
        return area;
    };
    this.perimeter = function () {
        var perimeter = 2 * Math.PI * this.radius;
        return perimeter;
    };
    this.logCircle = function () {
        console.log("I am circle with a radius of " + this.radius+ ", a perimeter of " + this.perimeter() + ", and an area of " + this.area() + ".");
    };
};
for (i = 1; i <= 10; i++) {
    circles[i - 1] = new Circle(i);
};
var logCircles = function () {
    for (i = 0; i < circles.length; i++) {
        circles[i].logCircle();
    };
};
logCircles();

#8

You're not being rude. :smile:

The issue you bring up is an interesting one that runs a lot deeper than the matter of the SCT for this exercise. How should we handle a Transcendental number (Wikipedia), when we want a function to produce an exact result?

The numerical constants that are defined in programming languages for pi and tau are inexact, so a function that uses those constants produces an inexact solution. Let's suppose we want a function to produce an exact result for the area of a circle with radius 1.

  • Inexact result: 3.141592653589793
  • Exact result: π

It is a challenge to try to perform numerical calculations with the second result, while maintaining exact numerical values.

But ... how about defining a Python or JavaScript type that maintains exact values by expressing them in terms of π and Τ? That might make for a good discussion in the Corner Bar, if someone wishes to initiate it, using the current discussion as a starting point. :wink:


Pi and Tau in programming
#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.