skip to Main Content

In the first line, the input is the line d. In the second line, a series of words separated by a dot is entered. You need to output these words by separating each word with a combination of d characters. (В первой строке на вход подается строка d. Во второй строке вводится ряд слов через точку. Вам нужно вывести эти слова, разделяя каждое слово сочетанием символов d.)
example:
Sample Input: d= /|
dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe
Sample Output:
dej/|fow/|orj/|jkk/|fgo/|pwk/|pkp/|lde/|efe

    let d=prompt();
    let b=prompt();
    let arr=b.split('');
    for(let i in arr) {
        if(i.indexOf('.') !== -1){
            arr = i.replace(i.indexOf('.'), d)
      }
    }
    arr = arr.join()
    console.log(arr)`

It doesn’t work!

4

Answers


  1. Concatenate characters with replacing the dot:

    const d = "/|\";
    const input = "dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe";
    
    let result = '';
    const arr = input.split('');
    for(let i = 0; i<arr.length;i++){
      result += arr[i] === '.' ?  '/|\' : arr[i];
    }
    
    console.log(result);

    Array::reduce() version:

    const d = "/|\";
    const input = "dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe";
    
    const result = input.split('').reduce((r, c) => r += c === '.' ?  '/|\' : c, '');
    
    console.log(result);

    A benchmark:

    Cycles: 1000000 / Chrome/117
    -------------------------------------------------------
    Alexander   142/min  1.0x  151  145  146  158  142  187
    Roco        168/min  1.2x  193  181  168  176  197  186
    -------------------------------------------------------
    https://github.com/silentmantra/benchmark
    
    <script benchmark="1000000">
    
    // @benchmark Roco
    'dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe'.replace(/./g, '/|\')
    
    // @benchmark Alexander
    let output = '';
    const arr = 'dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe'.split('');
    for(let i = 0; i<arr.length;i++){
      output += arr[i] === '.' ?  '/|\' : arr[i];
    }
    output;
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
  2. Escape the special characters (escape) and Regex’s . (any character) using backslash: \ .

    const d = "/|\";
    const input = "dej.fow.orj.jkk.fgo.pwk.pkp.lde.efe";
    const result = input.replace(/./g, d);
    console.log(result)
    Login or Signup to reply.
  3. Mr . has a blue house and a . car.

    let str = document.getElementById("demo").innerHTML;
    let res = str.replace(/./g, "d");

    OutPut:
    Mr d has a blue house and a d car.

    Login or Signup to reply.
  4. It seems like you’re trying to replace the dots in the input string b with the character d. However, there are a few issues in your code. You can achieve this by using the split and join methods correctly. Here’s the corrected code:

    let d = prompt();
    let b = prompt();
    let arr = b.split('.'); // Split the input string by dots
    
    // Join the array elements with the character 'd'
    let result = arr.join(d);
    
    console.log(result);
    

    With this code, it will correctly split the input string b by dots and then join the elements with the character d, giving you the desired output.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search