Here are the sample code snippets embedded in an HTML file with proper formatting:
Sample Code Snippets
Fetch API Example
// Sample code to fetch data from an API using Fetch API in JavaScript
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log('Data received:', data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
Simple Function to Calculate Factorial
# Sample code to calculate the factorial of a number using a recursive function in Python
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
number = 5
print(f"Factorial of {number} is {factorial(number)}")
Basic Form
<!-- Sample code for a basic HTML form -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Form</title>
</head>
<body>
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Basic Hello World Program
// Sample code for a basic Hello World program in Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Select Query
-- Sample code for a basic SELECT query in SQL
SELECT first_name, last_name, email
FROM users
WHERE active = 1
ORDER BY last_name;
Simple Class Definition
// Sample code for defining a simple class in C#
using System;
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void Introduce()
{
Console.WriteLine($"Hello, my name is {FirstName} {LastName}.");
}
}
// Example usage
public class Program
{
public static void Main()
{
Person person = new Person();
person.FirstName = "John";
person.LastName = "Doe";
person.Introduce();
}
}
Comments
0 comments
Please sign in to leave a comment.