Skip to content

Instantly share code, notes, and snippets.

@camckin10
Last active July 19, 2017 00:00
Show Gist options
  • Save camckin10/19b2551dccdbae355753a792cb90f2e8 to your computer and use it in GitHub Desktop.
Save camckin10/19b2551dccdbae355753a792cb90f2e8 to your computer and use it in GitHub Desktop.
emaildrills.js
JS Exercises sent by Anna(Thinkful) through email 7-13-17
PRACTICE THE FOLLOWING:
FUNCTION EXERCISES
1) Take this array, carData and write a function
that returns the highest value of the property of year:
2) THEN write a function that returns all the models,
in Uppercase and sorts them. --alphabetically?
3) THEN write a function that replaces all the
properties of red and turns the value into cherry red.--2 objects that have property red.
--------------------------------------------------------------------------------------------------
Take this array, carData and write a function
that returns the highest value of the property of year:
var carData=[
{
model:'Camry',
color:'red',
year:2013
},
{
model:'Accord',
color:'black',
year:2016
},
{
model:'Optima',
color:'silver',
year:2015
},
{
model: 'focus',
color: 'blue',
year:2014
},
{
model: 'i8',
color: 'brown',
year: 2017
},
{
model: 'CTS',
color: 'white',
year: 2012
},
{
model: 'pruies',
color: 'red',
year: 2011
}
];
THEN write a function that returns all the models,
in Uppercase and sorts them. --alphabetically?
**problem begins**
Function inUppercase(array) {
Return array.toUppercase.sort;
}
console.log((carData.model));
*Correct answer worked w/ Anna *
var carData=[
{
model:'Camry',
color:'red',
year:2013
},
{
model:'Accord',
color:'black',
year:2016
},
{
model:'Optima',
color:'silver',
year:2015
},
{
model: 'focus',
color: 'blue',
year:2014
},
{
model: 'i8',
color: 'brown',
year: 2017
},
{
model: 'CTS',
color: 'white',
year: 2012
},
{
model: 'pruies',
color: 'red',
year: 2011
}
];
function inUppercase(array) {
return array.toUppercase.sort;
}
//console.log(inUppercase(carData.model));
function exampleFunction(data){
var copy = [];
data.forEach(function(elem){
copy.push(elem.year);
})
return copy.sort();
}
console.log(exampleFunction(carData));
THEN write a function that replaces all the
properties of red and turns the value into cherry red.--2 objects that have property red.
**problem begins**
Var redCars = [
Var objectOne= {
model:'Camry',
color:'red',
Year:2013
},
Var objectTwo = {
model: 'pruies',
color: 'red',
year: 2011
}
Function changeColor(object) {
Delete redCars.color[“red”];
redCars.Color[]=”cherryred”;
}
------------------------------------------------------------------
*worked w/anna 7-18-17
var redCars = [
{
model: 'Camry',
color: 'red',
year: 2013
},
{
model: 'Preis',
color: 'red',
year: 2011
}
]
redCars.forEach(function(val, index) {
// val and arr[index] point to the same object
// so changing the object that val points to changes the object that
// arr[index] points to
val.color = "cherry red";
})
alert(redCars[0].color);
alert(redCars[1].color);
console.log(arr);
Now if we wanted to write a function... how might we do this?
Let's have you practice and show me how you might
change the color to Cherry Red with a function.
*Chelsey work through w/ cleaner JS problem*
var redCars = [
{
model: 'Camry',
color: 'red',
year: 2013
},
{
model: 'Preis',
color: 'red',
year: 2011
}
]
function changeColor(object){
object.color = 'cherry red';//object is changing property color to
//'cherry red'
return object;//returning object
}
console.log(redCars);//printing array
for(var i =0; i < redCars.length; i ++){//for loop is indexing through
//array redCars
redCars[i] = changeColor(redCars[i]);//redCars in the i position is
//equal to the function with the array(object name) in the 0 position.
}
console.log(redCars);//printing array with new color change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment