Skip to content

Instantly share code, notes, and snippets.

@camckin10
Last active January 14, 2017 00:15
Show Gist options
  • Save camckin10/eea94a7d1855dd883429fa72d0cf0d60 to your computer and use it in GitHub Desktop.
Save camckin10/eea94a7d1855dd883429fa72d0cf0d60 to your computer and use it in GitHub Desktop.
pedro-drills.js
//word counter drill
function wordCounter(word_list) {
}
// Should return { apple: 3, orange: 1 }
wordCounter(['apple', 'apple', 'apple', 'orange']);
function wordCounter(word_list){
var fruit = {};
fruit['apple']=3;
fruit['orange'];
return fruit;
}
console.log(wordCounter(word_list))
// Should return { dog: 3, cat: 2 }
wordCounter(['dog', 'dog', 'cat', 'dog', 'cat']);
function wordCounter(word_list) {
var animals = {};
animals['dog'] = 3;
animals['cat'] = 2;
return animals
}
console.log(wordCounter(word_list))
//how many words drill
function howManyWords(word_list) {
}
howManyWords(['a', 'lot', 'of', 'them']);
// should return 4.
// A simple solution would be:
function howManyWords(word_list) {
return word_list.length;
}
// But you are not allowed to do that, instead
// use forEach. I'm counting on you :D
function objectFromPair(key,value){
var obj = {};
obj[key] = value;
return obj;
}
console.log(objectFromPair('alface', 'crespa'));
console.log(objectFromPair('dog', 'Snoop'));
console.log(objectFromPair('cat', 'Garfield'));
// Given an object and a key, increases the value associated with the key by 1
// increaseCounter({ dogs: 1 }, 'dogs') => { dogs: 2 }
// increaseCounter({ answerToTheUniverse: 42, everythingElse: 42 }, 'answerToTheUniverse') => { answerToTheUniverse: 43, everythingElse: 42 }
function increaseCounter(object, key) {
object[key] = object[key] +1;
return object;
}
console.log(increaseCounter({ answerToTheUniverse: 42, everythingElse: 42 }, 'answerToTheUniverse'));
Possible answers
//how many words drill
function howManyWords(word_list) {
var count = 0;
word_list.forEach(function(element) {
console.log(element);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment