Built with blockbuilder.org
xxxxxxxxxx
<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 5 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).
console.log("Question 1")
array1 = [
{name: "Arizona", location: "Southwest"},
{name: "Virginia", location: "East"},
{name: "Florida", location: "Southeast"}
];
array1.forEach(function(e){
if (e.location == "East"){
console.log(e.name)
}
})
// Problem 2: search array1 (above) for those states whose location contains the substring "South"
// using console.log(), print just the state name(s) for these states.
console.log("Question 2")
array1.forEach(function(s){
if (s.location.includes("South")){
console.log(s.name)
}
})
// console.log(array1)
// Problem 3: using iteration and console.log(), print those items that are in
// array2 but NOT in array3
console.log("Question 3")
array2 = ['a','b','c', 65, 'd'];
array3 = ['a','c','e','f','g', 87];
array2.forEach(function(a2){
console.log(a2)
if(array3.includes(a2) == false){
console.log(array3.includes(a2))
}
})
// Problem 4: 'states' is an array of objects. Sort the array in ascending order
// by state name
console.log("Question 4")
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}]
states.sort(function(a,b){
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase()
if(nameA < nameB)
return -1
if(nameA > nameB)
return 1
})
console.log(states)
//OR
states.sort(function(a,b){
return d3.ascending(a.name, b.name)
})
console.log(states)
//OR
states.sort(function(a,b){
if(a.name < b.name){return -1}
else if(a.name == b.name){return 0}
else {return 1}
})
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.forEach(function(p){
var pop=p.population
if(pop % 2 == 0)
console.log(p.name)
})
//OR
states.forEach(function(s){
console.log(s.name, s.population, s.population % 2==0)
if(s.population % 2==0) {console.log(s)}
})
//MORE
states2 = [{name: "Alaska", id: "AK", population: 741894},
{name: "Virginia", id: "VA", population: 8411808},
{name: "Arizona", id: "AZ", population: 6931071},
{name: "Florida", id: "FL", population: 20984400}]
// Problem 1: using d3.mean(), compute and print the average (mean)
// population of the states in the states array
console.log("Question 1B")
var popMean = d3.mean(states2, function(i){
return i.population
})
console.log(popMean)
// Problem 2: WITHOUT using d3.mean(), compute and print the average (mean)
// population of the states in the states array
console.log("Question 2B")
var average=0;
for(i=0; i < states2.length; i++){
average += states2[i].population;
}
average = average / states2.length;
console.log(average)
//OR
var populations=0;
var numberOfStates=0;
states2.forEach(function(d){
populations += d.population;
numberOfStates++;
})
var mean = populations/numberOfStates;
console.log(mean)
//OR
//???? not working...
var populations2=0
states2.forEach(function(d){
populations2 += d.population;
var mean2=populations2/states2.length;
console.log("mean2", mean2)
})
//OR
//Just kidding...
var statesNumber
var pop5
while(pop5 in states2){
statesNumber = statesNumber+1
pop5 += d.population
}
var mean3 = pop5/states.length
console.log("mean3", mean3)
var pop4 = 0
var mean4 = 0
var i = 0
while(i < states2.length){
pop4 += states2[i].population;
// += basically equals pop4 = pop4 + states2...
i++;}
mean4= pop4/states.length
console.log("mean4", mean4)
</script>
</body>
https://d3js.org/d3.v4.min.js