Practice Set - Conditionals & Blocks within Braces

by Angela Baruth

HTML

<h3>Conditionals &amp; Blocks within Braces</h3>


<h4>2 Tasks for this Practice Set:</h4>

<ol>
    <li>Use conditionals to create code which outputs a sentence to the console stating whether the variable <i>x</i> is greater to, equal to, or less than zero. It should work for any numeric value of x</li>
    <li>Look at the code in this section, and fix it so that the output is correct. In your console, it should look like this (Hint: it involves braces) :
        <img src="http://courses.dce.harvard.edu/~cscie3/img/captains.png" title="program output" style="width:80%;padding-top:.5em;">
    </li>
</ol>

JavaScript

"use strict";

// Insert your code for #1 here
//Use conditionals to create code which outputs a sentence to the console stating whether the variable x is greater to, equal to, or less than zero. It should work for any numeric value of x

var x = 2;

if (x > 0) {
	console.log(x + " is greater than 0.");
} else if (x == 0) {
	console.log(x + " is equal to 0.");
} else if (x < 0) {
	console.log(x + " is less than 0.");
} else {
	console.log(x + " cannot be evaluated in relation to 0. Is " + x + " really a number?");
}

// Insert your code for #2 here
// Look at the code in this section, and fix it so that the output is correct. In your console, it should look like this (Hint: it involves braces) : 

var myCaptains = ["James T. Kirk", "Jean-Luc Picard", "Katherine Janeway"];

//capitalize each captains' name by looping through the array
for (var i = 0; i < myCaptains.length; i++) {
    myCaptains[i] = myCaptains[i].toUpperCase();
    console.log("The Captain in Capitals is " + myCaptains[i]);
}

//tell the user how many captains' names have been capitalized
console.log(i + " captains have been capitalized");