text: function init() {
    // Grab a reference to the dropdown select element
    var selector = d3.select("#selDataset");
  
    // Use the list of sample names to populate the select options
    d3.json("samples.json").then((data) => {
      var sampleNames = data.names;
  
      sampleNames.forEach((sample) => {
        selector
          .append("option")
          .text(sample)
          .property("value", sample);
      });
  
      // Use the first sample from the list to build the initial plots
      var firstSample = sampleNames[0];
      buildCharts(firstSample);
      buildMetadata(firstSample);
    });
  }
  
  // Initialize the dashboard
  init();
  
  function optionChanged(newSample) {
    // Fetch new data each time a new sample is selected
    buildMetadata(newSample);
    buildCharts(newSample); 
  }
  
  // Demographics Panel 
  function buildMetadata(sample) {
    d3.json("samples.json").then((data) => {
      var metadata = data.metadata;
      // Filter the data for the object with the desired sample number
      var resultArray = metadata.filter(sampleObj => sampleObj.id == sample);
      var result = resultArray[0];
      // Use d3 to select the panel with id of `#sample-metadata`
      var PANEL = d3.select("#sample-metadata");
  
      // Use `.html("") to clear any existing metadata
      PANEL.html("");
  
      // Use `Object.entries` to add each key and value pair to the panel
      // Hint: Inside the loop, you will need to use d3 to append new
      // tags for each key-value in the metadata.
      Object.entries(result).forEach(([key, value]) => {
        PANEL.append("h6").text(`${key.toUpperCase()}: ${value}`);
      });
  
    });
  }
  
  // 1. Create the buildCharts function.
  function buildCharts(sample) {
  
    // 2. Use d3.json to load and retrieve the samples.json file 
    d3.json("samples.json").then((data) => {
  
      // 3. Create a variable that holds the samples array. 
      var sampleInfo = data.samples;
  
      // 4. Create a variable that filters the samples for the object with the desired sample number.
      var sampleInfoArray = sampleInfo.filter(sampleObj => sampleObj.id == sample);
  
      // Deliverable (3): 1. Create a variable that filters the metadata array for an object
      var metaInfoArray = data.metadata.filter(sampleObj => sampleObj.id == sample);
  
      //  5. Create a variable that holds the first sample in the array.
      var sampleFirst = sampleInfoArray[0]
  
      // Deliverable (3): 2. Create a variable that holds the first sample in the array
      var metaFirst = metaInfoArray[0]
  
      // 6. Create variables that hold the otu_ids, otu_labels, and sample_values.
      var otu_ids = sampleFirst.otu_ids;
      var otu_labels = sampleFirst.otu_labels;
      var sample_values = sampleFirst.sample_values;
  
      // Deliverable (3): 3. Create a variable that converts the washing frequency to a floating point number
      var wfreq = metaFirst.wfreq;
  
      // 7. Create the yticks for the bar chart.
      // Hint: Get the the top 10 otu_ids and map them in descending order  
      //  so the otu_ids with the most bacteria are last. 
  
      var yticks = otu_ids.slice(0,10);
      let y = yticks.map(tt => `OTU ${tt}`).reverse();
  
      // 8. Create the trace for the bar chart. 
      var barData = [{
        x: sample_values.slice(0,10).reverse(),
        y: y,
        text: otu_labels.slice(0,10).reverse(),
        type: "bar",
        orientation: "h"
      }];
  
      // 9. Create the layout for the bar chart. 
      var barLayout = {
       title: "Top 10 Bacteria Cultures Found",
       margin: {
         l: 100,
         r: 100,
         t: 100,
         b: 100
       }
      };
  
      // 10. Use Plotly to plot the data with the layout. 
      Plotly.newPlot("bar", barData, barLayout);
  
  // Bulble and Gauge 
      // 1. Create the trace for the bubble chart.
      var bubbleData = [{
        x: otu_ids,
        y: sample_values,
        text: otu_labels,
        mode: 'markers',
        marker: {
         size: sample_values, 
         color: otu_ids,
         colorscale: "viridis"
        } 
      }];
  
      // 2. Create the layout for the bubble chart.
      var bubbleLayout = {
        title: "Bacteria Species per Sample",
        xaxis: {title: "Otu Id"},
        hovermode: "closest",
        showlegend: false
      };
  
      // 3. Use Plotly to plot the data with the layout.
      Plotly.newPlot("bubble", bubbleData, bubbleLayout); 
  
      // 4. Create the trace for the gauge chart.
      var gaugeData = [{
        title: {text: "Belly button Washing Frequency <br> scrubs per Week"},
        mode: "gauge+number",
        type: "indicator",
        value: wfreq,
        gauge: {
          axis: {range: [null,10]},
          bar: {color: "black"},
          steps: [
            {range: [0,2], color: "red"},
            {range: [2,4], color: "orange"},
            {range: [0,2], color: "yellow"},
            {range: [0,2], color: "lightgreen"},
            {range: [0,2], color: "green"}
          ]
        }
      }  
      ];
        
      // 5. Create the layout for the gauge chart.
      var gaugeLayout = { 
        width: 500,
        height: 460,
        margin: {t:0, b:0}   
      };
    
      // 6. Use Plotly to plot the gauge data and layout.
      Plotly.newPlot("gauge", gaugeData, gaugeLayout);
    });
  }
  