<h3>Conditionals & 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
// This code runs if expressions, evaluates x to true and prints corresponding message to console
var x = 9;
// We'll see if x is greater than zero
if ( x > 0) {
console.log("x is greater than 0");
// Equal to zero
} else if ( x === 0) {
console.log("x is equal to 0");
// Or less than zero
} else {
console.log("x is less than 0");
};
// Insert your code for #2 here
var myCaptains = ["James T. Kirk", "Jean-Luc Picard", "Katherine Janeway"];
// Placed braces to correct output
// Initialize the array to 0 and continue through the loop until the length of the array is reached
for (var i = 0; i < myCaptains.length; i++) {
// Each item in the arry will get capitalized
myCaptains[i] = myCaptains[i].toUpperCase();
// Print the array item in the console concatenated to a string
console.log("The Captain in Capitals is " + myCaptains[i]);
}
// Placed outside of the loop since we want one final total of the lenght of the array
console.log(i + " captains have been capitalized");
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.