Repeat a string: Difference between revisions

Content added Content deleted
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
(→‎{{header|JavaScript}}: Updated primitives)
Line 924: Line 924:
See the technique of 'Egyptian Multiplication' described in the Rhind Mathematical Papyrus at the British Museum.
See the technique of 'Egyptian Multiplication' described in the Rhind Mathematical Papyrus at the British Museum.


<lang javascript>function nreps(s, n) {
<lang javascript>(() => {
var o = '';
'use strict';
if (n < 1) return o;
while (n > 1) {
if (n & 1) o += s;
n >>= 1;
s += s;
}
return o + s;
}


// replicate :: Int -> String -> String
nreps('ha', 50000);</lang>
const replicate = (n, s) => {
let v = [s],
o = [];
if (n < 1) return o;
while (n > 1) {
if (n & 1) o = o + v;
n >>= 1;
v = v + v;
}
return o.concat(v);
};


return replicate(5000, "ha")
})();</lang>


=={{header|jq}}==
=={{header|jq}}==