Skip to content

Instantly share code, notes, and snippets.

// 1. Interface for Magical Item
interface IMagicalItem {
name: string
type: string
powerLevel: number
isRare: boolean
}
// 2. Class implementing IMagicalItem
class MagicalItem implements IMagicalItem {
const o = { message: 'hi', count: 5 }
// type Output = {
// message: string
// count: number
// }
type Output = typeof o
const results = []
// Type definition for wand using tuple
type Wand = [core: string, length: number, material: string]
// Enum for house names
enum House {
Gryffindor = 'Gryffindor',
Slytherin = 'Slytherin'
}
// Type for character
var person = {
firstName: 'Zelda',
lastName: 'Fitzgerald',
introduce: function () {
console.log('my name is ' + this.firstName + ' ' + this.lastName)
}
}
// Question 1: Given a greet() function modify it to update its context with an object
// having firstName and lastName properties.
// Question 1: Declare an object named "person" with properties "name", "age", and "city" and set their respective values to "John", 30, and "New York".
var person = {
name: 'John',
age: 30,
city: 'New York'
}
console.log('person', person)
// Question 2: Declare an object named "book" with properties "title", "author", and "year" and set their respective values to "The Great Gatsby", "F. Scott Fitzgerald", and 1925. Access the "author" property and store its value in a variable called "authorName".
var book = {
// Question 1: Anonymous Function
// Write an anonymous function that takes two numbers as parameters and returns their sum.
var add = function (a, b) {
return a + b
}
// Question 2: IIFE Function
// Write an IIFE (Immediately Invoked Function Expression) that prints "Hello, World!" to the console.
;(function () {
console.log('hello world')
// Primitive
var message = 'hello'
var count = 42
var correct = true
// Container/Collection
// Entity (One thing, multiple attributes)
var person = {
name: 'Zelda Fitzgerald',
// 1. Write a function `greet` that takes a name as a parameter and returns a greeting message.
/*
function greet (name) {
const message = 'Hello ' + name
return message
}
var message = greet('Tallulah')
console.log(message)
// 2. Write a function `calculateArea` that takes the length and width of a rectangle as parameters and returns its area.
// 1. Write a JavaScript program that prints the numbers from 1 to 10 using a for loop.
/*
for (var i = 1; i <= 10; i++) {
console.log(i)
}
// 2. Write a JavaScript program that calculates the sum of all numbers from 1 to a given number using a while loop.
var number = 10
var sum = 0
var i = 1
// 1. Write a program that checks if a given number, `num`, is positive, negative, or zero.
/*
var num = 0
var positive = num > 0
var negative = num < 0
if (positive) {
console.log('positive')
} else if (negative) {
console.log('negative')
} else {