Prog W Javascript Coursera Ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MODULE 3: Programming Assignment: Array and object

iteration
// Task 1

// Task 2

// Task 3

// Step 1: logDairy function

var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake'];

function logDairy() {

for (let item of dairy) {

console.log(item);

// Call the logDairy function

logDairy();

// Step 2: birdCan function

const animal = {

canJump: true

};

const bird = Object.create(animal);

bird.canFly = true;

bird.hasFeathers = true;

function birdCan() {
for (let key in bird) {

if (bird.hasOwnProperty(key)) {

console.log(`${key}: ${bird[key]}`);

// Call the birdCan function

birdCan();

// Step 3: animalCan function

function animalCan() {

for (let key in bird) {

console.log(`${key}: ${bird[key]}`);

// Call the animalCan function

animalCan();

MODULE 4: Programming Assignment: Writing a Unit


Test
timesTwo.js

function timesTwo(number) {

return number * 2;

module.exports = timesTwo;

timesTwo.test.js
const timesTwo = require('./timesTwo');

test("returns the number times 2", () => {

expect(timesTwo(10)).toBe(20);

});

Package.json

"name": "jest-testing",

"version": "1.0.0",

"description": "",

"main": "timesTwo.js",

"scripts": {

"test": "jest"

},

"devDependencies": {

"jest": "^28.0.0"

MODULE 5: Programming Assignment: Little Lemon Receipt Maker


finalProject.js

// Updated dish data to match the expected output from the test cases

const dishData = [

{ name: "Italian pasta", price: 9.55 },

{ name: "Rice with veggies", price: 8.65 },

{ name: "Chicken with potatoes", price: 15.55 },

{ name: "Vegetarian Pizza", price: 6.45 }

];

// Function to calculate prices with or without tax


function getPrices(taxBoolean) {

const tax = 1.2; // Tax rate of 20%

for (let dish of dishData) {

let finalPrice;

if (taxBoolean === true) {

finalPrice = dish.price * tax;

} else if (taxBoolean === false) {

finalPrice = dish.price;

} else {

console.log("You need to pass a boolean to the getPrices call!");

return;

console.log("Dish: " + dish.name + " Price: $" + finalPrice.toFixed(2));

// Function to apply discounts based on guest count and print the final output

function getDiscount(taxBoolean, guests) {

getPrices(taxBoolean);

if (typeof guests === 'number' && guests > 0 && guests < 30) {

let discount = 0;

if (guests < 5) {

discount = 5;

} else if (guests >= 5) {

discount = 10;

}
console.log("Discount is: $" + discount);

} else {

console.log("The second argument must be a number between 0 and 30");

// Example test calls to verify each case

getDiscount(true, 2); // Tax applied, discount 5

getDiscount(true, 10); // Tax applied, discount 10

getDiscount(false, 3); // No tax, discount 5

getDiscount(false, 15); // No tax, discount 10

getDiscount(true, 0); // Invalid guest number, should print error

getDiscount(false, 'five'); // Invalid guest input type, should print error

You might also like