Home > Enterprise >  Insert * every 5th character
Insert * every 5th character

Time:09-30

How to write the number from 1 to 1000 on the screen like the following : 1 2 3 4 5 * 6 7 8 9 10 * 11 12 13 14 15 * 16 17 18 19 20 *...

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:

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:

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:

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 add 1000 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:

Using flatMap to insert * after every 5th elements. Then join up the numbers.

Array.from(Array(1000))
  .flatMap((_, i) => (  i, i%5 ? [i] : [i, '*']))
    .join(' ')

// '1 2 3 4 5 * 6 7 8 9 10 * 11 12 13 14 15 * …'

Of course there is also reduce: (will add a trailing space!)

Array.from(Array(1000))
  .reduce((s, _, i) =>
    (  i, s (i%5 ? i ' ' : i ' * ')), '')

If we wanted to golf it a little bit we could just do:

Array.from(Array(1000),
  (_, i) => (  i, i%5 ? i : i ' *'))
    .join(' ')

How to avoid the trailing star?

Most answers (including mine) will leave you with a trailing star e.g. '… 1000 *'.

To avoid that we can instead loop 200 times and produce the next five numbers at each iteration:

  • 0 -> [0*5 1, 0*5 2, 0*5 3, 0*5 4, 0*5 5] -> [1, 2, 3, 4, 5]
  • 1 -> [1*5 1, 1*5 2, 1*5 3, 1*5 4, 1*5 5] -> [6, 7, 8, 9, 10]
  • 2 -> [2*5 1, 2*5 2, 2*5 3, 2*5 4, 2*5 5] -> [11, 12, 13, 14, 15]

We don't need those intermediate arrays so we can join them up in strings immediately. We now have 200 strings (group of five numbers each) that we can join with a star:

Array.from(Array(200),
  (_, i) => (i*=5, `${i 1} ${i 2} ${i 3} ${i 4} ${i 5}`))
    .join(' * ')

CodePudding user response:

let str = '';
for(let i = 1; i <=1000;i  ){
  str =i;
  if(i % 5 ===0) str ='*';
}
console.log(str);

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)

  • Related