Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

squareRoot(6) returns 3, squareRoot(12) returns 4; test expects squareRoot(3, 1) = 1.8 (correct: 1.7)

オープン 初心者向け
#2,231 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
testing-qa

調査の方向性

まず src/algorithms/math/square-root/squareRoot.js と、squareRoot.test.js:30 にある失敗する期待値を確認し、ループの停止条件を issue の例と比較してください。平方根のテストを実行し、squareRoot(3, 1) を含む、列挙された入力を確認してください。結果が正しく丸められ、#2187 で報告されたハングが解消されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

src/algorithms/math/square-root/squareRoot.js stops Newton's iteration when |number - root²| <= 10^-tolerance (line 31). That bounds the error of the square, not of the root. Newton's method approaches √n from above, so the loop stops on an overestimate, and rounding then goes the wrong way.

squareRoot(6);     // 3     expected 2   (√6 = 2.449)
squareRoot(12);    // 4     expected 3   (√12 = 3.464)
squareRoot(3, 1);  // 1.8   expected 1.7 (√3 = 1.732)

squareRoot.test.js:30 asserts expect(squareRoot(3, 1)).toBe(1.8), so the test enshrines the bug. For n = 0..3000 and tolerance 0..6 there are 68 wrong results.

Fix: iterate until the root stops decreasing (while (next < root)), then round. Checked on 259,695 inputs against correctly rounded Math.sqrt; the only mismatches are 53 last-digit float effects at tolerance >= 10. This also removes the hang reported in #2187 (squareRoot(65549, 11)). The #2187 change alone still returns 3 and 1.8.


Found during an independent property-based audit of this repo (full report and reproducible harness: https://github.com/devladpopov/algorithm-autopsy/tree/main/autopsy-02).

主要言語
JavaScript
スター
197k
フォーク
31k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

trekhleb/javascript-algorithms のほかの issue

trekhleb/javascript-algorithms の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。