Skip to content

Instantly share code, notes, and snippets.

@Craftbd
Last active March 1, 2018 15:31
Show Gist options
  • Save Craftbd/5874ebd9cbe63f146a8fcef26673092e to your computer and use it in GitHub Desktop.
Save Craftbd/5874ebd9cbe63f146a8fcef26673092e to your computer and use it in GitHub Desktop.
Sample problems 2: objects and arrays
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<h1>Please look in the code below for 4 JavaScript practice problems.</h1>
<p>(These problems don't include any SVG manipulation, so you will need to open the developer console in order to see the output of your code)</p>
<script>
// Problem 1: search array1 for those states whose location is "East"
// using console.log(), print just the state name(s).
array1 = [
{name: "Arizona", location: "Southwest"},
{name: "Virginia", location: "East"},
{name: "Florida", location: "Southeast"}
];
console.log("QUESTION 1")
array1.forEach(function(s) {
if(s.location == 'East') {
console.log(s)
}
});
// Problem 2: search array1 (above) for those states whose location contains the substring "South"
console.log("QUESTION 2")
array1.forEach(function(s) {
if(s.location.includes("South")) {
console.log(s)
}
});
// using console.log(), print just the state name(s) for these states.
// Problem 3: using iteration and console.log(), print those items that are in
// array2 but NOT in array3
var array2 = ['a','b','c', 65, 'd'];
var array3 = ['a','c','e','f','g', 87];
console.log("QUESTION 3")
array2.forEach(function (i){
if (array3.includes(i) == false){
console.log(i)
}
})
// Problem 4: 'states' is an array of objects. Sort the array in ascending order
// by state name
states = [{name: "Alaska", id: "AK", population: 741894},
{name: "Virginia", id: "VA", population: 8411808},
{name: "Arizona", id: "AZ", population: 6931071},
{name: "Florida", id: "FL", population: 20984400}]
console.log("QUESTION 4")
//states.sort(functon (a,b) {
// return d3.ascending ( a.name, b.name)
// });
console.log(states)
// Problem 5: write code that determines which state(s) have a population value that is an even number. Print out the state(s) to the console
console.log("QUESTION 5")
states.sort(function(a,b){return(a.population)<(b.population)});
console.log(states)
states.forEach(function(i){
console.log(i.name, i.population,)
if (i.population % 2 == 0) {
console.log(i)
}
})
/* Question 2 on problem #3
var average = 0;
console.log(states.length)
for (i=0, i< state.length; i++){
average += state[i].population;
}
average = average / states.length;*/
/*
var populations = 0;
var numberofstates = 0;
states.forEach(function(d){
population += d.population;
numberofStates++;
})
var mean = populations / numberofStates;
or**** var mean2 = populations / states.length;
console.log(" solution 3: ", mean) */
// var x;
// x = x+1
//x+ = 1;
//x++
// all the same type
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment