Skip to content

Instantly share code, notes, and snippets.

@rghosh8
Created November 28, 2018 03:56
Show Gist options
  • Save rghosh8/0a7db8b99a6dadfc2201b1020db76819 to your computer and use it in GitHub Desktop.
Save rghosh8/0a7db8b99a6dadfc2201b1020db76819 to your computer and use it in GitHub Desktop.
//Write a function identity which expects 1 argument and simply returns that input value.
//Calling your function should result in:
//identity("hello world"); //"hello world"
//identity(500); //500
/// Exercise 1
console.log("Exercise 1")
function identity(argument){
return argument;
}
console.log(identity("Movie 300"))
/// Exercise 2
//Exercise 2 (conditionals)
//Write a function coffeeLover which takes in a boolean value, and returns the string "Enjoy a cup for free!" if the input is true, or "Take a look at our other drinks!" if the input is false.
//Calling your function should result in:
console.log("Exercise 2")
function coffeeLover(arg){
if (arg === true){
return "Enjoy a cup for free!"
}
if (arg === false){
return "Take a look at our other drinks!"
}
}
console.log(coffeeLover(true)); //"Enjoy a cup for free!"
console.log(coffeeLover(false)); //"Take a look at our other drinks!"
///Exercise 3 (objects, conditionals)
console.log("Exercise 3")
//Write a function coffeeLoverExtended which takes in an object with the structure like:
var customer001 = {
name: "John Riley",
ticketNumber: "A01",
enjoysCoffee: true
};
//likewise..
var customer002 = {
name: "Harold Crane",
ticketNumber: "A02",
enjoysCoffee: false
};
/*..and returns the string "Enjoy a cup for free!" if the property enjoysCoffee has a value of true, or "Take a look at our other drinks!" if the enjoysCoffee property has a value of false*/
//Calling your function should result in:
function coffeeLoverExtended(customerID){
if (customerID['enjoysCoffee'] === true){
return "Enjoy a cup for free!"
}
if (customerID['enjoysCoffee'] === false){
return "Take a look at our other drinks!"
}
}
console.log(coffeeLoverExtended(customer001)); //"Enjoy a cup for free!"
console.log(coffeeLoverExtended(customer002)); //"Take a look at our other drinks!"
/// Exercise 4
console.log("Exercise 4")
//Exercise 4 (numbers)
/*Write a function convertToKilometers which expects a number of miles passed in, and returns that number multiplied by 1.60934 (an accepted approximation of 1 mile in kilometers).*/
function convertToKilometers(miles) {
//you do not have to worry about rounding to any number of decimal places. Our tests will do that for you.
var conversionFactor = 1.60934;
return conversionFactor * miles;
}
//Calling your function should result in:
console.log(convertToKilometers(50)); //80.467
console.log(convertToKilometers(361)); //580.973
/// Exercise 5
console.log("Exercise 5")
//Exercise 5 (conditionals and numbers)
/* Write a function isGoodFreethrowShooter which takes in a shooting percentage (a number you can assume will be between 0 and 1), and returns based on the following criteria: */
function isGoodFreethrowShooter(arg){
if (arg>=0 && arg<0.65){
return "Horrible freethrow shooter"
}
if (arg>=0.65 && arg<0.80){
return "Decent freethrow shooter"
}
if (arg>=0.80 && arg<=1.00){
return "Great freethrow shooter"
}
}
/*If the number is between 0-0.65, return "Horrible freethrow shooter"
If the number is between 0.65-0.80 return "Decent freethrow shooter"
If the number is between 0.80-1.00 return "Great freethrow shooter"
Each range is inclusive of the lower bound, and exclusive of the upper -- in other words, 0.65 is counted as a "Decent freethrow shooter" (not "Horrible freethrow shooter") and 0.80 is considered a "Great freethrow shooter" (not "Decent freethrow shooter").*/
//Calling your function should result in:
console.log(isGoodFreethrowShooter(0.90)); //"Great freethrow shooter"
console.log(isGoodFreethrowShooter(0.09)); //"Horrible freethrow shooter"
console.log(isGoodFreethrowShooter(0.75)); //"Decent freethrow shooter"
console.log(isGoodFreethrowShooter(0.65));
console.log(isGoodFreethrowShooter(0.80));
/// Exercise 6
///Exercise 6 (strings)
console.log("Exercise 6")
//Calling your function should result in:
function dogsIWouldPet(arg){
if (arg === "ottoman"){
return "I would pet dogs no bigger than an ottoman"
}
if (arg === "small horse"){
return "I would pet dogs no bigger than an small horse"
}
if (arg === "Terrier"){
return "I would pet dogs no bigger than an Terrier"
}
if (arg === "I do not like dogs"){
return "I would not pet dogs"
}
}
console.log(dogsIWouldPet("ottoman")); //"I would pet dogs no bigger than an ottoman"
console.log(dogsIWouldPet("small horse")); //"I would pet dogs no bigger than an small horse"
console.log(dogsIWouldPet("Terrier")); //"I would pet dogs no bigger than an Terrier"
console.log(dogsIWouldPet("I do not like dogs"))
/*Bonus (extra): If your function were passed in a string "I do not like dogs", you can have your function return "I would not pet dogs".*/
/// Exercise 7
console.log("Exercise 7")
/* Write a function isTallEnough which takes in a height (number) in inches, and returns true if the input is greater than or equal to 48, and false if it's less than 48 inches.
Calling your function should result in: */
function isTallEnough(arg){
return arg >= 48
}
console.log(isTallEnough(72)); //true
console.log(isTallEnough(28)); //false
console.log(isTallEnough(48)); // true
/// Exercise 8
/*Exercise 8 (objects)
console.log("Exercise 8")
Write a function favoriteDessert which takes in no arguments, but returns an object with 3 key-value pairs. The names of each property and their respective values are up to you, but the values cannot be undefined. How would you describe your favorite dessert?
For example, one could return an object such as:
{ name: "Flan", ovenTemp: 350, prepTime: "25 minutes + 50 minutes chilling" }
Another example could be:
{ name: "Gelato", countryOfOrigin: "Italy", flavor: "Chocolate Chip" }*/
function favoriteDessert(){
return {
name: "Gulab Jamun",
ingredient: "Flour",
color: "Dark Brown"
}
}
console.log(favoriteDessert())
/// Exercise 9
///Exercise 9 (loops)
console.log("Exercise 9")
/*Write a function countNumOfStrings which takes in an array of mixed elements (different data types), and returns the number of strings there are in the array.*/
//Calling your function should result in:
function countNumOfStrings(arr){
var count = 0;
for (var i in arr){
if (typeof arr[i]=== 'string'){
count++;
}
}
return count;
}
console.log(countNumOfStrings(["hello", "world", 5, true, {}])); //2
console.log(countNumOfStrings(["foo", 2, 10, "bar", 5, false, {}, "baz"])); //3
console.log(countNumOfStrings(["There is only one string in this example"])); //1
//Hint: Recall typeof? If not: check it out on MDN. :-)
console.log("Exercise 10")
/*Write a function listLengthOfAllWords which takes in an array of words (strings), and returns an array of numbers representing the length of each word.*/
function listLengthOfAllWords(arr){
var lenWord = []
for (var i in arr){
lenWord.push(arr[i].length)
}
return lenWord;
}
//Calling your function should result in:
console.log(listLengthOfAllWords(['hello', 'world', 'I', 'can', 'code'])); //[5, 5, 1, 3, 4];
console.log("Exercise 11")
var groupA = [
{
name: "Mia",
age: 10,
heightInInches: 52
},
{
name: "Jaya",
age: 9,
heightInInches: 45
},
{
name: "Kiana",
age: 10,
heightInInches: 55
},
{
name: "Alex",
age: 11,
heightInInches: 48
}
]
/*Write a function tallEnoughToRide which takes in an array of people objects, and returns a an array of names of people who are greater than or equal to 48 inches in height.*/
function tallEnoughToRide(arr){
var name = [];
for (var i in arr){
if (arr[i]['heightInInches'] >= 48){
name.push(arr[i]['name'])
}
}
return name;
}
console.log(tallEnoughToRide(groupA))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment