Lab 4: Exploring JavaScript

Overview

In this lab, you will learn the basics of 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 this 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 input 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

Greeting

Please write a function that returns the string "All hail professor Ryan

ParametersExpected Return ValueActual Return Value
()"All hail professor Ryan"
Calculate Sum

Please write a function that accepts two numbers and returns the sum of them.

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

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: Checking Palindromity

A string is a palindrome if it is the same when you read it forward or backward. For instance, if you read "racecar" from right to left, it is still "racecar". Please write a function that takes a string parameter and returns true if the string is a palindrome

ParametersExpected Return ValueActual Return Value
("abc")false
("aba")true
("a")true
("racecar")true
("tacocat")true

Section 3: Create a Calculator

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 4: Sum of Primes

For this problem, I will pass an array to your function. Please return the sum of all the prime numbers in the array. Remember that a prime number is a number that is not divisible by any number other than itself and 1. You probably want to break this function into two parts. For the first part, can you write a function that takes an array of numbers and returns the sum? Can you write a function that checks if a number is prime? How can you combine these two functions together to come up with the desired result?

ParametersExpected Return ValueActual Return Value
([ 2, 3, 4, 6, 11 ])16
([ 1, 1, 1, 1, 1, 1, 13 ])13
([ 100, 102, 104, 106 ])0
([ 2, 5, 7, 9, 11 ])25

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 2 would get a single file called isPalindrome.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.