D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
annalabrozzi
Full window
Github gist
Sample problems 2-27
Built with
blockbuilder.org
<!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). console.log("Question 1") var array1 = [ {name: "Arizona", location: "Southwest"}, {name: "Virginia", location: "East"}, {name: "Florida", location: "Southeast"} ]; array1.forEach(function(a) { if (a.location == "East") { console.log(a.name) } }) console.log(".") console.log(".") // 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") var array1 = [ {name: "Arizona", location: "Southwest"}, {name: "Virginia", location: "East"}, {name: "Florida", location: "Southeast"} ]; array1.forEach(function(a) { if (a.location.includes("South")) console.log(a.name) }) console.log(".") console.log(".") // 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(c){ if (array3.includes(c)){ return false } console.log(c) }) /*array2.forEach(function(i){ console.log(i) if (array3.includes(i) == false) { console.log(i) } })*/ console.log(".") console.log(".") // Problem 3: 'states' is an array of objects. Sort the array in ascending order // by state name, return 0 is a tie console.log("Question 4") var 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 states.sort(function(a, b){ if(a.name < b.name) return -1; if(a.name > b.name) return 1; return 0; }) console.log(states) //another way /* states.sort(function(a,b){ return d3.ascending(a.name, b.name) })*/ // Problem 4: 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(i) { if (i.population % 2 == 0){ console.log(i.name) } }); </script> </body>
https://d3js.org/d3.v4.min.js