September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,7 +1,9 @@
|
|||
function toBinary(number) {
|
||||
return new Number(number).toString(2);
|
||||
return new Number(number)
|
||||
.toString(2);
|
||||
}
|
||||
var demoValues = [5, 50, 9000];
|
||||
for (var i=0; i<demoValues.length; ++i) {
|
||||
print(toBinary(demoValues[i])); // alert() in a browser, wscript.echo in WSH, etc.
|
||||
for (var i = 0; i < demoValues.length; ++i) {
|
||||
// alert() in a browser, wscript.echo in WSH, etc.
|
||||
print(toBinary(demoValues[i]));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,30 @@
|
|||
console.log(
|
||||
(() => {
|
||||
|
||||
[5, 50, 9000].map(function (n) {
|
||||
return (n).toString(2);
|
||||
}).join('\n')
|
||||
// showIntAtBase_ :: // Int -> Int -> String
|
||||
const showIntAtBase_ = (base, n) => (n)
|
||||
.toString(base);
|
||||
|
||||
)
|
||||
// showBin :: Int -> String
|
||||
const showBin = n => showIntAtBase_(2, n);
|
||||
|
||||
// GENERIC FUNCTIONS FOR TEST ---------------------------------------------
|
||||
|
||||
// intercalate :: String -> [a] -> String
|
||||
const intercalate = (s, xs) => xs.join(s);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
|
||||
return unlines(map(
|
||||
n => intercalate(' -> ', [show(n), showBin(n)]),
|
||||
[5, 50, 9000]
|
||||
));
|
||||
})();
|
||||
|
|
|
|||
47
Task/Binary-digits/JavaScript/binary-digits-3.js
Normal file
47
Task/Binary-digits/JavaScript/binary-digits-3.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(() => {
|
||||
|
||||
// showBin :: Int -> String
|
||||
const showBin = n => {
|
||||
const binaryChar = n => n !== 0 ? '一' : '〇';
|
||||
|
||||
return showIntAtBase(2, binaryChar, n, '');
|
||||
};
|
||||
|
||||
// showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String
|
||||
const showIntAtBase = (base, toChr, n, rs) => {
|
||||
const showIt = ([n, d], r) => {
|
||||
const r_ = toChr(d) + r;
|
||||
return n !== 0 ? (
|
||||
showIt(quotRem(n, base), r_)
|
||||
) : r_;
|
||||
};
|
||||
return base <= 1 ? (
|
||||
'error: showIntAtBase applied to unsupported base'
|
||||
) : n < 0 ? (
|
||||
'error: showIntAtBase applied to negative number'
|
||||
) : showIt(quotRem(n, base), rs);
|
||||
};
|
||||
|
||||
// quotRem :: Integral a => a -> a -> (a, a)
|
||||
const quotRem = (m, n) => [Math.floor(m / n), m % n];
|
||||
|
||||
// GENERIC FUNCTIONS FOR TEST ---------------------------------------------
|
||||
|
||||
// intercalate :: String -> [a] -> String
|
||||
const intercalate = (s, xs) => xs.join(s);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
|
||||
return unlines(map(
|
||||
n => intercalate(' -> ', [show(n), showBin(n)]), [5, 50, 9000]
|
||||
));
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue