-1

I need to write a program that prints out an array, removes the first two elements of the array using arr.remove(i), and then prints out the new array. My program correctly removes the first two elements, but I am having trouble with getting the program to print out the lists in the correct format.

The program should print out:

Sam, Lisa, Laurie, Bob, Ryan
Laurie, Bob, Ryan

My program prints out:

Sam, Lisa, Laurie, Bob, RyanLaurie, Bob, Ryan

This is my program:

function start(){
    var arr = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];

    for(var i = 0; i < arr.length; i++){
        print(arr[i]);

        if(i < arr.length - 1){
            print(", ");
        } 

    }


    var elem = arr.remove(0);
    var elem2 = arr.remove(0);


    for(var i = 0; i < arr.length; i++){
        print(arr[i]);

        if(i < arr.length - 1){

            print(", ");
        } 

    }

}

How can I fix this program so that it prints the new list on a different line?

J..
  • 35
  • 1
  • 5

2 Answers2

2

Call print("\n") between the loops

Jakub Licznerski
  • 614
  • 10
  • 28
1

There is no Array.remove method. However, Array.splice can be used for removing elements:

if (index > -1) {
    array.splice(index, 1);
}

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.

user2864740
  • 54,112
  • 10
  • 112
  • 187
Sanka
  • 1,104
  • 1
  • 10
  • 18