Codecademy - Aprende a língua dos códigos!

27 Ago 2012, 13:56 [#1]
O Codecademy ensina-te a linguagem HTML, HTTP, JavaScript, jQuery, entre outros.
É como se fossem aulas, dão indicações e tu só tens de passar as indicações para códigos.
Eu gosto bastante, já tenho conta à algum tempo, e até agora não foi difícil.
Mas, chego a uma parte (Functions in JavaScript > Section 1 of 5 > 4. Fix me!) e não consigo passar daqui, será que alguém me pode explicar como se faz esta parte?

Re: Codecademy - Aprende a língua dos códigos!

27 Ago 2012, 14:13 [#2]
Eu aprendi a programar em HTML, CSS, PHP, SQL/MySQL e JavaScript/jQuery na w3schools e com um ou outro vídeo de lynda.com.
Quack Escreveu:HTTP
Essa língua é nova, nunca ouvi falar. :geek:
Quack Escreveu:Eu gosto bastante, já tenho conta à algum tempo, e até agora não foi difícil.
Mas, chego a uma parte (Functions in JavaScript > Section 1 of 5 > 4. Fix me!) e não consigo passar daqui, será que alguém me pode explicar como se faz esta parte?
Será que podes postar aqui?
Imagem

Re: Codecademy - Aprende a língua dos códigos!

27 Ago 2012, 14:18 [#3]
eduardextreme Escreveu:
Quack Escreveu:HTTP
Essa língua é nova, nunca ouvi falar. :geek:
Não?! :facepalm: :mrgreen:
eduardextreme Escreveu:
Quack Escreveu:Eu gosto bastante, já tenho conta à algum tempo, e até agora não foi difícil.
Mas, chego a uma parte (Functions in JavaScript > Section 1 of 5 > 4. Fix me!) e não consigo passar daqui, será que alguém me pode explicar como se faz esta parte?
Será que podes postar aqui?
Claro, claro.

Os objectivos são estes:
Spoiler
You never forget your first function!

Alas, Bob isn't there yet. He doesn't know about functions. To the right, you see a whole big bunch of code. It is a bit scary! You can see the code on lines 10-14 is repeated on lines 18-22. We can use a function to simplify all of this!

What does the repeated code do? It takes in some variable name, capitalizes the first character and then prints the full name.

There are many things we want you to do!

1) Define a function named fixName beginning on line 7.

2) Copy and paste one set of the repeated code into this function (ie. What was originally on lines 10 - 14).

3) Then delete both the repeated code blocks.

4) After each 'name = prompt' line of code, call your fixName function.

This shows how using a function is more efficient than typing in the same code over and over! Win!
As linhas são estas:
Spoiler
1. var fullName = "";
2. var name;
3. var firstLetter;
4. /*
5. fixName function definition should go here.
6. */
7.
8. name = prompt("Enter your first name (all in lower case):");
9.
10. /***** Begin repeated code block *****/
11. firstLetter = name.substring(0, 1);
12. name = firstLetter.toUpperCase() + name.substring(1);
13. fullName = fullName + " " + name;
14. /***** End repeated code block *****/
15.
16. name = prompt("Enter your second name (all in lower case):");
17.
18. /***** Begin repeated code block *****/
19. firstLetter = name.substring(0, 1);
20. name = firstLetter.toUpperCase() + name.substring(1);
21. fullName = fullName + " " + name;
22. /***** End repeated code block *****/
23.
24. console.log("And your full name is:" + fullName);

Re: Codecademy - Aprende a língua dos códigos!

27 Ago 2012, 14:49 [#4]
Isso é para aprenderes as funções.
Tens código repetido e podes utilizar uma função com esse mesmo código e chamá-lo quando necessário.

Código: Seleccionar todos

function fixName (string name, string &fullName)
{
 firstLetter = name.substring(0, 1);
 name = firstLetter.toUpperCase() + name.substring(1);
 fullName = fullName + " " + name;
}

fixName(prompt("Enter your first name (all in lower case):"));

fixName(prompt("Enter your second name (all in lower case):"));

console.log("And your full name is:" + fullName);
Eu já nem me lembro da sintaxe, isto foi só resolver por alto a coisa.

Não sei se apenas chamando por fixName(string name), ele consegue aceder a fullName. Se conseguir tudo bem, senão terás de passar a variável fullName para a função ( fixName(string name, string &fullName) ) ou definir fullName como variável global.
Imagem