How to Edit Todo List Without Using Global Variables Functional Programming Vanilla JSHow do I define global variables in CoffeeScript?Define global variable in a JavaScript functionHow can a time function exist in functional programming?Reverse an array in JavaGlobal Variables, Functions and JavascriptArray of subarrays of objects, how to not modify original array in function?Math.max returns null on variables?Javascript filter() function does not workJS closure example explainedJavascript: Change Global Variables but in a function without using “Promise”
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Can I make popcorn with any corn?
Why doesn't H₄O²⁺ exist?
Can a vampire attack twice with their claws using Multiattack?
Why can't we play rap on piano?
How to determine what difficulty is right for the game?
Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?
A case of the sniffles
Convert two switches to a dual stack, and add outlet - possible here?
Theorems that impeded progress
What does "Puller Prush Person" mean?
Is it possible to do 50 km distance without any previous training?
Paid for article while in US on F-1 visa?
How much of data wrangling is a data scientist's job?
How can bays and straits be determined in a procedurally generated map?
What's that red-plus icon near a text?
Cross compiling for RPi - error while loading shared libraries
I'm flying to France today and my passport expires in less than 2 months
Malformed Address '10.10.21.08/24', must be X.X.X.X/NN or
Are the number of citations and number of published articles the most important criteria for a tenure promotion?
Revoked SSL certificate
Did Shadowfax go to Valinor?
What doth I be?
DC-DC converter from low voltage at high current, to high voltage at low current
How to Edit Todo List Without Using Global Variables Functional Programming Vanilla JS
How do I define global variables in CoffeeScript?Define global variable in a JavaScript functionHow can a time function exist in functional programming?Reverse an array in JavaGlobal Variables, Functions and JavascriptArray of subarrays of objects, how to not modify original array in function?Math.max returns null on variables?Javascript filter() function does not workJS closure example explainedJavascript: Change Global Variables but in a function without using “Promise”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How do you continually edit an array of todos while using FP? I understand that when you use array methods such as .concat() you are returning a copy of the array and not editing the original. Here is my original code that simply pushes a new todo to the todo array. This permanently changes the myTodos variable which we are trying to avoid.
let myTodos = []
function addTodo(newTodo)
myTodos.push(newTodo)
return myTodos
I have rewritten this function as such:
const addTodo = (arr, todoText) => arr.concat(todoText)
This works just fine but I don't understand how to keep the value that is returned when I am supposed to avoid global variables. If I call this to add a second todo to my list it will only return that second todo since the var is not being stored anywhere. I feel like there is a very obvious way to work this but I just cannot seem to figure it out.
My apologies I am still very new to the programming world. Any help would be greatly appreciated.
javascript arrays functional-programming concat
|
show 4 more comments
How do you continually edit an array of todos while using FP? I understand that when you use array methods such as .concat() you are returning a copy of the array and not editing the original. Here is my original code that simply pushes a new todo to the todo array. This permanently changes the myTodos variable which we are trying to avoid.
let myTodos = []
function addTodo(newTodo)
myTodos.push(newTodo)
return myTodos
I have rewritten this function as such:
const addTodo = (arr, todoText) => arr.concat(todoText)
This works just fine but I don't understand how to keep the value that is returned when I am supposed to avoid global variables. If I call this to add a second todo to my list it will only return that second todo since the var is not being stored anywhere. I feel like there is a very obvious way to work this but I just cannot seem to figure it out.
My apologies I am still very new to the programming world. Any help would be greatly appreciated.
javascript arrays functional-programming concat
2
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
3
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
1
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
2
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
1
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59
|
show 4 more comments
How do you continually edit an array of todos while using FP? I understand that when you use array methods such as .concat() you are returning a copy of the array and not editing the original. Here is my original code that simply pushes a new todo to the todo array. This permanently changes the myTodos variable which we are trying to avoid.
let myTodos = []
function addTodo(newTodo)
myTodos.push(newTodo)
return myTodos
I have rewritten this function as such:
const addTodo = (arr, todoText) => arr.concat(todoText)
This works just fine but I don't understand how to keep the value that is returned when I am supposed to avoid global variables. If I call this to add a second todo to my list it will only return that second todo since the var is not being stored anywhere. I feel like there is a very obvious way to work this but I just cannot seem to figure it out.
My apologies I am still very new to the programming world. Any help would be greatly appreciated.
javascript arrays functional-programming concat
How do you continually edit an array of todos while using FP? I understand that when you use array methods such as .concat() you are returning a copy of the array and not editing the original. Here is my original code that simply pushes a new todo to the todo array. This permanently changes the myTodos variable which we are trying to avoid.
let myTodos = []
function addTodo(newTodo)
myTodos.push(newTodo)
return myTodos
I have rewritten this function as such:
const addTodo = (arr, todoText) => arr.concat(todoText)
This works just fine but I don't understand how to keep the value that is returned when I am supposed to avoid global variables. If I call this to add a second todo to my list it will only return that second todo since the var is not being stored anywhere. I feel like there is a very obvious way to work this but I just cannot seem to figure it out.
My apologies I am still very new to the programming world. Any help would be greatly appreciated.
javascript arrays functional-programming concat
javascript arrays functional-programming concat
edited Mar 8 at 1:56
KwMarkHintz
asked Mar 8 at 1:53
KwMarkHintzKwMarkHintz
166
166
2
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
3
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
1
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
2
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
1
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59
|
show 4 more comments
2
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
3
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
1
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
2
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
1
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59
2
2
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
3
3
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
1
1
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
2
2
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
1
1
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59
|
show 4 more comments
1 Answer
1
active
oldest
votes
/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055630%2fhow-to-edit-todo-list-without-using-global-variables-functional-programming-vani%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
add a comment |
/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
add a comment |
/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());/*
1) how to avoid global variables?
2) how to not change the original?
3) how to keep the changed
#1 - this is called an Immediately Invoked Functional Expression (IIFE for short)
What this does is lets us create a "scope" within our script. Variables we
create inside it with `var` or `let` or `const`, are not global. They will
only exist inside the scope.
*/
(function()
var originalArray = [];
// #2 - concat does not change the original array
// #3 - to keep the change, just store it in another variable
var changedArray = originalArray.concat('me');
// #3 - if I want to make more changes, I use the new variable
changedArray = changedArray.concat('yet another value');
console.log(originalArray);
console.log(changedArray);
());answered Mar 8 at 2:15
TaplarTaplar
18k21529
18k21529
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
add a comment |
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
Thank you! This is exactly what I was looking for. I was under the impression that using a new variable to store the array was impure.
– KwMarkHintz
Mar 8 at 4:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055630%2fhow-to-edit-todo-list-without-using-global-variables-functional-programming-vani%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Uh, no, push and pop alter the original array
– Taplar
Mar 8 at 1:54
3
You literally have zero reason to make a wrapper method around the push method, with what you are doing. Just push to your array
– Taplar
Mar 8 at 1:56
1
@Taplar - probably homework assignment - must be functional
– Randy Casburn
Mar 8 at 1:57
2
But it's not functional, because it's being over complicated, :)
– Taplar
Mar 8 at 1:57
1
@Taplar - agree completely.
– Randy Casburn
Mar 8 at 1:59