fn
and, for some reason, you want the result to be true
.
fn
and, for some reason, you want the result to be true
.
describe("our function", () => {
it("returns true", () => {
expect(fn()).toBe(true)
})
})
describe
and it
above, I'll give you a function and you’ll tell me if it returns true
when it's called.
function fn() {
return true
}
Of course it is, it's right there.
true
is true.
function fn() {
return true || false
}
Yes, I know that
||
is the logical or operator andtrue
ORfalse
is true.
function fn() {
if (5 > 3) {
return true
}
}
Yes. I know how to count, and I know that 5 is greater than 3, so I know that
fn()
is true.
function fn() {
if (9 > 10) {
return true
} else {
return false
}
}
No, 9 is not greater than 10, so this will be false and false is not
true
.
function fn() {
if (9 > 10) {
return true
}
}
That's pretty much the same code. There is no
else
, so we'll get to the end and return undefined - which is not true.
function fn() {
for (let i = 0; i < 10; i++) {
if (i > 8) {
return true
}
}
}
Yes, when
i
becomes9
, the result becomestrue
.
function fn() {
for (let i = 0; i > -1; i++) {
// Do nothing
}
return true
}
That for loop doesn't do anything, so this must be true.
…no. I guess not:
i
will always be> -1
, forever and ever.
true
?
Never. So this is not true.
I can say with certainty that
fn()
is not true.
function fn() {
for (let i = 0; i < 10000000000000; i++) {}
return true
}
Eventually. It will take quite a bit I think, but it will be true.
function fn() {
for (let i = 1; i % 2 == 1; i += 2) {}
return true
}
Hey I recognize
%
, that's the modulo operator. We'll loop as long asi
is odd (that is, when divided by two the remainder is one), so this will stop when i is even. This must be true.
i
on each iteration?
On second thought, we start it at 1 and add 2 to it each time. 1, 3, 5, 7, etc. All odds. The return value will never become true, so this is not true.
Am I destined to parse loops in my head forever?
function fn() {
let bigNumber = 1000000000000000000000000
for (let count = 1; count < bigNumber; count++) {
let n = count
while (n > 1) {
if (n % 2 == 0) {
n = n / 2
} else {
n = 3 * n + 1
}
}
}
return true
}
I think
bigNumber
is too big for JavaScript.
Well of course you'll allow it - you make the rules here. Okay but what’s all the math inside the while loop?
n
is odd we make it 3n+1
, if n
is even we make it n/2
. We stop when n
equals 1.
Got it. So we count up to
bigNumber
and do a bunch of pointless math on each iteration. We return true at the end so this must be true.
You said "we stop when n equals 1."
n
always reach 1 at some point?
Well I ran it for
bigNumber
set to 100000 and it took a bit, but it got there. I'm not entirely su– wait why is it called a "conjecture"?
Oh so n doesn't always get to 1. This must not be true then.
n
does not always get to 1.
Okay so I'm not sure if
fn()
is true, but I’m also not sure iffn()
is not true.
I don't… I don’t know. Do I? What am I? Can I exist?
I guess I need to be able to solve the Collatz conjecture to decide. I suppose it’s only a matter of time until we figure out if the conjecture is true or not.
Thank heavens.
Can I please have a break? I'm beginning to question whether or not I can exist.
Okay, that makes me feel better, thank you.
function fn() {
for (let n = 3; ; n += 2) {
let sumOfFactors = 0
for (let i = 1; i < n; i++) {
if (n % i === 0) {
sumOfFactors += i
}
}
if (sumOfFactors === n) {
return true
}
}
}
Oh goodness what do we have here. It looks like we're summing up factors (numbers less than
n
that divide inton
)… but only for the odd numbers? And we want to check if the sum of those factors is the original number.
I believe these are called Perfect numbers. But I don't understand why we start at 3 and increment by 2.
You're losing me. Look, I’m not interested in solving your silly math problems. You said I exist, so while I don’t know if this function is true or not, I know I can figure it out and give you a yes or no answer.
It… is? I mean “is this true” seems like a pretty clear-cut yes or no question.
returnsTrue
.
I like the sound of that.
What?
function halts(program) {
let fn = () => {
eval(program)
return true
}
return returnsTrue(fn)
}
Wait, you can't write
halts
. This is the Halting problem and the The Halting Problem cannot be solved.
It won't work. Nice try.
What if the program loops forever?
But… you can't determine if an arbitrary program will halt.
That's not possible. You can’t just do that.
Hello?Are you still there?Do you have more problems for me?Hello?