Unlisted
Like
spring_attempt_25-4-10
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v123View latest version
title: | Code Examples for Testing | ||||||
---|---|---|---|---|---|---|---|
author: | Admin | ||||||
published: | 2024-04-10 | ||||||
tags: |
|
This post demonstrates various code block examples with syntax highlighting. It's useful for testing how different programming languages are displayed in the blog.
// A simple JavaScript function to calculate factorial
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
// Example usage
const num = 5;
console.log(`The factorial of ${num} is ${factorial(num)}`);
// ES6 arrow function example
const square = (x) => x * x;
console.log(`The square of 4 is ${square(4)}`);
# A Python class example class Calculator: def __init__(self, name): self.name = name self.result = 0 def add(self, x, y): self.result = x + y return self.result def subtract(self, x, y): self.result = x - y return self.result def multiply(self, x, y): self.result = x * y return self.result def divide(self, x, y): if y == 0: raise ValueError("Cannot divide by zero!") self.result = x / y return self.result # Create an instance and use it calc = Calculator("MyCalculator") print(f"Addition: {calc.add(10, 5)}") print(f"Subtraction: {calc.subtract(10, 5)}") print(f"Multiplication: {calc.multiply(10, 5)}") print(f"Division: {calc.divide(10, 5)}")
#!/bin/bash # A simple bash script to backup files # Define variables SOURCE_DIR="/path/to/source" BACKUP_DIR="/path/to/backup" DATESTAMP=$(date +"%Y-%m-%d") ARCHIVE_NAME="backup_${DATESTAMP}.tar.gz" # Create backup directory if it doesn't exist if [ ! -d "$BACKUP_DIR" ]; then mkdir -p "$BACKUP_DIR" echo "Created backup directory: $BACKUP_DIR" fi # Create the archive tar -czf "${BACKUP_DIR}/${ARCHIVE_NAME}" "$SOURCE_DIR" # Check if backup was successful if [ $? -eq 0 ]; then echo "Backup completed successfully: ${BACKUP_DIR}/${ARCHIVE_NAME}" else echo "Backup failed!" exit 1 fi # List existing backups echo "Existing backups:" ls -lh "$BACKUP_DIR"
- Add JavaScript code example
- Add Python code example
- Add Bash code example
- Add more programming languages
This post demonstrates how code blocks with syntax highlighting appear in the blog. The highlighting is handled by the highlight.js library that's included in the project.