-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayPushLimit.js
More file actions
45 lines (31 loc) · 919 Bytes
/
arrayPushLimit.js
File metadata and controls
45 lines (31 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// arrayPushLimit.js
function arrayPushLimit(whichArray, newItem, maxLimit)
{
// add new item
whichArray.push(newItem);
// check limit
if (whichArray.length > maxLimit)
{
// remove the oldest
whichArray.shift();
}
// return the updated list
return whichArray;
}
//----//
// example:
let myJournal = ["Msg 1", "Msg 2", "Msg 3"];
myJournal = arrayPushLimit(myJournal, "Msg 4", 3);
console.log(myJournal);
//----//
/*
// ["Msg 2", "Msg 3", "Msg 4"]
Queue Function (Add and Limit)
This function adds a new item to an array, but ensures the array never gets bigger than a specific limit (e.g., 3 items).
*/
//----//
// Dedicated to God the Father
// All Rights Reserved Christopher Andrew Topalian Copyright 2000-2025
// https://github.com/ChristopherTopalian
// https://github.com/ChristopherAndrewTopalian
// https://sites.google.com/view/CollegeOfScripting