Creating a wave of string in Javascript












7















I can't seem to figure it out how to make a wave from a string in Javascript.



Rules:




  1. The input will always be lower case string.

  2. Ignore whitespace.


Expected result:



wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]

wave ("") =>


This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2). Because of BIG O Scalability.



const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");









share|improve this question

























  • Shouldn't you convert some characters to upper case?

    – Aycan Yaşıt
    Mar 4 at 12:28
















7















I can't seem to figure it out how to make a wave from a string in Javascript.



Rules:




  1. The input will always be lower case string.

  2. Ignore whitespace.


Expected result:



wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]

wave ("") =>


This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2). Because of BIG O Scalability.



const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");









share|improve this question

























  • Shouldn't you convert some characters to upper case?

    – Aycan Yaşıt
    Mar 4 at 12:28














7












7








7


1






I can't seem to figure it out how to make a wave from a string in Javascript.



Rules:




  1. The input will always be lower case string.

  2. Ignore whitespace.


Expected result:



wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]

wave ("") =>


This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2). Because of BIG O Scalability.



const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");









share|improve this question
















I can't seem to figure it out how to make a wave from a string in Javascript.



Rules:




  1. The input will always be lower case string.

  2. Ignore whitespace.


Expected result:



wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]

wave ("") =>


This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2). Because of BIG O Scalability.



const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");






javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 4 at 12:24









vahdet

2,06431436




2,06431436










asked Mar 4 at 12:21









Arnas DičkusArnas Dičkus

8719




8719













  • Shouldn't you convert some characters to upper case?

    – Aycan Yaşıt
    Mar 4 at 12:28



















  • Shouldn't you convert some characters to upper case?

    – Aycan Yaşıt
    Mar 4 at 12:28

















Shouldn't you convert some characters to upper case?

– Aycan Yaşıt
Mar 4 at 12:28





Shouldn't you convert some characters to upper case?

– Aycan Yaşıt
Mar 4 at 12:28












5 Answers
5






active

oldest

votes


















8














You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.






function wave(string) {
var result = ,
i;

for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}

console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); //

.as-console-wrapper { max-height: 100% !important; top: 0; }








