JavaScript Logical Operators

Things are getting a little more complicated, but I love how we’re learning new skills and building on the concepts we’ve already practiced. That was the case this week in learning about using logic in our programs. I took some time to get my head around, but I think I’ve got a decent grasp on the concept and how to use the logical operators ! (not), && (and), || (inclusive or), and ^ (exclusive or).

I started working with a tutor, Brady Nelson, this week to help me review and cement the concepts, and I’ll continue doing that as I go forward. He helped me work through my assignment programs and get them to a point I was happy with, and I had some general JavaScript questions that he was able to help me answer.

I received some helpful feedback from a couple of fellow students who reviewed the code I submitted. One of them was where I’d left an important condition out, which caused my program to function only about half the time. After fixing that issue and realizing I needed some additional parentheses in the statement, my code worked perfectly.

As we’ve been creating programs this semester I’ve sometimes made them a little more complicated than the assignments required simply because I wanted to make them more user friendly. I have a tendency to consider programs from a user experience perspective, and I want to make sure that programs I write are not only functional, but also intuitive.

For example, in my school status calculator, I added a message that reminded the user to input a valid number if the happened to input a non-numerical value. For my discount & total price calculator, I added an additional output if there was a discount applied. This requires a little more work, but I enjoy it and I think it makes the programs better. Other students agreed they liked that I’d done that little extra bit in my programs this week.

Here are my programs from week 7.

School Status

<script type="text/javascript">
/* Defining Table
 * Input: Student name and number of school credits completed
 * Processing: Determine grade in school based on number of credits completed
 * Output: Student name and school grade
function studentStatus() {
	// Receive from user student's full name from id="name" and store as var name.
		var name = document.getElementById("name").value;
	// Receive from user student's total credits completed from id="credits", convert
	// to an real number, and store as var credits.
		var credits = parseFloat(document.getElementById("credits").value);
	// Determine grade in school based on credits and store as var grade.
		var grade;
		if (credits >= 0 && credits < 30) {
			grade = "Freshman";
			document.getElementById("outputDiv").innerHTML = name + "'s grade is " + grade + ".";
		} else if (credits < 60) {
			grade = "Sophomore";
			document.getElementById("outputDiv").innerHTML = name + "'s grade is " + grade + ".";
		} else if (credits < 90) {
			grade ="Junior";
			document.getElementById("outputDiv").innerHTML = name + "'s grade is " + grade + ".";
		} else if (credits >= 90) {
			grade = "Senior";
			document.getElementById("outputDiv").innerHTML = name + "'s grade is " + grade + ".";
		} else {
			document.getElementById("outputDiv").innerHTML = "Please enter a valid number of credits.";
	// Build output and display it to user.
	// document.getElementById("outputDiv").innerHTML = name + "'s grade is " + grade + ".";

Discount & Price Calculator

<script type="text/javascript">
/* Defining Table
 * Input: subtotal from user and weekday from Date() object
 * Processing: determine if discount is available from subtotal and day
 *             and apply discount if applicable
 * Output: final total including sales tax
function discountCheck() {
	// Receive subtotal from user, convert to a real number, 
	// and store as var subtotal.
	var subtotal = parseFloat(document.getElementById("subtotal").value);
	// Determine if subtotal is greater than 50 and if today is
	// Tuesday or Wednesday. If so, add a 10% discount to subtotal.
	// Add tax to subtotal and save as var total.
	var dayOfWeek = new Date().getDay();
		var total;
		if (subtotal > 50 && (dayOfWeek == 2 || dayOfWeek == 3)) {
			subtotal *= .9;
			total = subtotal * 1.06;
			document.getElementById("subtotalDiv").innerHTML = 
			"You qualify for a 10% discount! Your new subtotal is: $"+ subtotal.toFixed(2);
		} else {
			total = subtotal * 1.06;
			document.getElementById("subtotalDiv").innerHTML = "";
	// Output total to user.
	document.getElementById("outputDiv").innerHTML = 
	"Your total amount including tax is: $" + total.toFixed(2);