Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Binary-digits/JavaScript/binary-digits-1.js
Normal file
9
Task/Binary-digits/JavaScript/binary-digits-1.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function toBinary(number) {
|
||||
return new Number(number)
|
||||
.toString(2);
|
||||
}
|
||||
var demoValues = [5, 50, 9000];
|
||||
for (var i = 0; i < demoValues.length; ++i) {
|
||||
// alert() in a browser, wscript.echo in WSH, etc.
|
||||
print(toBinary(demoValues[i]));
|
||||
}
|
||||
24
Task/Binary-digits/JavaScript/binary-digits-2.js
Normal file
24
Task/Binary-digits/JavaScript/binary-digits-2.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ------------------ BINARY DIGITS ------------------
|
||||
|
||||
// showBinary :: Int -> String
|
||||
const showBinary = n =>
|
||||
showIntAtBase_(2)(n);
|
||||
|
||||
|
||||
// showIntAtBase_ :: // Int -> Int -> String
|
||||
const showIntAtBase_ = base =>
|
||||
n => n.toString(base);
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () => [5, 50, 9000]
|
||||
.map(n => `${n} -> ${showBinary(n)}`)
|
||||
.join("\n");
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
56
Task/Binary-digits/JavaScript/binary-digits-3.js
Normal file
56
Task/Binary-digits/JavaScript/binary-digits-3.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// -------------- DIGITS FOR GIVEN BASE --------------
|
||||
|
||||
// showIntAtBase :: Int -> (Int -> Char) ->
|
||||
// Int -> String -> String
|
||||
const showIntAtBase = base =>
|
||||
// A string representation of n, in the given base,
|
||||
// using a supplied (Int -> Char) function for digits,
|
||||
// and a supplied suffix string.
|
||||
toChr => n => rs => {
|
||||
const go = ([x, d], r) => {
|
||||
const r_ = toChr(d) + r;
|
||||
|
||||
return 0 !== x ? (
|
||||
go(quotRem(x)(base), r_)
|
||||
) : r_;
|
||||
};
|
||||
|
||||
const e = "error: showIntAtBase applied to";
|
||||
|
||||
return 1 >= base ? (
|
||||
`${e} unsupported base`
|
||||
) : 0 > n ? (
|
||||
`${e} negative number`
|
||||
) : go(quotRem(n)(base), rs);
|
||||
};
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () => {
|
||||
// showHanBinary :: Int -> String
|
||||
const showHanBinary = n =>
|
||||
showIntAtBase(2)(
|
||||
x => "〇一" [x]
|
||||
)(n)("");
|
||||
|
||||
return [5, 50, 9000]
|
||||
.map(
|
||||
n => `${n} -> ${showHanBinary(n)}`
|
||||
)
|
||||
.join("\n");
|
||||
};
|
||||
|
||||
|
||||
// --------------------- GENERIC ---------------------
|
||||
|
||||
// quotRem :: Integral a => a -> a -> (a, a)
|
||||
const quotRem = m =>
|
||||
// The quotient, tupled with the remainder.
|
||||
n => [Math.trunc(m / n), m % n];
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue