douglascrockford / howjavascriptworks

Programs from the book _How JavaScript Works_
319 stars 53 forks source link

Fix error in string() function #1

Closed stefanhamburger closed 6 years ago

stefanhamburger commented 6 years ago

There is an error in the string() function from the big_rational library (in the book on page 5.7).

In the first branch of the if statement, the nr_places argument is expected to be defined, so the condition must be negated.

Example code to reproduce (throws "TypeError: big is undefined" but prints the correct values with this fix):

import big_integer from './big_integer.js';
import big_rational from './big_rational.js';

const eight_thirds = big_rational.make(8, 3);
console.log(big_rational.string(eight_thirds)); // 2 2/3
console.log(big_rational.string(eight_thirds, big_integer.make(2))); // 2.67

const ten_thirds = big_rational.make(10, 3);
console.log(big_rational.string(ten_thirds)); // 3 1/3
console.log(big_rational.string(ten_thirds, big_integer.make(2))); // 3.33
douglascrockford commented 6 years ago

Thank you so much.