javascript

exercises

exercises.js
/**
 * ============================================
 * 3.6 TERNARY OPERATOR - EXERCISES
 * ============================================
 */

// ============================================
// EXERCISE 1: Basic Ternary
// ============================================
/**
 * Task: Use a ternary operator to determine if a number is positive or negative.
 * Return "positive" for numbers > 0, "negative" otherwise.
 */

const number = 5;
// Your code here:
// const result = ...

// console.log("Exercise 1:", result);  // "positive"

// ============================================
// EXERCISE 2: Convert if-else to Ternary
// ============================================
/**
 * Task: Convert this if-else statement to a ternary operator
 *
 * let ticketPrice;
 * if (age < 12) {
 *     ticketPrice = 5;
 * } else {
 *     ticketPrice = 10;
 * }
 */

const age2 = 8;
// Your code here:
// const ticketPrice = ...

// console.log("Exercise 2: Ticket price:", ticketPrice);  // 5

// ============================================
// EXERCISE 3: String Pluralization
// ============================================
/**
 * Task: Create a message that says "X item" or "X items" depending on count
 * Use ternary to add 's' only when count !== 1
 */

const count = 3;
// Your code here:
// const itemMessage = ...

// console.log("Exercise 3:", itemMessage);  // "3 items"

// ============================================
// EXERCISE 4: Default Username
// ============================================
/**
 * Task: Use ternary to display the username if it exists,
 * otherwise display "Guest"
 */

const username = '';
// Your code here:
// const displayName = ...

// console.log("Exercise 4:", displayName);  // "Guest"

// ============================================
// EXERCISE 5: Nested Ternary for Grades
// ============================================
/**
 * Task: Convert this to a nested ternary operator:
 *
 * A: 90-100
 * B: 80-89
 * C: 70-79
 * D: 60-69
 * F: below 60
 */

const score = 72;
// Your code here:
// const grade = ...

// console.log("Exercise 5: Score", score, "= Grade", grade);  // "C"

// ============================================
// EXERCISE 6: Time of Day Greeting
// ============================================
/**
 * Task: Create a greeting based on the hour:
 * - Before 12: "Good morning"
 * - 12 to 17: "Good afternoon"
 * - 18 to 21: "Good evening"
 * - After 21: "Good night"
 */

const hour = 15;
// Your code here:
// const greeting = ...

// console.log("Exercise 6:", greeting);  // "Good afternoon"

// ============================================
// EXERCISE 7: Ternary in Template Literal
// ============================================
/**
 * Task: Create a status message that shows:
 * "User [name] is [online/offline]"
 */

const user = { name: 'Alice', isOnline: true };
// Your code here:
// const statusMessage = ...

// console.log("Exercise 7:", statusMessage);  // "User Alice is online"

// ============================================
// EXERCISE 8: Object with Conditional Properties
// ============================================
/**
 * Task: Create a config object with:
 * - mode: "production" or "development" based on isProd
 * - debug: false if isProd, true otherwise
 */

const isProd = false;
// Your code here:
// const config = ...

// console.log("Exercise 8:", config);

// ============================================
// EXERCISE 9: Conditional Array Element
// ============================================
/**
 * Task: Create a navigation array that includes "Admin" only if isAdmin is true
 * Base items: ["Home", "Profile", "Settings"]
 */

const isAdmin = true;
// Your code here:
// const navItems = ...

// console.log("Exercise 9:", navItems);  // ["Home", "Profile", "Admin", "Settings"]

// ============================================
// EXERCISE 10: Fee Calculator
// ============================================
/**
 * Task: Calculate shipping fee based on:
 * - Free shipping if total >= 50
 * - $5.99 otherwise
 * Return the total with shipping
 */

const cartTotal = 35;
// Your code here:
// const shippingFee = ...
// const finalTotal = ...

// console.log("Exercise 10: Cart:", cartTotal, "Shipping:", shippingFee, "Total:", finalTotal);

// ============================================
// EXERCISE 11: Button Class Builder
// ============================================
/**
 * Task: Build a CSS class string for a button:
 * - Always include "btn"
 * - Add "btn-primary" if isPrimary, otherwise "btn-secondary"
 * - Add "btn-large" if isLarge, otherwise nothing extra
 */

const isPrimary = true;
const isLarge = false;
// Your code here:
// const buttonClass = ...

// console.log("Exercise 11:", buttonClass);  // "btn btn-primary"

// ============================================
// EXERCISE 12: Access Level Checker
// ============================================
/**
 * Task: Determine access level based on role:
 * - "admin": full access
 * - "editor": write access
 * - "viewer": read access
 * - anything else: no access
 */

const role = 'editor';
// Your code here:
// const accessLevel = ...

// console.log("Exercise 12:", accessLevel);  // "write access"

// ============================================
// BONUS: Complex Condition
// ============================================
/**
 * Task: Determine discount based on:
 * - Premium members get 20%
 * - Regular members with orders > $100 get 10%
 * - Regular members with orders <= $100 get 5%
 * - Non-members get 0%
 */

const isMember = true;
const isPremium = false;
const orderTotal = 150;
// Your code here:
// const discount = ...

// console.log("Bonus: Discount:", discount + "%");  // "10%"

// ============================================
// SOLUTIONS
// ============================================
/**
 * Exercise 1:
 * const result = number > 0 ? "positive" : "negative";
 *
 * Exercise 2:
 * const ticketPrice = age2 < 12 ? 5 : 10;
 *
 * Exercise 3:
 * const itemMessage = `${count} item${count !== 1 ? "s" : ""}`;
 *
 * Exercise 4:
 * const displayName = username ? username : "Guest";
 *
 * Exercise 5:
 * const grade = score >= 90 ? "A"
 *             : score >= 80 ? "B"
 *             : score >= 70 ? "C"
 *             : score >= 60 ? "D"
 *             : "F";
 *
 * Exercise 6:
 * const greeting = hour < 12 ? "Good morning"
 *                : hour < 18 ? "Good afternoon"
 *                : hour < 22 ? "Good evening"
 *                : "Good night";
 *
 * Exercise 7:
 * const statusMessage = `User ${user.name} is ${user.isOnline ? "online" : "offline"}`;
 *
 * Exercise 8:
 * const config = {
 *     mode: isProd ? "production" : "development",
 *     debug: isProd ? false : true  // or: debug: !isProd
 * };
 *
 * Exercise 9:
 * const navItems = ["Home", "Profile", isAdmin ? "Admin" : null, "Settings"].filter(Boolean);
 * // Or: const navItems = ["Home", "Profile", ...(isAdmin ? ["Admin"] : []), "Settings"];
 *
 * Exercise 10:
 * const shippingFee = cartTotal >= 50 ? 0 : 5.99;
 * const finalTotal = cartTotal + shippingFee;
 *
 * Exercise 11:
 * const buttonClass = `btn ${isPrimary ? "btn-primary" : "btn-secondary"}${isLarge ? " btn-large" : ""}`;
 *
 * Exercise 12:
 * const accessLevel = role === "admin" ? "full access"
 *                   : role === "editor" ? "write access"
 *                   : role === "viewer" ? "read access"
 *                   : "no access";
 *
 * Bonus:
 * const discount = !isMember ? 0
 *                : isPremium ? 20
 *                : orderTotal > 100 ? 10
 *                : 5;
 */
Exercises - JavaScript Tutorial | DeepML