share|improve this answer































    1














    This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):






    const wave = (str = '') => {
    return str
    .split('')
    .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
    }

    console.log(wave('wave'));
    console.log(wave('foo bar'));
    console.log(wave());








    share|improve this answer































      1














      You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string






      var array=;
      const wave = (str) => {
      if(typeof str === 'string' && str === str.toLowerCase()){
      for (let index = 0; index < str.length; index++) {
      if (str[index] === ' ') continue;
      waveChar=str.charAt(index).toUpperCase();
      preStr=str.substring(0,index);
      postStr=str.substring(index,str.length-1);
      array.push(preStr+waveChar+postStr);
      }

      }else{
      alert(`${str} is either not a string or not lowercase`);
      }
      }
      wave("hello");
      console.log(array);








      share|improve this answer































        0














        I use tradicional js. This works on 99% off today browsers.



        Where answer very pragmatic. I use array access for string ;



        Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
        Make it inverse always when we call this line.






        var index = 0;
        var mydata= "hello";

        function wave (str){

        var FINAL = ;
        var newString = "";

        for (var j =0; j < str.length; j++) {
        for (var x =0; x < str.length; x++) {

        var rez = "";
        if (x == index) {
        rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
        } else {
        rez = str[x];
        }
        newString += rez ;
        }
        FINAL.push(newString);
        newString = "";
        index++;
        }

        return FINAL;
        }

        var rezArray = wave(mydata);

        console.log(rezArray);

        // Just for teory
        console.log(Array.from([1, 2, 3], x => console.log(x)));








        share|improve this answer


























        • Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

          – Arnas Dičkus
          Mar 4 at 13:12











        • You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

          – Nikola Lukic
          Mar 4 at 13:18











        • What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

          – Nikola Lukic
          Mar 4 at 13:19






        • 1





          Thanks, for explanation I wasn't aware of this.

          – Arnas Dičkus
          Mar 4 at 13:24






        • 2





          "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

          – Bergi
          Mar 4 at 18:14



















        0














        I use modify of String prototype :



        for implementation of replaceAt .






        // Modify prototype
        String.prototype.replaceAt=function(index, replacement) {
        return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
        }

        function WaveFunction(str) {

        var base = str;
        var R = ;
        var n =str.length;

        for (var i=0 ; i<n ;i++){

        str = base;
        var c=str.charAt(i);
        var res = c.toUpperCase();
        // console.log(res);
        str = str.replaceAt(i, res);

        R.push(str);
        }
        return R;
        }

        var REZ = WaveFunction("hello");

        console.log(REZ);








        share|improve this answer





















        • 1





          Nice solution with replaceAt !

          – Nikola Lukic
          Mar 4 at 13:23











        • where is replaceAt from?

          – Nina Scholz
          Mar 4 at 13:39











        • @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

          – Arnas Dičkus
          Mar 4 at 13:48






        • 1





          String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

          – Peter Hassaballah
          Mar 4 at 14:22






        • 1





          Don't modify objects you don't own.

          – Emile Bergeron
          Mar 4 at 14:51











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54983179%2fcreating-a-wave-of-string-in-javascript%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8














        You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.






        function wave(string) {
        var result = ,
        i;

        for (i = 0; i < string.length; i++) {
        if (string[i] === ' ') continue;
        result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
        }
        return result;
        }

        console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
        console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
        console.log(wave("")); //

        .as-console-wrapper { max-height: 100% !important; top: 0; }








        share|improve this answer




























          8














          You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.






          function wave(string) {
          var result = ,
          i;

          for (i = 0; i < string.length; i++) {
          if (string[i] === ' ') continue;
          result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
          }
          return result;
          }

          console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
          console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
          console.log(wave("")); //

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer


























            8












            8








            8







            You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.






            function wave(string) {
            var result = ,
            i;

            for (i = 0; i < string.length; i++) {
            if (string[i] === ' ') continue;
            result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
            }
            return result;
            }

            console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
            console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
            console.log(wave("")); //

            .as-console-wrapper { max-height: 100% !important; top: 0; }








            share|improve this answer













            You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.






            function wave(string) {
            var result = ,
            i;

            for (i = 0; i < string.length; i++) {
            if (string[i] === ' ') continue;
            result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
            }
            return result;
            }

            console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
            console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
            console.log(wave("")); //

            .as-console-wrapper { max-height: 100% !important; top: 0; }








            function wave(string) {
            var result = ,
            i;

            for (i = 0; i < string.length; i++) {
            if (string[i] === ' ') continue;
            result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
            }
            return result;
            }

            console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
            console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
            console.log(wave("")); //

            .as-console-wrapper { max-height: 100% !important; top: 0; }





            function wave(string) {
            var result = ,
            i;

            for (i = 0; i < string.length; i++) {
            if (string[i] === ' ') continue;
            result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
            }
            return result;
            }

            console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
            console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
            console.log(wave("")); //

            .as-console-wrapper { max-height: 100% !important; top: 0; }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 4 at 12:32









            Nina ScholzNina Scholz

            190k15100174




            190k15100174

























                1














                This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):






                const wave = (str = '') => {
                return str
                .split('')
                .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                }

                console.log(wave('wave'));
                console.log(wave('foo bar'));
                console.log(wave());








                share|improve this answer




























                  1














                  This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):






                  const wave = (str = '') => {
                  return str
                  .split('')
                  .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                  }

                  console.log(wave('wave'));
                  console.log(wave('foo bar'));
                  console.log(wave());








                  share|improve this answer


























                    1












                    1








                    1







                    This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):






                    const wave = (str = '') => {
                    return str
                    .split('')
                    .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                    }

                    console.log(wave('wave'));
                    console.log(wave('foo bar'));
                    console.log(wave());








                    share|improve this answer













                    This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):






                    const wave = (str = '') => {
                    return str
                    .split('')
                    .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                    }

                    console.log(wave('wave'));
                    console.log(wave('foo bar'));
                    console.log(wave());








                    const wave = (str = '') => {
                    return str
                    .split('')
                    .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                    }

                    console.log(wave('wave'));
                    console.log(wave('foo bar'));
                    console.log(wave());





                    const wave = (str = '') => {
                    return str
                    .split('')
                    .map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
                    }

                    console.log(wave('wave'));
                    console.log(wave('foo bar'));
                    console.log(wave());






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 4 at 12:37









                    zvonazvona

                    12.6k14059




                    12.6k14059























                        1














                        You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string






                        var array=;
                        const wave = (str) => {
                        if(typeof str === 'string' && str === str.toLowerCase()){
                        for (let index = 0; index < str.length; index++) {
                        if (str[index] === ' ') continue;
                        waveChar=str.charAt(index).toUpperCase();
                        preStr=str.substring(0,index);
                        postStr=str.substring(index,str.length-1);
                        array.push(preStr+waveChar+postStr);
                        }

                        }else{
                        alert(`${str} is either not a string or not lowercase`);
                        }
                        }
                        wave("hello");
                        console.log(array);








                        share|improve this answer




























                          1














                          You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string






                          var array=;
                          const wave = (str) => {
                          if(typeof str === 'string' && str === str.toLowerCase()){
                          for (let index = 0; index < str.length; index++) {
                          if (str[index] === ' ') continue;
                          waveChar=str.charAt(index).toUpperCase();
                          preStr=str.substring(0,index);
                          postStr=str.substring(index,str.length-1);
                          array.push(preStr+waveChar+postStr);
                          }

                          }else{
                          alert(`${str} is either not a string or not lowercase`);
                          }
                          }
                          wave("hello");
                          console.log(array);








                          share|improve this answer


























                            1












                            1








                            1







                            You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string






                            var array=;
                            const wave = (str) => {
                            if(typeof str === 'string' && str === str.toLowerCase()){
                            for (let index = 0; index < str.length; index++) {
                            if (str[index] === ' ') continue;
                            waveChar=str.charAt(index).toUpperCase();
                            preStr=str.substring(0,index);
                            postStr=str.substring(index,str.length-1);
                            array.push(preStr+waveChar+postStr);
                            }

                            }else{
                            alert(`${str} is either not a string or not lowercase`);
                            }
                            }
                            wave("hello");
                            console.log(array);








                            share|improve this answer













                            You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string






                            var array=;
                            const wave = (str) => {
                            if(typeof str === 'string' && str === str.toLowerCase()){
                            for (let index = 0; index < str.length; index++) {
                            if (str[index] === ' ') continue;
                            waveChar=str.charAt(index).toUpperCase();
                            preStr=str.substring(0,index);
                            postStr=str.substring(index,str.length-1);
                            array.push(preStr+waveChar+postStr);
                            }

                            }else{
                            alert(`${str} is either not a string or not lowercase`);
                            }
                            }
                            wave("hello");
                            console.log(array);








                            var array=;
                            const wave = (str) => {
                            if(typeof str === 'string' && str === str.toLowerCase()){
                            for (let index = 0; index < str.length; index++) {
                            if (str[index] === ' ') continue;
                            waveChar=str.charAt(index).toUpperCase();
                            preStr=str.substring(0,index);
                            postStr=str.substring(index,str.length-1);
                            array.push(preStr+waveChar+postStr);
                            }

                            }else{
                            alert(`${str} is either not a string or not lowercase`);
                            }
                            }
                            wave("hello");
                            console.log(array);





                            var array=;
                            const wave = (str) => {
                            if(typeof str === 'string' && str === str.toLowerCase()){
                            for (let index = 0; index < str.length; index++) {
                            if (str[index] === ' ') continue;
                            waveChar=str.charAt(index).toUpperCase();
                            preStr=str.substring(0,index);
                            postStr=str.substring(index,str.length-1);
                            array.push(preStr+waveChar+postStr);
                            }

                            }else{
                            alert(`${str} is either not a string or not lowercase`);
                            }
                            }
                            wave("hello");
                            console.log(array);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 4 at 12:40









                            Code_ModeCode_Mode

                            1,212816




                            1,212816























                                0














                                I use tradicional js. This works on 99% off today browsers.



                                Where answer very pragmatic. I use array access for string ;



                                Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
                                Make it inverse always when we call this line.






                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));








                                share|improve this answer


























                                • Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                  – Arnas Dičkus
                                  Mar 4 at 13:12











                                • You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                  – Nikola Lukic
                                  Mar 4 at 13:18











                                • What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                  – Nikola Lukic
                                  Mar 4 at 13:19






                                • 1





                                  Thanks, for explanation I wasn't aware of this.

                                  – Arnas Dičkus
                                  Mar 4 at 13:24






                                • 2





                                  "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                  – Bergi
                                  Mar 4 at 18:14
















                                0














                                I use tradicional js. This works on 99% off today browsers.



                                Where answer very pragmatic. I use array access for string ;



                                Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
                                Make it inverse always when we call this line.






                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));








                                share|improve this answer


























                                • Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                  – Arnas Dičkus
                                  Mar 4 at 13:12











                                • You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                  – Nikola Lukic
                                  Mar 4 at 13:18











                                • What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                  – Nikola Lukic
                                  Mar 4 at 13:19






                                • 1





                                  Thanks, for explanation I wasn't aware of this.

                                  – Arnas Dičkus
                                  Mar 4 at 13:24






                                • 2





                                  "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                  – Bergi
                                  Mar 4 at 18:14














                                0












                                0








                                0







                                I use tradicional js. This works on 99% off today browsers.



                                Where answer very pragmatic. I use array access for string ;



                                Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
                                Make it inverse always when we call this line.






                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));








                                share|improve this answer















                                I use tradicional js. This works on 99% off today browsers.



                                Where answer very pragmatic. I use array access for string ;



                                Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
                                Make it inverse always when we call this line.






                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));








                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));





                                var index = 0;
                                var mydata= "hello";

                                function wave (str){

                                var FINAL = ;
                                var newString = "";

                                for (var j =0; j < str.length; j++) {
                                for (var x =0; x < str.length; x++) {

                                var rez = "";
                                if (x == index) {
                                rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
                                } else {
                                rez = str[x];
                                }
                                newString += rez ;
                                }
                                FINAL.push(newString);
                                newString = "";
                                index++;
                                }

                                return FINAL;
                                }

                                var rezArray = wave(mydata);

                                console.log(rezArray);

                                // Just for teory
                                console.log(Array.from([1, 2, 3], x => console.log(x)));






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 4 at 13:18

























                                answered Mar 4 at 13:08









                                Nikola LukicNikola Lukic

                                1,75721937




                                1,75721937













                                • Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                  – Arnas Dičkus
                                  Mar 4 at 13:12











                                • You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                  – Nikola Lukic
                                  Mar 4 at 13:18











                                • What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                  – Nikola Lukic
                                  Mar 4 at 13:19






                                • 1





                                  Thanks, for explanation I wasn't aware of this.

                                  – Arnas Dičkus
                                  Mar 4 at 13:24






                                • 2





                                  "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                  – Bergi
                                  Mar 4 at 18:14



















                                • Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                  – Arnas Dičkus
                                  Mar 4 at 13:12











                                • You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                  – Nikola Lukic
                                  Mar 4 at 13:18











                                • What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                  – Nikola Lukic
                                  Mar 4 at 13:19






                                • 1





                                  Thanks, for explanation I wasn't aware of this.

                                  – Arnas Dičkus
                                  Mar 4 at 13:24






                                • 2





                                  "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                  – Bergi
                                  Mar 4 at 18:14

















                                Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                – Arnas Dičkus
                                Mar 4 at 13:12





                                Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet

                                – Arnas Dičkus
                                Mar 4 at 13:12













                                You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                – Nikola Lukic
                                Mar 4 at 13:18





                                You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.

                                – Nikola Lukic
                                Mar 4 at 13:18













                                What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                – Nikola Lukic
                                Mar 4 at 13:19





                                What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));

                                – Nikola Lukic
                                Mar 4 at 13:19




                                1




                                1





                                Thanks, for explanation I wasn't aware of this.

                                – Arnas Dičkus
                                Mar 4 at 13:24





                                Thanks, for explanation I wasn't aware of this.

                                – Arnas Dičkus
                                Mar 4 at 13:24




                                2




                                2





                                "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                – Bergi
                                Mar 4 at 18:14





                                "String.fromCharCode(str.charCodeAt(x) ^ 32)" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase().

                                – Bergi
                                Mar 4 at 18:14











                                0














                                I use modify of String prototype :



                                for implementation of replaceAt .






                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);








                                share|improve this answer





















                                • 1





                                  Nice solution with replaceAt !

                                  – Nikola Lukic
                                  Mar 4 at 13:23











                                • where is replaceAt from?

                                  – Nina Scholz
                                  Mar 4 at 13:39











                                • @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                  – Arnas Dičkus
                                  Mar 4 at 13:48






                                • 1





                                  String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                  – Peter Hassaballah
                                  Mar 4 at 14:22






                                • 1





                                  Don't modify objects you don't own.

                                  – Emile Bergeron
                                  Mar 4 at 14:51
















                                0














                                I use modify of String prototype :



                                for implementation of replaceAt .






                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);








                                share|improve this answer





















                                • 1





                                  Nice solution with replaceAt !

                                  – Nikola Lukic
                                  Mar 4 at 13:23











                                • where is replaceAt from?

                                  – Nina Scholz
                                  Mar 4 at 13:39











                                • @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                  – Arnas Dičkus
                                  Mar 4 at 13:48






                                • 1





                                  String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                  – Peter Hassaballah
                                  Mar 4 at 14:22






                                • 1





                                  Don't modify objects you don't own.

                                  – Emile Bergeron
                                  Mar 4 at 14:51














                                0












                                0








                                0







                                I use modify of String prototype :



                                for implementation of replaceAt .






                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);








                                share|improve this answer















                                I use modify of String prototype :



                                for implementation of replaceAt .






                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);








                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);





                                // Modify prototype
                                String.prototype.replaceAt=function(index, replacement) {
                                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
                                }

                                function WaveFunction(str) {

                                var base = str;
                                var R = ;
                                var n =str.length;

                                for (var i=0 ; i<n ;i++){

                                str = base;
                                var c=str.charAt(i);
                                var res = c.toUpperCase();
                                // console.log(res);
                                str = str.replaceAt(i, res);

                                R.push(str);
                                }
                                return R;
                                }

                                var REZ = WaveFunction("hello");

                                console.log(REZ);






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 4 at 14:23









                                Nikola Lukic

                                1,75721937




                                1,75721937










                                answered Mar 4 at 12:31









                                Peter HassaballahPeter Hassaballah

                                825




                                825








                                • 1





                                  Nice solution with replaceAt !

                                  – Nikola Lukic
                                  Mar 4 at 13:23











                                • where is replaceAt from?

                                  – Nina Scholz
                                  Mar 4 at 13:39











                                • @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                  – Arnas Dičkus
                                  Mar 4 at 13:48






                                • 1





                                  String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                  – Peter Hassaballah
                                  Mar 4 at 14:22






                                • 1





                                  Don't modify objects you don't own.

                                  – Emile Bergeron
                                  Mar 4 at 14:51














                                • 1





                                  Nice solution with replaceAt !

                                  – Nikola Lukic
                                  Mar 4 at 13:23











                                • where is replaceAt from?

                                  – Nina Scholz
                                  Mar 4 at 13:39











                                • @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                  – Arnas Dičkus
                                  Mar 4 at 13:48






                                • 1





                                  String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                  – Peter Hassaballah
                                  Mar 4 at 14:22






                                • 1





                                  Don't modify objects you don't own.

                                  – Emile Bergeron
                                  Mar 4 at 14:51








                                1




                                1





                                Nice solution with replaceAt !

                                – Nikola Lukic
                                Mar 4 at 13:23





                                Nice solution with replaceAt !

                                – Nikola Lukic
                                Mar 4 at 13:23













                                where is replaceAt from?

                                – Nina Scholz
                                Mar 4 at 13:39





                                where is replaceAt from?

                                – Nina Scholz
                                Mar 4 at 13:39













                                @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                – Arnas Dičkus
                                Mar 4 at 13:48





                                @NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.

                                – Arnas Dičkus
                                Mar 4 at 13:48




                                1




                                1





                                String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                – Peter Hassaballah
                                Mar 4 at 14:22





                                String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)

                                – Peter Hassaballah
                                Mar 4 at 14:22




                                1




                                1





                                Don't modify objects you don't own.

                                – Emile Bergeron
                                Mar 4 at 14:51





                                Don't modify objects you don't own.

                                – Emile Bergeron
                                Mar 4 at 14:51


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54983179%2fcreating-a-wave-of-string-in-javascript%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to send String Array data to Server using php in android

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                Is anime1.com a legal site for watching anime?