# Programação Funcional - Refatorar variáveis globais fora de funções

**URL:** <https://forum.freecodecamp.org/t/programacao-funcional-refatorar-variaveis-globais-fora-de-funcoes/555548>\
**Category:** General\
**Tags:** português\
**Created:** [September 19, 2022, 4:29pm UTC](https://forum.freecodecamp.org/t/programacao-funcional-refatorar-variaveis-globais-fora-de-funcoes/555548 "2022-09-19T16:29:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![97revenge](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/97revenge/32/261131_2.png) [@97revenge](https://forum.freecodecamp.org/u/97revenge)\
**Post date:** [September 19, 2022, 4:29pm UTC](https://forum.freecodecamp.org/t/programacao-funcional-refatorar-variaveis-globais-fora-de-funcoes/555548/1 "2022-09-19T16:29:33Z")

</div>

**Conte-nos o que está acontecendo:**  
Descreva detalhadamente o problema aqui.

```
   **Seu código até o momento**

```

```js
// A variável global
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

function add( bookList, bookName ) {

	let bookListCopy = bookList.slice(0);
	
	bookListCopy.push( bookName );
	
	
	return bookListCopy;
};

function remove( bookList, bookName ) {
	
	let bookListCopy = bookList.slice(0);
	
	if( bookListCopy.indexOf( bookName ) >= 0 ) {
		
		bookListCopy.splice( bookListCopy.indexOf( bookName ), 1 );
		
		
		return bookListCopy;
   }
};

```

porem , eu fiz outro codigo e acho que tive resultado parecidos , olha :

```const
  'The Hound of the Baskervilles',
  'On The Electrodynamics of Moving Bodies',
  'Philosophiæ Naturalis Principia Mathematica',
  'Disquisitiones Arithmeticae',
];
const [bookOne, bookTwo, bookThree, ...otherBooks] = bookList;
console.log(`esta é a lenght de bookList : ${bookList.length}`);
console.log(bookList);

function add(bookName) {
  const addBooks = bookList;
  addBooks.push(bookName);
  return addBooks;
}

function remove(bookName) {
  const removeBooks = bookList;
  const book_index = removeBooks.indexOf(bookName);
  if (book_index >= 0) {
    removeBooks.splice(book_index, 1);
    return removeBooks;
  }
}

const adding = add(bookOne);
console.log(adding);

const removing = remove(bookThree);
console.log(removing);

```

o que vcs acham ???

```
   **Informações de seu navegador:**

```

Agente de usuário: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36`

**Desafio:** Programação Funcional - Refatorar variáveis globais fora de funções

**Link para o desafio:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/portuguese/learn/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions)**
>
> Aprenda a programar — de graça

---

<div class="post-metadata">

**Author:** ![afucher](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/afucher/32/303209_2.png) [@afucher](https://forum.freecodecamp.org/u/afucher)\
**Post date:** [February 7, 2023, 2:08am UTC](https://forum.freecodecamp.org/t/programacao-funcional-refatorar-variaveis-globais-fora-de-funcoes/555548/2 "2023-02-07T02:08:34Z")

</div>

@97revenge não sei se entendi. O segundo código que colocou, ele está passando nos testes?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 7, 2025, 3:39pm UTC](https://forum.freecodecamp.org/t/programacao-funcional-refatorar-variaveis-globais-fora-de-funcoes/555548/3 "2025-01-07T15:39:52Z")

</div>

Closed on January 7, 2025, 3:39pm UTC


