Search code examples
arraysslicesplice

Javascript array manipulation / splice(), slice()


Javascript beginner.

I apply the split() method to the string, and next apply the splice() array method to the words array, removing two words at array "index 3", "really" and "cool" Since the splice () method returns any deleted words to display these in an alert dialog.

But "index" is unhappy with the code. the output from script is:

JpowerfulaScript is really cool language

I want the output "javascript is perfect language"

enter image description here


Solution

  • Complete solution here

        function wrangleArray() {
                var sentence = "JavaScript is really cool language";
                document.getElementById('div1').innerHTML = "<p>"+ sentence + "</p>";
                var words = sentence.split(" ");
                words.splice(2, 2, "powerful");
                document.getElementById('div2').innerHTML = '<p>'+ words.join(" ") + '</p>';
    
            }