In JavaScript, join
and pop
are two methods that can be used on arrays.
join
method is used to concatenate all the elements of an array into a string. pop
method is used to remove the last element of an array and return that element. It modifies the original array, reducing its length by one.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var old_pl = new Array("Cobol", "Fortran", "Algol");
var new_string = old_pl.join(" ");
document.write(new_string);
</script>
</body>
</html>
This script creates an array called old_pl
with three elements: "Cobol", "Fortran", and "Algol". Then, the join
method is called on the old_pl
array with a space as the separator argument. The resulting string is assigned to a variable called new_string
.
Finally, the document.write
method is used to output the new_string
to the webpage. The resulting output would be a string that concatenates all the elements of the old_pl
array.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var old_pl = new Array("Cobol", "Fortran", "Algol");
document.write(old_pl + "<br><br>");
old_pl.pop();
document.write(old_pl + "<br><br>");
old_pl.pop();
document.write(old_pl + "<br><br>");
old_pl.pop();
</script>
</body>
</html>
This script creates an array called old_pl
with three elements: "Cobol", "Fortran", and "Algol". Then, the document.write
method is used to output the old_pl
array to the webpage.
After that, the pop
method is called on the old_pl
array three times in a row, which removes the last element of the array on each call.
After each pop
method call, the document.write
method is used to output the old_pl
array to the webpage again. As a result, the output of the script would be:
Cobol,Fortran,Algol
Cobol,Fortran
Cobol
The first line shows the original array with all three elements. The second line shows the old_pl
array after the first pop
method call, which removed the last element ("Algol") from the array. The third line shows the old_pl
array after the second pop
method call, which removed the last element ("Fortran") from the array. The fourth line shows the old_pl
array after the third pop
method call, which removed the last element ("Cobol") from the array.
At this point, the old_pl
array is empty.
No comments:
Post a Comment