I am trying to generate a string with the numbers from 1 to 1000 with the character '*'
after every 5th numbers e.g.,
1 2 3 4 5 * 6 7 8 9 10 * 11 12 13 14 15 * 16 17 18 19 20 * …
Here's my attempt but as you can it is not working as expected:
const insertCharacter = () => {
let contain = [];
let new_value;
for (let i = 1; i <= 1000; i ) {
contain = [i];
if (contain.length)
contain.toString()
let parts = contain.match(/.{0,5}/g);
new_value = parts.join("*");
}
return new_value;
}
console.log(insertCharacter());
CodePudding user response:
I would suggest using the modulus here:
var output = "";
for (var i=1; i <= 1000; i) {
if (i > 1 && i % 5 == 1) output = " *";
if (i > 1) output = " ";
output = i;
}
console.log(output);
CodePudding user response:
Please refer this code.
const result = [...Array(1000)].map((val, index) => (index 1) % 5 === 0 ? index 1 " *" : index 1);
console.log(result.join(' '));
CodePudding user response:
Older JavaScript format version, but still valid.
The %
is called a Remainder and is the key here.
Sample code is only counting to 50.
Edit: Changed to commented version from mplungjan.
Edit 2, for comment from georg. If you do not want a trailing Asterisk, some options:
- count to
999
, then add1000
to the result - use
result.substring(0,result.length-2)
var i, result = "";
for(i=1;i<51;i ){
result = i (i % 5 ? " " : " * ");
}
console.log(result);
CodePudding user response:
Assuming you don't want the trailing character, you could simply do:
Array.from(Array(1000 / 5),
(_, i) => (i *= 5, `${i 1} ${i 2} ${i 3} ${i 4} ${i 5}`))
.join(' * ');
We don't need to iterate 1000 times: we know all the numbers already and we know we'll have 200 groups of 5 numbers each. We just need to produce those groups and join them up together.
But there are many issues with this:
- The interval is hardcoded
- The separator is hardcoded
- What if we can't split evenly the numbers?
Let say we need to insert a |
after every 4th numbers of 10 numbers:
1 2 3 4 | 5 6 7 8 | 9 10
We have 2 groups of 4 numbers each and 1 group with the last two numbers.
(I am going to annotate these examples so you can hopefully connect them with the full example below)
The first two groups can be produced as follow:
Array.from(Array(Math.floor(10 / 4)), (_, i) => (i*=4, [i 1, i 2, i 3, i 4].join(' ')))
// ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
// imax tmpl
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// nseq
//
//=> ['1 2 3 4', '5 6 7 8']
The last group with:
Array(10 % 4).fill(0).map((_, i) => Math.floor(10 / 4) * 4 i 1).join(' ')
// ^^^^^^ ^^^^^^^^^^^^^^^^^^
// tmax imax
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// nseq
//
//=> '9 10'
Then you basically do:
['1 2 3 4', '5 6 7 8'].concat('9 10').join(' | ')
//=> '1 2 3 4 | 5 6 7 8 | 9 10'
Demo
console.log("sprintnums(10, 7, '|') -> " sprintnums(10, 7, '|'));
console.log("sprintnums(10, 4, '|') -> " sprintnums(10, 4, '|'));
console.log("sprintnums(10, 2, '|') -> " sprintnums(10, 2, '|'));
console.log("sprintnums(10, 1, '|') -> " sprintnums(10, 1, '|'));
console.log(`
In your case:
${sprintnums(1000, 5, '*')}
`);
<script>
function sprintnums(n, x, c) {
const tmpl = Array(x).fill(0);
const nseq = (arr, mul) => arr.map((_, i) => mul * x i 1).join(' ');
const imax = Math.floor(n / x);
const tmax = n % x;
const init = Array.from(Array(imax), (_, mul) => nseq(tmpl, mul));
const tail = tmax ? nseq(Array(tmax).fill(0), imax) : [];
return init.concat(tail).join(` ${c} `);
}
</script>
CodePudding user response:
check this code
function insertChar() {
let res = '';
for (let i = 0; i < 100; i ) {
res = i;
if (i % 5 == 0) {
res = '*'
}
}
return res;
}
console.log(insertChar());
CodePudding user response:
let str = '';
for(let i = 1; i <=1000;i ){
str =i;
if(i - 1 % 5 ===0) str ='*';
}
console.log(str)