There is some variable with a string, in it you need to delete one character by index (randomly obtained through Math.random() and written to a variable), how to do this if the strings in javascript are of immutable type. The replace() function cannot be done as this function replaces by the value itself and not by the index.

  var string = "Lorem ipsum dolor", a = Math.floor(Math.random()*string.length); 

    1 answer 1

     let string = "Lorem ipsum dolor", pos = Math.floor(Math.random() * string.length); // String.prototype.substr() - Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠ΅ количСство символов ΠΈΠ· строки, // начиная с ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ // https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/substr // ++N - опСрация ΠΏΡ€Π΅ΠΈΠ½ΠΊΡ€Π΅ΠΌΠ΅Π½Ρ‚Π°, ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ N ΠΈ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΏΠΎΡ‚ΠΎΠΌ ΠΎΡ‚Π΄Π°Ρ‘Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ // Π’. Π΅. Ρ‚ΡƒΡ‚ Ρ‚Π°ΠΊΠΈΠ΅ ΠΏΠΈΡ€ΠΎΠΆΠΊΠΈ: ΠΎΡ‚Ρ€Π΅Π·Π°Π΅ΠΌ Ρ‡Π°ΡΡ‚ΡŒ строки Π΄ΠΎ Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ ΠΈ // ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΡŽ с остатком строки взятой со ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅Π³ΠΎ символа Π΄ΠΎ ΠΊΠΎΠ½Ρ†Π° строки let newString = string.substr(0, pos) + string.substr(++pos); 

    • Can you tell us how this works? In particular, this post is not clear: + string.substr (++ pos); - Kardanx
    • @Kardanx, updated the answer. - user207618