Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

📘 JavaScript Homework 2

This repository contains solutions for 4 tasks focused on branching and loops in JavaScript. Each task is implemented in a separate file (task-1.js, task-2.js, task-3.js, task-4.js).


🔹 Task 1: Droid Orders (task-1.js)

A function was written to handle a droid ordering system with credit validation.

function makeTransaction(quantity, pricePerDroid, customerCredits) {
  const totalPrice = quantity * pricePerDroid;

  if (totalPrice > customerCredits) {
    return "Insufficient funds!";
  }

  return `You ordered ${quantity} droids worth ${totalPrice} credits!`;
}

✅ Test Results

makeTransaction(5, 3000, 23000) → "You ordered 5 droids worth 15000 credits!"
makeTransaction(3, 1000, 15000) → "You ordered 3 droids worth 3000 credits!"
makeTransaction(10, 5000, 8000) → "Insufficient funds!"
makeTransaction(8, 2000, 10000) → "Insufficient funds!"
makeTransaction(10, 500, 5000) → "You ordered 10 droids worth 5000 credits!"

🔹 Task 2: Message Formatting (task-2.js)

The function trims long messages and appends ... if they exceed a given length.

function formatMessage(message, maxLength) {
  if (message.length <= maxLength) {
    return message;
  }
  return message.slice(0, maxLength) + "...";
}

✅ Test Results

formatMessage("Curabitur ligula sapien", 16) → "Curabitur ligula..."
formatMessage("Curabitur ligula sapien", 23) → "Curabitur ligula sapien"
formatMessage("Vestibulum facilisis purus nec", 20) → "Vestibulum facilisis..."
formatMessage("Vestibulum facilisis purus nec", 30) → "Vestibulum facilisis purus nec"
formatMessage("Nunc sed turpis a felis in nunc fringilla", 15) → "Nunc sed turpis..."
formatMessage("Nunc sed turpis a felis in nunc fringilla", 41) → "Nunc sed turpis a felis in nunc fringilla"

🔹 Task 3: Spam Check (task-3.js)

The function checks if the message contains banned words spam or sale (case-insensitive).

function checkForSpam(message) {
  const lowerMessage = message.toLowerCase();
  return lowerMessage.includes("spam") || lowerMessage.includes("sale");
}

✅ Test Results

checkForSpam("Latest technology news") → false
checkForSpam("JavaScript weekly newsletter") → false
checkForSpam("Get best sale offers now!") → true
checkForSpam("Amazing SalE, only tonight!") → true
checkForSpam("Trust me, this is not a spam message") → true
checkForSpam("Get rid of sPaM emails. Our book in on sale!") → true
checkForSpam("[SPAM] How to earn fast money?") → true

🔹 Task 4: Shipping Cost (task-4.js)

The function determines shipping availability and calculates cost by country using a switch statement.

function getShippingCost(country) {
  let price;

  switch (country) {
    case "China":
      price = 100;
      break;
    case "Chile":
      price = 250;
      break;
    case "Australia":
      price = 170;
      break;
    case "Jamaica":
      price = 120;
      break;
    default:
      return "Sorry, there is no delivery to your country";
  }

  return `Shipping to ${country} will cost ${price} credits`;
}

✅ Test Results

getShippingCost("Australia") → "Shipping to Australia will cost 170 credits"
getShippingCost("Germany") → "Sorry, there is no delivery to your country"
getShippingCost("China") → "Shipping to China will cost 100 credits"
getShippingCost("Chile") → "Shipping to Chile will cost 250 credits"
getShippingCost("Jamaica") → "Shipping to Jamaica will cost 120 credits"
getShippingCost("Sweden") → "Sorry, there is no delivery to your country"

📌 Summary & Reflection

Great job! 💪 Now it's time to summarize and reflect on what you learned in Module 2.

Now you know:

  • How branching works: if statements, switch operator, ternary operator
  • Logical operators: &&, ||, !
  • String methods: slice(), toLowerCase(), toUpperCase(), includes(), startsWith(), endsWith(), indexOf(), trim()
  • Loops: while, do…while, for

It’s time to reinforce this knowledge with practice and also review what you learned in the previous module.


📌 Overview

  • Task 1: Droid order system with credit check
  • Task 2: Message formatting with length limit
  • Task 3: Spam detection in text
  • Task 4: Shipping cost calculation by country

About

JavaScript HW-02 — Branching and Loops: Includes conditional logic with if-else, switch, ternary operator, and logical operators (&&, ||, !). Covers loops (while, do…while, for) and string methods (slice, toLowerCase, toUpperCase, includes, startsWith, endsWith, indexOf, trim).

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages