p5.js | shorten() function

The shorten() function in p5.js is used to decrease the number of elements of the given array by one.

Syntax:

shorten(Array)

Parameters: This function accepts a parameter Array whose elements are to be shortened by one.



Return Value: It returns the shortened array.

Below program illustrates the shorten() function in p5.js.
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  
    // Creating Canvas size
    createCanvas(500, 90);
}
  
function draw() {
  
    // Set the background color 
    background(220);
  
    // Initializing the arrays
    let Array1 = ['IT', 'CSE', 'ECE'];
  
    // Calling to shorten() function.
    let Array2 = shorten(Array1);
  
    // Set the size of text 
    textSize(16);
  
    // Set the text color 
    fill(color('red'));
  
    // Getting new shortened array
    text("Shortened array is : " + Array2, 50, 30);
  
}

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  
    // Creating Canvas size
    createCanvas(500, 90);
}
  
function draw() {
  
    // Set the background color 
    background(220);
  
    // Calling to shorten() function on an array
    // Taken as the parameter
    let Array = shorten(['Ram', 'Shayam', 'Geeta', 'Anita']);
  
    // Set the size of text 
    textSize(16);
  
    // Set the text color 
    fill(color('red'));
  
    // Getting new shortened array
    text("Shortened array is : " + Array, 50, 30);
  
}

chevron_right


Output:

Reference: https://p5js.org/reference/#/p5/shorten



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.