Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created January 8, 2018 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/503b4974bab4a571eb724b2794236286 to your computer and use it in GitHub Desktop.
Save amandarfernandes/503b4974bab4a571eb724b2794236286 to your computer and use it in GitHub Desktop.
JSDestructuringExercises
/*
Write a function called displayStudentInfo which accepts an object and returns the string "Your full name is" concatenated with the value of the first key and a space and then the value of the last key. See if you can destructure this object inside of the function.
Examples:
displayStudentInfo({first: 'Elie', last:'Schoppik'}) // 'Your full name is Elie Schoppik')
*/
function displayStudentInfo(obj){
var {first,last} = obj;
return `Your full name is ${first} ${last}`;
}
/*
Write a function called printFullName which accepts an object and returns the string "Your full name is" concatenated with the value of the first key and a space and then the value of the last key. See if you can destructure this object DIRECTLY from the parameters. The output of the printFullName function should be the exact same as the displayStudentInfo function.
Examples:
printFullName({first: 'Elie', last:'Schoppik'}) // 'Your full name is Elie Schoppik'
*/
// you will have to pass in the correct parameters for this function!
function printFullName({first,last}){
return `Your full name is ${first} ${last}`;
}
/*
Write a function called createStudent which accepts as a parameter, a default parameter which is a destructured object with the key of likesES2015 and value of true, and key of likesJavaScript and value of true.
If both the values of likesJavaScript and likesES2015 are true, the function should return the string 'The student likes JavaScript and ES2015'.
If the value of likesES2015 is false the function should return the string 'The student likes JavaScript!'
If the value of likesJavaScript is false the function should return the string 'The student likesES2015!'
If both the value of likesJavaScript and likesES2015 are false, the function should return the string 'The student does not like much...'
Examples:
createStudent() // 'The student likes JavaScript and ES2015')
createStudent({likesES2015:false}) // 'The student likes JavaScript!')
createStudent({likesJavaScript:false}) // 'The student likes ES2015!')
createStudent({likesJavaScript:false, likesES2015:false}) // 'The student does not like much...')
*/
// you will have to pass in the correct parameters for this function!
function createStudent({likesES2015=true,likesJavaScript=true}={}){
var stmt = 'The student ';
if (likesJavaScript && likesES2015) {
stmt += 'likes JavaScript and ES2015!';
} else if (!likesJavaScript && !likesES2015) {
stmt += 'does not like much...';
} else if (!likesJavaScript) {
stmt += 'likes ES2015!';
} else {
stmt += 'likes JavaScript!';
}
return stmt;
}
/*
Write a function called reverseArray which accepts an array and returns the array with all values reversed. See if you can do this without creating a new array!
Examples:
reverseArray([1,2,3,4,5]) // [5,4,3,2,1]
reverseArray([1,2]) // [2,1]
reverseArray([]) // []
reverseArray([1,2,3,4,5,6,7,8,9,10]) // [10,9,8,7,6,5,4,3,2,1
*/
function reverseArray(arr){
var len = arr.length;
for(let i=0,j=len-1; i<j;i++,j--) {
[arr[i],arr[j]] = [arr[j],arr[i]];
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment