Platform
Resources
Pricing
Sign in
Contact us
Harry Stevens
News and graphics at @ClimateLab, a project of @PostGraphics.
Workspace
Fork
Public
Math
By
Harry Stevens
Edited
Mar 10
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
divs
=
result
.
map
(
(
{
hi2lo
,
lo2hi
,
answer
}
,
i
,
e
)
=>
{
const
color
=
i
===
e
.
length
-
1
&&
answer
===
6174
?
"red"
:
"black"
;
return
html
`<div>${
format
(
hi2lo
)
} - ${
format
(
lo2hi
)
} = <span style="color: ${
color
}">${
format
(
answer
)
}</span></div>`
;
}
)
Insert cell
result
=
{
let
n
=
number
;
let
attempts
=
0
;
let
result
=
0
;
const
rounds
=
[
]
;
while
(
result
!==
6174
&&
attempts
<
8
)
{
const
p
=
process
(
n
)
;
result
=
p
.
answer
;
n
=
result
;
rounds
.
push
(
p
)
;
attempts
++
;
}
return
rounds
;
}
Insert cell
function
format
(
n
)
{
const
s
=
n
.
toString
(
)
;
const
l
=
s
.
length
;
const
spaces
=
l
===
4
?
""
:
Array
.
from
(
{
length
:
5
-
l
}
)
.
map
(
(
)
=>
" "
)
.
join
(
""
)
;
return
`${
spaces
}${
commas
(
s
)
}`
}
Insert cell
function
process
(
integer
)
{
const
string
=
integer
.
toString
(
)
;
let
lo2hi
=
string
.
split
(
""
)
.
sort
(
)
;
const
hi2lo
=
+
lo2hi
.
slice
(
)
.
reverse
(
)
.
join
(
""
)
;
lo2hi
=
+
lo2hi
.
join
(
""
)
;
return
{
hi2lo
,
lo2hi
,
answer
:
hi2lo
-
lo2hi
}
;
}
Insert cell
function
commas
(
x
)
{
return
x
.
replace
(
/\B(?=(\d{3})+(?!\d))/g
,
","
)
;
}
;
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
number
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
random
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
divs
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
result
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
format
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
process
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
commas
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML