Unsolved
Last updated: 4:15 p.m.
Where I'm at
I have three categories for people to choose: Gender, Age, Ethnicity. Each with their own set of options Male/Female; under 35, 36-64, 65+; White, Black, Aboriginal, Metis and Asian, respectively.
When one of the options is clicked, the number should subtract from var total
(which is the number of MLAs). I've set global variables for the numbers, which changes the span.number
located in my index.html
file to say, "There are x-number of MLAs that fit in your demographic."
Where I'm stuck
a) I'm looking for a more efficient way of doing the math in Javascript, right now it's pretty janky. If you look at the Gender option in scripts.js
, I've created a variable var resultMale = total - female
but
it gets a lot less pretty when generating span.number
relies on the result of the previously selected option.
b) I've tried using the pipe boolean, but I'm unclear about how in the second category "Age", which includes var resultLow = resultMale || resultFemale -(middle + high)
how it will know what has been clicked/chosen in category "Gender" so that it can do the proper math.
scripts.js
$(function() {
// Numbers
var total = 56
var male = 41;
var female = 15;
var low = {}
var middle = {}
var high = {}
var white = 41
var black = 0;
var aboriginal = null;
var metis = null;
var asian = null;
// MLAs
var MLAs = [
{
"Name": "Nancy Allan",
"Age": 62,
"Constuency": "St. Vital",
"Party": "NDP",
"Gender": "Female",
"Ethnicity": "White"
},
{
"Name": "James Allum",
"Age": null,
"Constuency": "Fort Garry-Riverview",
"Party": "NDP",
"Gender": "Male",
"Ethnicity": "White"
},
{
"Name": "Rob Altemeyer",
"Age": null,
"Constuency": "Wolseley",
"Party": "NDP",
"Gender": "Male",
"Ethnicity": "White"
}]
// Option #1: Gender
$( ".G1" ).click(function() {
$(".G2").toggleClass("disabled");
$(".headshot").not(".Female").toggleClass("opacity");
var resultMale = total - female;
$(".number").html(resultMale);
});
$( ".G2" ).click(function() {
$(".G1").toggleClass("disabled");
$(".headshot").not(".Male").toggleClass("opacity");
var resultFemale = total - male
$(".number").html(resultFemale);
});
// Option #2: Age
$( ".A1" ).click(function() {
$(".A2").toggleClass("disabled");
$(".A3").toggleClass("disabled");
$(".Low").toggleClass("show");
var resultLow = resultMale || resultFemale - (middle + high);
$(".number").html("?");
});
$( ".A2" ).click(function() {
$(".A1").toggleClass("disabled");
$(".A3").toggleClass("disabled");
$(".Medium").toggleClass("show");
var resultMiddle = resultMale || resultFemale - (low + high);
$(".number").html("?");
});
$( ".A3" ).click(function() {
$(".A1").toggleClass("disabled");
$(".A2").toggleClass("disabled");
$(".High").toggleClass("show");
var resultHigh = resultMale || resultFemale - (low + middle);
$(".number").html("?");
});
// OLD
$( ".E1" ).click(function() {
$( ".White" ).toggleClass("show");
var resultWhite = resultLow || resultMiddle || resultHigh - (black + aboriginal + metis + asian);
$(".number").html(resultWhite);
});
$( ".E2" ).click(function() {
$( ".Black" ).toggleClass("show");
var resultBlack = resultLow || resultMiddle || resultHigh - (white + aboriginal + metis + asian);
$(".number").html("0");
});
$( ".E3" ).click(function() {
$( ".Aboriginal" ).toggleClass("show");
var resultAboriginal = resultLow || resultMiddle || resultHigh - (white + black + metis + asian);
$(".number").html(null);
});
$( ".E4" ).click(function() {
$( ".Metis" ).toggleClass("show");
var resultMetis = resultLow || resultMiddle || resultHigh - (white + black + aboriginal + asian);
$(".number").html(null);
});
$( ".E5" ).click(function() {
$( ".Asian" ).toggleClass("show");
var resultAsian = resultLow || resultMiddle || resultHigh - (white + black + aboriginal + metis);
$(".number").html(null);
});
// Option #3: Ethnicity
// $("input[name='ethnicity']").on("change", function() {
// var $checkedbox = $("input[name='ethnicity']:checked");
// if($checkedbox.length >= 2)
// {
// var $uncheckedbox = $("input[name='ethnicity']:not(:checked)");
// $.each($uncheckedbox,function() {
// $(this).attr("disabled", "disabled");
// });
// }
// else
// {
// $("input[name='ethnicity']").removeAttr("disabled");
// }
// });
// Shows a popup with MLA information
$(".headshot").click(function(){
var idx = $(this).index() - 1;
$(".tooltip").fadeIn("slow");
$(".tooltipName").html(MLAs[idx].Name);
$(".tooltipParty").html(MLAs[idx].Party);
$(".tooltipConstuency").html(MLAs[idx].Constuency);
$(".tooltipEthnicity").html(MLAs[idx].Ethnicity) + ",";
$(".tooltipAge").html(MLAs[idx].Age);
});
// Bounce and show result
$(".rect").click(function(){
console.log("Bounce test");
$(".others").fadeIn("slow");
$(".others").effect( "bounce",
{times:3}, 600 );
});
// This hides the footer on click
$(".crossContainer").click(function(){
$("footer").slideUp("slow", function(){
console.log("No feedback makes us sad.");
});
});
// This hides the credits
$(".credits").hide();
});
index.html (includes span.number)
<!-- Three options readers can click -->
<section class="interactive clearfix">
<section class="selection" id="selection">
<div class="gender">
<p class="category">Gender</p>
<div class="options">
<p class="rect G1">Male</p>
<p class="rect G2">Female</p>
</div><!-- /.options -->
</div><!-- /.gender -->
<div class="age">
<p class="category">Age</p>
<div class="options">
<p class="rect A1">Under 35</p>
<p class="rect A2">36-64</p>
<p class="rect A3">65+</p>
</div><!-- /.options -->
</div><!-- /.age -->
<div class="ethnicity">
<p class="category">Ethnicity<span>*<span></p>
<div class="options">
<p class="rect E1 E">White</p>
<p class="rect E2 E">Black</p>
<p class="rect E3 E">Aboriginal</p>
<p class="rect E4 E">Metis</p>
<p class="rect E5 E">Asian</p>
</div><!-- /.options -->
</div><!-- /.ethnicity -->
</section>
<section class="others">
<h2>There are <span class="number">56</span> MLAs that fit in your demographic</h2>
<figcaption class="special">(Does not include the single vacant seat for the Pas.)</figcaption>
</section>