Forked from dustinddoan/w2d2_checkpoint_dustin.js
Last active
August 25, 2016 01:04
-
-
Save rexfordkelly-at-makersquare/0c5219d97ce424a39c6908c9a6e68a26 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
//1. Write a function average that takes two numbers as input (parameters), and returns the average of those numbers. | |
function average(num1, num2) { // Good Job! | |
return (num1 + num2) / 2; | |
} | |
//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 greeting(name) { // Good Job! | |
console.log('Hello ' + name + '!'); | |
} | |
//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 isEven(num) { // Good Job! | |
if (num % 2 === 0) { | |
return true; | |
} | |
return false; | |
} | |
//odd: the opposite of the above. | |
function isOdd(num) { // Good Job! | |
if (num %2 !== 0) { | |
return true; | |
} | |
return false; | |
} | |
//positive: returns true if its argument is positive, and false otherwise. | |
function isPossitive(num) { // Good Job! | |
if (num >= 0) { | |
return true; | |
} | |
return false; | |
} | |
//negative: the opposite of the above. | |
function isNegative(num) { // Good Job! | |
if (num < 0) { | |
return true; | |
} | |
return false; | |
} | |
//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 language(para) { // Good Job! | |
if (para === 'english') { | |
return 'Hello'; | |
} else if (para === 'french') { | |
return 'Bonjour'; | |
} else if (para === 'spanish') { | |
return 'Hola'; | |
} | |
} | |
//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) { // Good Job! | |
if (username.length >= 6 && password >= 10) { | |
return true; | |
} | |
return 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) { // Good Job! | |
var newStr = ''; | |
for (var i = 0; i < count; i++) { | |
newStr += str; | |
} | |
return newStr; | |
} | |
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. | |
function ave(array) { // Good Job! | |
var average = 0; | |
var sum = 0; | |
for (var i = 0; i < array.length; i++) { | |
sum += array[i]; | |
} | |
average = sum / (array.length); | |
return average; | |
} | |
//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: | |
function countWords(s) { // Good Job! | |
var words = s.split(' '); | |
var result = {}; | |
var count = 0; | |
for (var i = 0; i < words.length; i++) { | |
var prop = words[i]; | |
if (prop in result) { | |
count += 1; | |
result[prop] = count; | |
} else { | |
result[prop] = count; | |
count +=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(); // => ??? | |
function countWords(s) { // Good Job! | |
var words = s.toLowerCase().split(' '); | |
var result = {}; | |
var count = 0; | |
for (var i = 0; i < words.length; i++) { | |
var prop = words[i]; | |
if (prop in result) { | |
count += 1; | |
result[prop] = count; | |
} else { | |
result[prop] = count; | |
count +=1 | |
} | |
} | |
return result; | |
} | |
// 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. | |
function reverse(str) { // Good Job! | |
var temp = str.split(' '); | |
var reverseStr = []; | |
for (var i = str.length - 1; i >= 0; i--) { | |
reverseStr.push(str[i]); | |
} | |
return reverseStr.join(' '); | |
} | |
// 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}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment