diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js index 791cfe7a2..949e50b04 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -13,7 +13,7 @@ function randomNumbers(n) { } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; @@ -24,7 +24,7 @@ function randomNumbers(n) { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } } diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index 68ed9bc35..a7318634f 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -13,7 +13,7 @@ function randomNumbers(n: number): number[] { } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; @@ -24,7 +24,7 @@ function randomNumbers(n: number): number[] { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } }