Last active
August 26, 2016 02:21
-
-
Save jinhwanee93/2e8e3990ec6d8dcd0b1371bab28bd483 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# W2D2 Checkpoint | |
// I'M HAVING THE HARDEST TIME WITH LOOPS (FOR, DO, WHILE "ALL OF THE ABOVE") AND INCORPORATING LOOPS INTO THE FUNCTIONS!!!' | |
Submit a link to your gist [here](https://goo.gl/forms/HywFjkNIU9mLM7c03) | |
1. Write a function `average` that takes two numbers as input (parameters), and | |
returns the average of those numbers. | |
function average (num1, num2) { | |
return ((num1 + num2) / 2) | |
} | |
average(2, 4); // 3 | |
2. Write a function `greeter` that takes a name as an argument and *greets* | |
that name by returning something along the lines of `"Hello, <name>!"` | |
function greeter (name) { | |
return "Hello, " + name + "!"; | |
} | |
greeter("Alex"); | |
// "Hello, Alex!" | |
3. Write the following functions that each accept a single number as an | |
argument: | |
+ `even`: returns `true` if its argument is even, and `false` otherwise. | |
function even (number) { | |
if(number % 2 === 0) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
even(2); // true | |
even(1); // false | |
+ `odd`: the opposite of the above. | |
function odd (number) { | |
if(number % 2 !== 0) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
odd(2); // false | |
odd(1); // true | |
+ `positive`: returns `true` if its argument is positive, and `false` otherwise. | |
function positive (number) { | |
if(number > 0) { | |
return true; | |
} | |
else { | |
return false | |
} | |
} | |
positive(2); // true | |
positive(-1); // false | |
+ `negative`: the opposite of the above. | |
function negative (number) { | |
if(number < 0) { | |
return true; | |
} | |
else { | |
return false | |
} | |
} | |
negative(2); // false | |
negative(-1); // true | |
4. Write a function `sayHello` that takes a *language* parameter, and returns | |
"hello" in that language. Make the function work with **at least three | |
languages**. | |
function sayHello (language) { | |
if(language == "Portuguese") { | |
return "OLA!"; | |
} | |
else if (language == "Korean") { | |
return "안영"; | |
} | |
else if (language == "English") { | |
return "HELLO!" | |
} | |
} | |
sayHello("Portuguese"); // "OLA!" | |
sayHello("Korean"); // "안영" | |
sayHello("English"); // "HELLO!" | |
5. Write a function `validCredentials` that accepts two parameters: *username* | |
and *password*, and returns `true` if **both** are long enough, and `false` | |
otherwise. You can decide what constitutes "long enough". | |
function validCredentials (username, password) { | |
if (username.length >= 8 && password.length >= 8) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
validCredentials("ASDFASDF", "asdfasdf"); // true | |
validCredentials("asdf", "sadfasdfasdf"); // false | |
6. **Repeating a String `n` Times:** Let's write a function called | |
`repeatString` that takes two parameters: a string `str`, which is the string | |
to be repeated, and `count` -- a number representing how many times the | |
string `str` should be repeated, *e.g.* | |
function repeatString(str, count) { | |
// TODO: your code here | |
return str.repeat(count); | |
} | |
OR | |
function repeatString(str, count) { | |
var rtn = ''; | |
for(var i = 0; i < count; i++) { | |
rtn += str; | |
} | |
return rtn; | |
} | |
repeatString('dog', 0); // => '' | |
repeatString('dog', 1); // => 'dog' | |
repeatString('dog', 2); // => 'dog' + 'dog' => 'dogdog' | |
repeatString('dog', 3); // => 'dog' + 'dog' + 'dog' => 'dogdogdog' | |
7. Write a function called `average` that takes an array of numbers as a | |
parameter and returns the *average* of those numbers. | |
// TOOK ME A WHILE TO FIGURE THIS ONE OUT, PARTNER HELPED ME OUT WITH THIS ONE | |
var arrayOfNumbers = [] | |
function average (numbers) { | |
var total = 0; | |
for (var i = 0; i < numbers.length; i++) { | |
total += numbers[i] | |
} | |
return total/numbers.length; | |
} | |
average([1, 3, 4, 5, 6, 7]) | |
8. Write a function `countWords` that, when given a string as an argument, | |
returns an *object* where *keys* are the words in the string, and *values* | |
are the number of occurrences of that word within the string: | |
// I HAD MAJOR DIFFICULTIES DOING THIS EXERCISE, I WILL PROBABLY NEED TO REVIEW THIS PROBLEM (from this point foward it was difficult to finish this checkpoint) | |
function countWords(s) { | |
// ... | |
var s = s.split(" "); | |
var result = {}; | |
for (var i = 0; i < s.length; i++) { | |
if(result[s[i]] ){ | |
result[s[i]]++; | |
} | |
else { | |
result[s[i]] = 1; | |
} | |
} | |
return result; | |
} | |
countWords("hello hello"); // => {"hello": 2} | |
countWords("Hello hello"); // => {"Hello": 1, "hello": 1} | |
countWords("The quick brown"); // => {"The": 1, "quick": 1, "brown": 1} | |
**HINT:** You will want to make use of the string method `split`. Try | |
`\"Hello hello".split(" ")` at a console to see how it works. | |
- Modify `countWords` to be *case insensitive* by using the following string | |
method (experiment at a console with it to learn its behavior): | |
"HElLo".toLowerCase(); // => "hello" | |
### Bonus | |
// I HAVEN'T REALLY TOUCHED THIS EXERCISE BUT WILL TRY TO TACKLE IT AGAIN. | |
1. Read about the join method on MDN and use it to implement a function that accepts a string | |
as an argument and returns that string reversed. | |
2. The function `Object.keys` returns an array of an object's *keys*. Experiment | |
with it at the console like this: | |
Object.keys({a: 1, b: 2}); | |
function reverseString(str) { | |
return str = str.split('').reverse().join(''); | |
} | |
reverseString('abc'); | |
Using this property, write versions of the above functions using repetition | |
through function invocation (*i.e.* recursion) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment