Created
November 5, 2020 17:00
-
-
Save caasi/5c9bc8ff4204783d2efc3669c71e26c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 沒限制 | |
function g(a: number[], p: (x: number) => boolean) { | |
let i; | |
for (i = 0; i < a.length; ++i) { | |
if (p(a[i])) return i; // early return | |
} | |
return i; | |
} | |
// 有限制 | |
function h(a: number[], p: (x: number) => boolean) { | |
let i; | |
let n; | |
for (n = a.length, i = n - 1; i > 0; --i) { | |
if (p(a[i])) n = i; | |
} | |
return n; | |
} | |
const a: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
function f(t: number): number { | |
return Math.sqrt(Math.abs(t)) + 5 * Math.pow(t, 3); | |
} | |
console.log(g(a, (x: number) => f(x) > 400)); | |
console.log(h(a, (x: number) => f(x) > 400)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment