Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------
- //Write a function that adds and removes numbers to/from an array.
- //You will receive a command which can either be "add" or "remove".
- //The initial number is 1. Each input command should increase that number, regardless of what it is.
- //• Upon receiving an "add" command you should add the current number to your array.
- //• Upon receiving the "remove" command you should remove the last entered number, currently existent in the array.
- // --- Input
- //The input comes as array of strings. Each element holds a command.
- //-------------------------------------------------------------------------------------
- function add_and_remove(commands) {
- let commandsL = commands.length;
- let endArray = [];
- let currentNum = 1;
- for (let i = 0; i < commandsL; i++) {
- if (commands[i] == "add") {
- endArray.push(currentNum);
- } else if (commands[i] == "remove") {
- endArray.pop()
- }
- currentNum += 1;
- }
- if (endArray.length > 0) {
- console.log(endArray.join(' '));
- } else {
- console.log("Empty");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement