console.log("Hello, World!");
node hello.js
// Declare dimensions of the rectangle using let
let length = 5; // length of the rectangle
let width = 3; // width of the rectangle
// Calculate area using const
const area = length * width;
// Print the area to the console
console.log("The area of the rectangle is:", area);
node rectangleArea.js
function calculateSum(a, b) {
return a + b;
}
// Example of function scoping
let number1 = 8;
let number2 = 12;
const sum = calculateSum(number1, number2);
console.log("The sum of", number1, "and", number2, "is:", sum);
node sumFunction.js
// Define a class to represent a Book
class Book {
constructor(title, author, year) {
this.title = title;
this.author = author;
this.year = year;
}
// Method to display book details
displayInfo() {
console.log(`Title: ${this.title}, Author: ${this.author}, Year: ${this.year}`);
}
}
// Create an array to store the book objects
let bookstore = [];
// Function to add books to the bookstore
function addBook(book) {
bookstore.push(book);
}
// Function to display all books in the bookstore
function displayBooks() {
bookstore.forEach(book => book.displayInfo());
}
// Create book objects
const book1 = new Book("To Kill a Mockingbird", "Harper Lee", 1960);
const book2 = new Book("1984", "George Orwell", 1949);
const book3 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
// Add books to the bookstore
addBook(book1);
addBook(book2);
addBook(book3);
addBook(book1);
addBook(book2);
addBook(book3);
// Display all books
console.log("Displaying all books in the bookstore:");
displayBooks();
node bookstore.js
// Define functions for basic arithmetic operations
function add(x, y) {
return x + y;
}
function subtract(x, y) {
return x - y;
}
function multiply(x, y) {
return x * y;
}
function divide(x, y) {
if (y === 0) {
console.log("Error: Division by zero is not allowed.");
return undefined;
}
return x / y;
}
// Export the functions so they can be used in other files
module.exports = {
add,
subtract,
multiply,
divide
};
// Import the calculator module
const calculator = require('./calculator');
// Examples of using the imported functions
const sum = calculator.add(5, 3);
const difference = calculator.subtract(10, 4);
const product = calculator.multiply(7, 6);
const quotient = calculator.divide(20, 5);
// Display the results
console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
console.log(`Product: ${product}`);
console.log(`Quotient: ${quotient}`);
node index.js
javascript
Copy code
// Define some variables to use within template literals
const user = {
name: "James T. Kirk",
role: "Captain",
ship: "USS Enterprise"
};
const missionCount = 112;
// Create a complex string using template literals
const introduction = `Hello, my name is ${user.name}, and I am the ${user.role} of the ${user.ship}.
I have commanded over ${missionCount} missions across various galaxies.
Today's date is ${new Date().toLocaleDateString()}.`;
// Use template literals to perform calculations directly within the string
const operationalYears = 5;
const report = `As of ${new Date().getFullYear()}, the ${user.ship} has been operational for ${operationalYears} years.
In this time, we've completed ${Math.floor(missionCount / operationalYears)} missions per year on average.`;
// Display the results
console.log(introduction);
console.log(report);
bash
Copy code
node templateLiterals.js
javascript
Copy code
// Initial inventory of flowers in the shop
const roses = ['red rose', 'white rose', 'pink rose'];
const tulips = ['yellow tulip', 'purple tulip', 'orange tulip'];
// Using the spread operator to combine arrays of flowers
const mixedFlowers = [...roses, ...tulips];
console.log('Mixed Flowers Inventory:', mixedFlowers);
// Creating a custom bouquet using selected flowers
const customBouquet = ['blue orchid', ...roses.slice(0, 2), tulips[1]];
console.log('Custom Bouquet:', customBouquet);
// Updating inventory with new shipments
const newFlowers = ['sunflower', 'daisy'];
const updatedInventory = [...mixedFlowers, ...newFlowers];
console.log('Updated Flower Inventory:', updatedInventory);
// Using spread operator to clone and update an object representing a special offer
const specialOffer = { name: 'Spring Bouquet', price: 15 };
const newSpecialOffer = { ...specialOffer, price: 12 }; // Discounted price
console.log('New Special Offer:', newSpecialOffer);
bash
Copy code
node flowerShop.js