Skip to content

Instantly share code, notes, and snippets.

@rexfordkelly-at-makersquare
Last active January 5, 2023 08:23
Show Gist options
  • Save rexfordkelly-at-makersquare/8d8e8ca2e5220674719476e881542983 to your computer and use it in GitHub Desktop.
Save rexfordkelly-at-makersquare/8d8e8ca2e5220674719476e881542983 to your computer and use it in GitHub Desktop.
Week 2 Day 2 Checkpoint for Eliot Mason
/*
EJ Mason
August 23, 2016
Week 2
----------------------------------------------------------------------------------
W2D2 Checkpoint
----------------------------------------------------------------------------------
*/
/*
1. Write a function average that takes two numbers as input (parameters), and
returns the average of those numbers.
*/
function average (firstNum, secondNum){ // GOOD JOB!
return (firstNum + secondNum) / 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 greeter(name){ // GOOD JOB!, the only thing is you forgot the semicolon on line 28
return '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.
- odd: the opposite of the above.
- positive: returns true if its argument is positive, and false
otherwise.
- negative: the opposite of the above.
*/
function even(number){ // Good Job!
if(number % 2 === 0){
return true;
}
return false;
}
function odd(number){
if(number % 2 === 0){
return false;
}
return true;
}
function positive(number){ // Good job!
if(number === 0){
return 'Error, 0 is unsigned';
}
else{
if(number > 0){
return true;
}
return false;
}
}
function negative(number){ // Good Job!
if(number === 0){
return 'Error, 0 is unsigned';
}
else{
if(number < 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 sayHello(language){ // Good Job!
if(language.toLowerCase() === 'english'){
return 'Hello';
}
if(language.toLowerCase() === 'spanish'){
return 'Hola';
}
if(language.toLowerCase() === 'portuguese'){
return 'Ola';
}
}
/*
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!
//password and username must be at least 8 chars
if(username.length > 7){
if(password .length > 7){
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.
*/
function repeatString(str, count){ // Good Job!, i like your use of recursion here!
if(count === 0){
return '';
}
else{
return str + repeatString(str, count - 1);
}
}
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 average(inArray){ // Good Job!
var answer = 0;
for(var i = 0; i < inArray.length; i++){
answer += inArray[i];
}
return answer / inArray.length;
}
/*
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:
*/
//helper function
function keyExists(myObject, stringToCheck){ // Good Job!
var alreadyExists = false;
for (var key in myObject){
if(key === stringToCheck){
alreadyExists = true;
}
}
return alreadyExists;
}
function countWords(s) { // Good Job!
//uncomment below and comment line 170 for case insensitive;
//var newArray = s.toLowerCase().split(' ');
var newArray = s.split(' ');
var newObject = {};
newObject[newArray[0]] = 1;
//does not need to enter loop if array only contained one word
if(newArray.length === 1){
return newObject;
}
//If there is more than one word in array
else{
for (var i = 1; i < newArray.length; i++){
if(keyExists(newObject, newArray[i])){
newObject[newArray[i]] += 1;
}
else{
newObject[newArray[i]] = 1;
}
}
}
return newObject;
}
countWords("hello hello"); // => {"hello": 2}
countWords("Hello hello"); // => {"Hello": 1, "hello": 1}
countWords("The quick brown"); // => {"The": 1, "quick": 1, "brown": 1}
/*
----------------------------------------------------------------------------------
BONUS
----------------------------------------------------------------------------------
*/
/*
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.
*/
function stringReversed(inString){ // Good Job!
var newString = '';
for(var i = inString.length - 1; i >= 0; i--){
newString += inString[i];
}
return newString;
}
/*
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}); //returns array of keys
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