Skip to content

Commit 08c4752

Browse files
committed
Remove string prerequisite and apply review comments
1 parent b0f24e5 commit 08c4752

File tree

10 files changed

+196
-98
lines changed

10 files changed

+196
-98
lines changed

concepts/comparison/about.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
11
# About
2+
3+
TODO: this is a stub that needs more information
4+
5+
## Comparing numbers
6+
7+
Numbers are considered equal if they have the same value.
8+
9+
```javascript
10+
1 == 1.0;
11+
// => true
12+
13+
1 === 1.0;
14+
// => true
15+
// Remember, all numbers are floating-points, so this is different syntax for
16+
// the exact same value.
17+
18+
1 === 1n;
19+
// => false
20+
// Strictly checking a number against a bigint will always result in false.
21+
22+
1 == 1n;
23+
// => true
24+
// A number is equal to a bigint if they represent the same value.
25+
26+
1.0 == 1n;
27+
// => true
28+
// A number is equal to a bigint if they represent the same value.
29+
```
30+
31+
There are different outcomes when comparing numbers with different types.
32+
In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`):
33+
34+
| A | B | `==` |
35+
| ------ | --------- | --------------------- |
36+
| Number | Undefined | `false` |
37+
| Number | Null | `false` |
38+
| Number | Number | `A === B` |
39+
| Number | String | `A === ToNumber(B)` |
40+
| Number | Boolean | `A === ToNumber(B)` |
41+
| Number | Object | `A == ToPrimitive(B)` |
42+
43+
- `ToNumber(X)` attempts to convert its argument `X` to a `number` before comparison. It is equivalent to `+B` (the unary `+` operator).
44+
- `ToPrimitive(X)` attempts to convert its object argument `X` to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`.

concepts/numbers/about.md

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# About
22

3-
There are two different kinds of numbers in JavaScript - numbers and bigints
3+
There are two different kinds of numbers in JavaScript - numbers and "bigints"
44

55
Numbers are the most used, and represent numeric data type in the double-precision 64-bit floating point format.
66

@@ -16,7 +16,7 @@ let numericValue = 42;
1616

1717
A number literal like `42` in JavaScript code is a floating-point value, not an integer.
1818
There is no separate integer type in common everyday use.
19-
The `BigInt` type was not designed to replace `Number` for everyday uses.
19+
The `bigint` type is not designed to replace the `number` type for everyday uses.
2020
`42` is still a `Number`, not a `BigInt`.
2121

2222
Number may also be expressed in literal forms like `0b101`, `0o13`, `0x0A`. Learn more on numeric lexical grammar [here][lexical-grammar].
@@ -28,7 +28,7 @@ There are two built-in objects that are useful when dealing with numbers:
2828
- [`Number`][built-in-number]: static properties for common / useful values, static methods for [type-checking][type-checking] and [type-conversion][type-conversion], instance methods for [type-conversion][type-conversion] and [formatting numbers as strings][string-formatting].
2929
- [`Math`][built-in-math]: properties and methods for mathematical constants and functions, does **not** work with `BigInt`.
3030

31-
The `Number` built-in object is _also_ a global `function` that can be used to convert _almost anything_ that can be represented as a `number` to a `number`. It is less forgiving then _parsing_ a string to a number.
31+
The `Number` built-in global `object` is _also_ a global `function` that can be used to convert _almost anything_ number-like to a `number`. It is less forgiving than _parsing_ a `string` to a `number`.
3232

3333
```javascript
3434
const date = new Date('December 17, 1995 03:24:00');
@@ -45,11 +45,11 @@ There are three types of maximum (and minimum / maximum negative) values for num
4545
- `SAFE_INTEGER`: given by `Number.MAX_SAFE_INTEGER` and `Number.MIN_SAFE_INTEGER`
4646

4747
Because of how numbers in JavaScript are implemented, **not** every number between `Number.MIN_VALUE` and `Number.MAX_VALUE` can be represented.
48-
However, _every_ number between `Number.MIN_SAFE_INTEGER - 1` and `Number.MAX_SAFE_INTEGER + ` **can** be represented.
48+
However, _every_ number between `Number.MIN_SAFE_INTEGER - 1` and `Number.MAX_SAFE_INTEGER + 1` **can** be represented.
4949

5050
## Comparison
5151

52-
Numbers can be considered equal if they have the same value.
52+
Numbers are considered equal if they have the same value.
5353

5454
```javascript
5555
1 == 1.0;
@@ -63,31 +63,20 @@ Numbers can be considered equal if they have the same value.
6363
1 === 1n;
6464
// => false
6565
// Strictly checking a number against a bigint will always result in false.
66+
```
6667

67-
1 == 1n;
68-
// => true
69-
// A number is equal to a bigint if they represent the same value.
68+
See [comparison][concept-comparison] for more information on comparisons in general and comparing numeric values in JavaScript.
7069

71-
1.0 == 1n;
72-
// => true
73-
// A number is equal to a bigint if they represent the same value.
74-
```
70+
## Pitfalls
7571

76-
Because there are different ways to do [comparison][comparison], there are different outcomes when comparing numbers with different types.
77-
In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`):
72+
Becuase numbers in JavaScript are floating point numbers, all math using these values is floating point math. Therefore, in JavaScript:
7873

79-
| Left | Right | `==` |
80-
| ------ | --------- | --------------------- |
81-
| Number | Undefined | `false` |
82-
| Number | Null | `false` |
83-
| Number | Number | `A === B` |
84-
| Number | String | `A === ToNumber(B)` |
85-
| Number | Boolean | `A === ToNumber(B)` |
86-
| Number | Object | `A == ToPrimitive(B)` |
74+
```javascript
75+
0.1 + 0.2 === 0.3;
76+
// => false
77+
```
8778

88-
In the above table, `ToNumber(X)` attempts to convert its argument to a number before comparison.
89-
Its behavior is equivalent to `+A` (the unary `+` operator).
90-
`ToPrimitive(X)` attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`.
79+
See [0.30000000000000004.com](https://0.30000000000000004.com/) for a brief explanation and [Appendix D](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) of Oracle's Numerical Computation Guide "What Every Computer Scientist Should Know About Floating-Point Arithmetic" for an in depth explanation.
9180

9281
## Related concepts
9382

@@ -103,5 +92,6 @@ Normally these would be put in a list, but it renders better when it's next to e
10392
[comparison]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
10493
[lexical-grammar]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_literals
10594
[string-formatting]: https://exercism.lol/tracks/javascript/concepts/string-formating
106-
[type-checking]: https://exercism.lol/tracks/javascript/concepts/type-checking
107-
[type-conversion]: https://exercism.lol/tracks/javascript/concepts/type-conversion
95+
[concept-comparison]: ..
96+
[concept-type-checking]: https://exercism.lol/tracks/javascript/concepts/type-checking
97+
[concept-type-conversion]: https://exercism.lol/tracks/javascript/concepts/type-conversion

concepts/numbers/introduction.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Introduction
22

3-
There are two different types of numbers in JavaScript:
3+
Many programming languages have specific numeric types to represent different types of numbers, but JavaScript only has two:
44

55
- `number`: a numeric data type in the double-precision 64-bit floating point format (IEEE 754).
66
Examples are `-6`, `-2.4`, `0`, `0.1`, `1`, `3.14`, `16.984025`, `25`, `976`, `1024.0` and `500000`.
77
- `bigint`: a numeric data type that can represent _integers_ in the arbitrary precision format.
88
Examples are `-12n`, `0n`, `4n`, and `9007199254740991n`.
99

10-
In contrast, in many other programming languages different numeric types can exist, for example: Integers, Floats, Doubles, or Bignums.
11-
1210
If you require arbitrary precision or work with extremely large numbers, use the `bigint` type.
1311
Otherwise, the `number` type is likely the better option.

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"status": "active"
6767
},
6868
{
69-
"slug": "numbers",
69+
"slug": "freelancer-rates",
7070
"name": "Freelancer Rates",
7171
"uuid": "0aff2fa7-55ea-47e9-af4a-78927d916baf",
7272
"concepts": ["numbers"],

exercises/concept/freelancer-rates/.docs/hints.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## 1. Calculate the day rate given an hourly rate
44

5-
- Use the numeric operators from Lucian's Lucious Lasagna.
5+
- Use the [numeric operators][ref-numeric-operators] from Lucian's Lucious Lasagna.
6+
7+
[ref-numeric-operators]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
68

79
## 2. Calculate the month rate, given an hourly rate and a discount
810

911
- There is a global built-in function to _parse_ a `string` to a fractional number, ignoring non-numeric characters, such as the `%` (percent)-sign.
1012
- `100% - discount` equals the percentage after the discount is applied.
11-
- There is a built-in helper object called [`Math`][ref-math-object] with functions to, for example, round-down (`floor`) or round-up (`ceil`).
13+
- There is a built-in global object called [`Math`][ref-math-object] with functions to, for example, round-down (`floor`) or round-up (`ceil`).
1214

1315
[ref-math-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
1416

exercises/concept/freelancer-rates/.docs/instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Instructions
22

3-
In this exercise you'll be writing code to help a freelancer communicate with a project manager by providing a few utilities to quickly calculate day- and month rates, optionally with a given discount.
3+
In this exercise you will be writing code to help a freelancer communicate with a project manager by providing a few utility functions to quickly calculate day- and month rates, optionally with a given discount.
44

55
We first establish a few rules between the freelancer and the project manager:
66

77
- The daily rate is 8 times the hourly rate;
88
- A month has 22 billable days.
99

10-
The freelancer is offering to apply a discount if the project manager chooses to let the freelancer bill per month, which can come in handy if there is a certain budget the project manager has to work with.
10+
If the freelancer bills the project manager per month, there is a discount applied. This can be handy if the project manager has a fixed budget.
1111

1212
Discounts are modeled as fractional numbers followed by a `%` (percentage) between `0.0%` (no discount) and `90.0%` (maximum discount).
1313

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Introduction
22

3-
There are two different types of numbers in JavaScript:
3+
Many programming languages have specific numeric types to represent different types of numbers, but JavaScript only has two:
44

55
- `number`: a numeric data type in the double-precision 64-bit floating point format (IEEE 754).
66
Examples are `-6`, `-2.4`, `0`, `0.1`, `1`, `3.14`, `16.984025`, `25`, `976`, `1024.0` and `500000`.
77
- `bigint`: a numeric data type that can represent _integers_ in the arbitrary precision format.
88
Examples are `-12n`, `0n`, `4n`, and `9007199254740991n`.
99

10-
In contrast, in many other programming languages different numeric types can exist, for example: Integers, Floats, Doubles, or Bignums.
11-
1210
If you require arbitrary precision or work with extremely large numbers, use the `bigint` type.
1311
Otherwise, the `number` type is likely the better option.

exercises/concept/freelancer-rates/.meta/exemplar.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function dayRate(ratePerHour) {
3333
* Calculates the rate per month
3434
*
3535
* @param {number} ratePerHour
36-
* @param {string} discount for example 20%
36+
* @param {number} discount for example 20% written as 0.2
3737
* @returns {number} the rounded up monthly rate
3838
*/
3939
export function monthRate(ratePerHour, discount) {
@@ -43,25 +43,25 @@ export function monthRate(ratePerHour, discount) {
4343
}
4444

4545
/**
46-
* Calculates the number of days in a budget, rounded on one decimal
46+
* Calculates the number of days in a budget, rounded down
4747
*
4848
* @param {number} budget the total budget
4949
* @param {number} ratePerHour the rate per hour
50-
* @param {string} discount the discount to apply
51-
* @returns {string} the number of days
50+
* @param {number} discount to apply, example 20% written as 0.2
51+
* @returns {number} the number of days
5252
*/
5353
export function daysInBudget(budget, ratePerHour, discount) {
5454
const discounted = applyDiscount(dayRate(ratePerHour), discount);
55-
return (budget / discounted).toFixed(1);
55+
return Math.floor(budget / discounted);
5656
}
5757

5858
/**
5959
* Applies a discount to the value
6060
*
6161
* @param {number} value
62-
* @param {string} percentage a percentage such as '20%'
62+
* @param {number} percentage for example 20% written as 0.2
6363
* @returns {number} the discounted value
6464
*/
6565
function applyDiscount(value, percentage) {
66-
return ((100 - parseFloat(percentage)) / 100) * value;
66+
return (1 - percentage) * value;
6767
}

exercises/concept/freelancer-rates/freelancer-rates.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ export function dayRate(ratePerHour) {
3333
* Calculates the rate per month
3434
*
3535
* @param {number} ratePerHour
36-
* @param {string} discount for example 20%
36+
* @param {number} discount for example 20% written as 0.2
3737
* @returns {number} the rounded up monthly rate
3838
*/
3939
export function monthRate(ratePerHour, discount) {
4040
throw new Error('Implement the monthRate function');
4141
}
4242

4343
/**
44-
* Calculates the number of days in a budget, rounded on one decimal
44+
* Calculates the number of days in a budget, rounded down
4545
*
4646
* @param {number} budget the total budget
4747
* @param {number} ratePerHour the rate per hour
48-
* @param {string} discount the discount to apply
49-
* @returns {string} the number of days
48+
* @param {number} discount to apply, example 20% written as 0.2
49+
* @returns {number} the number of days
5050
*/
5151
export function daysInBudget(budget, ratePerHour, discount) {
5252
throw new Error('Implement the daysInBudget function');
@@ -56,7 +56,7 @@ export function daysInBudget(budget, ratePerHour, discount) {
5656
* Applies a discount to the value
5757
*
5858
* @param {number} value
59-
* @param {string} percentage a percentage such as '20%'
59+
* @param {number} percentage for example 20% written as 0.2
6060
* @returns {number} the discounted value
6161
*/
6262
function applyDiscount(value, percentage) {

0 commit comments

Comments
 (0)