1)Given an number N and an array of size N-1 containing N-1 numbers between 1 to N. Find the number(between 1 to N), that is not present in the given array.
let N = 5;
let Arr = [1, 2, 4, 5]
sum=n*(n+1)/2
for(let i=0;i<arr.length;i++){
sum-=arr[i]
}
console.log(sum)
2)Input: pattern = "abba", s = "dog cat cat dog "
Output: true
function wordPattern(pattern, s) {
const words = s.split(' ');
if (pattern.length !== words.length) {
return false;
}
const charToWord = new Map();
for (let i = 0; i < pattern.length; i++) {
const char = pattern[i];
const word = words[i];
if (charToWord.has(char)) {
if (charToWord.get(char) !== word) {
return false;
}
} else {
charToWord.set(char, word);
}
}
return true;
}
// Example usage:
const pattern1 = "abba";
const s1 = "dog cat cat dog";
console.log(wordPattern(pattern1, s1)); // Output: true
const pattern2 = "abba";
const s2 = "dog cat cat fish";
console.log(wordPattern(pattern2, s2)); // Output: false
3)objects in typescript?
In TypeScript, as in JavaScript, objects are collections of key-value pairs where keys are strings (or symbols) and values can be of any data type, including other objects. TypeScript offers static typing and additional features for defining and working with objects.
There are several ways to define objects in TypeScript:
Literal Syntax: You can define objects using literal syntax, which involves enclosing key-value pairs within curly braces {}.
typescript
Copy code
let person: { name: string, age: number } = {
name: "John",
age: 30
};
Interface: Interfaces in TypeScript are a powerful way to define the shape of an object. They provide a contract for what properties and methods an object should have.
typescript
Copy code
interface Person {
name: string;
age: number;
}
let person: Person = {
name: "John",
age: 30
};
Type Alias: You can also use type aliases to define an object type.
typescript
Copy code
type Person = {
name: string;
age: number;
};
let person: Person = {
name: "John",
age: 30
};
Class: You can define objects using classes, which provide a blueprint for creating objects with specific properties and methods.
typescript
Copy code
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
let person = new Person("John", 30);
In all these examples, person is an object with properties name and age. TypeScript provides type checking and IntelliSense support, which helps catch errors at compile-time and provides better code completion and documentation when working with objects.
4)Enum in typescript?
In TypeScript, enum is a way to define a set of named constants. It allows you to define a collection of related values that can be numeric or string-based. Enums are useful when you have a fixed set of values that are known at compile time.
Here's how you can define and use enums in TypeScript:
Numeric Enums:
Numeric enums are auto-incremented starting from 0, but you can also set their values explicitly.
typescript
Copy code
enum Direction {
Up,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
console.log(move); // Output: 0
console.log(Direction[move]); // Output: "Up"
In this example, Direction is a numeric enum where Up has the value 0, Down has 1, Left has 2, and Right has 3.
String Enums:
String enums have string values associated with each enum member.
typescript
Copy code
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
let move: Direction = Direction.Up;
console.log(move); // Output: "UP"
console.log(Direction[move]); // Output: "UP"
In this example, Direction is a string enum where each enum member has an associated string value.
Heterogeneous Enums:
You can mix string and numeric members in the same enum, but it's not common.
typescript
Copy code
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES"
}
Reverse Mapping:
Enums in TypeScript support reverse mapping, which means you can access the name of the enum member by its value.
typescript
Copy code
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction.Up); // Output: 0
console.log(Direction[0]); // Output: "Up"
Enums in TypeScript provide a convenient way to work with sets of constants, making your code more readable and maintainable. They can be particularly useful when you have a finite set of options or states in your application.
Comments
Post a Comment