Lab 4: Exploring JavaScript

Overview

In this lab, you will practice some basic JavaScript. The first set of problems is meant to help you learn JavaScript syntax. Later problems are meant to help you learn to solve programming puzzles. Feel free to work together on the coding exercises in the first section of the lab. For later sections, please do all work independently.

Using This Page

When working on this lab, you will be able to write code in the embedded code editors on the page. I have also provided a set of sample inputs and outputs for each function. When you hit the run button, it will execute your function with the given inputs and compare the return value with the expected output. You can run your code as often as you want. Your grade is not dependent on running the code on this site. Also, remember that just because your inputs and outputs match for all the examples I have given, it does not mean your code is perfect. You should still test your code with some other values to make sure it works properly. Please do not change the name of the functions in the sample code. I rely on the function names being correct to call your function(s) and run tests.

Section 1: JavaScript Syntax

greetClass()

Please write a function that returns the string "Welcome to CIS 1052" (Don't overthink this one)

ParametersExpected Return ValueActual Return Value
()"Welcome to CIS 1052"
addNumbers()

Please write a function that takes two numbers as parameters and returns the sum of them.

ParametersExpected Return ValueActual Return Value
(1, 2)3
(1, -1)0
(100, -1)99
calcGrade()

Please write a function that takes a number between 0 and 100 and returns the class grade that would be associated with each score (90 - 100 = A, 80 - 89 = B, 70 - 79 = C, 60 - 69 = D). Please include grades with a + or -. For two examples, a 90-92 is an A- while a grade from 87-89, is a B+. All letter grades follow the same scale. Anything below a 60 is an F. Note: this is not necessarily the grade scaling I will use for this class!

ParametersExpected Return ValueActual Return Value
(93)"A"
(88)"B+"
(61)"D-"
(73)"C"
(50)"F"
(100)"A+"

Section 2: Conditions and Operators

calculate()

For this problem, please create a "calculator" function that takes two numbers and a string "operator", and does the math operation corresponding to the numbers and the operator. For instance, calculate(5, "+", 6) should return 11. Please support the following operators: + - x / ^ (addition, subtraction,multiplication, division, exponentiation)

ParametersExpected Return ValueActual Return Value
(1, "+", 4)5
(1, "x", 0)0
(20, "/", 5)4
(3, "^", 3)27

Section 3: Objects and Loops

isHappyDog()

Please write a function that takes in a dog object. The dog object will have 4 properties: numTreatsToday, hoursSinceLastWalk, bellyRubs, and strangersBarkedAt. You can calculate your dog's happiness by adding numTreatsToday with bellyRubs, dividing that sum by hoursSinceLastWalked, and adding strangersBarkedAt to the result. If the dog's happiness is greater than or equal to 5, please return an array with the string "Wag" repeated once for every happiness point. If the happiness is less than 5, please return an array with the string "Bark" repeated once for every point. Happiness points should be rounded to the nearest point

ParametersExpected Return ValueActual Return Value
({ "numTreatsToday": 5, "hoursSinceLastWalk": 2, "bellyRubs": 5, "strangersBarkedAt": 2 })[ "Wag", "Wag", "Wag", "Wag", "Wag", "Wag", "Wag" ]
({ "numTreatsToday": 1, "hoursSinceLastWalk": 4, "bellyRubs": 3, "strangersBarkedAt": 1 })[ "Bark", "Bark" ]

Submitting this lab

To submit this assignment, please create a single zip file and upload it to Canvas under the appropriate assignment. Your zip file should contain a separate .js file for every problem in the lab. Please give the file the same name as the main function you are writing. For instance, Section 3 would get a single file called isHappyDog.js

Grading

This assignment is a bit different from what you have done before in that all answers will either be objectively correct or incorrect for each input. I will run each of your functions for the inputs shown in the corresponding table. I will also test your function with a few inputs that are not shown. I will grade this assignment mostly based on the correctness of your responses. As a secondary factor, I will grade based on the quality of your code and your thought process. JavaScript is more picky about syntax so please make sure your code at least does something, even if it is not perfectly correct.