xxxxxxxxxx
// The eval() function in JavaScript allows you to evaluate a string as code.
// However, it is important to use it with caution due to security risks.
// Basic usage of eval():
const codeString = "console.log('Hello, World!');";
eval(codeString); // This will output "Hello, World!" to the console.
// You can also use eval() to evaluate expressions:
const expression = "2 + 3 * 4";
const result = eval(expression);
console.log(result); // This will output 14 to the console.
// However, be aware that using eval() with user-generated input can be dangerous.
// It can execute arbitrary code and lead to security vulnerabilities.
// It is generally recommended to avoid using eval() whenever possible and find alternative approaches to achieve the desired functionality.
xxxxxxxxxx
eval() -> is able to evalute the things inside it , like eval(a+b+c) , wher a=3,b=4,c=4 , so eval(a+b+c) = 11
xxxxxxxxxx
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2'); // returns 4
xxxxxxxxxx
// eval is a built-in JavaScript function that evaluates or executes code dynamically.
// However, it should be used with caution as it can be a potential security risk.
// Basic usage of eval:
const expression = "2 + 3";
const result = eval(expression);
console.log(result); // Output: 5
// Evaluating code with variables:
const x = 5;
const y = 10;
const equation = "x + y";
const solution = eval(equation);
console.log(solution); // Output: 15
// It is important to validate and sanitize any input used with eval to prevent code injections and vulnerabilities.
xxxxxxxxxx
class Graph {
private val adjList: HashMap<Int, MutableList<Int>> = HashMap()
// Adds a vertex to the graph
fun addVertex(vertex: Int) {
if (!adjList.containsKey(vertex)) adjList[vertex] = mutableListOf()
}
// Adds an edge between two vertices
fun addEdge(v1: Int, v2: Int) {
adjList[v1]?.add(v2)
adjList[v2]?.add(v1) // Since it's an undirected graph
}
// Removes a vertex and its associated edges
fun removeVertex(vertex: Int) {
while (adjList[vertex]?.isNotEmpty() == true) {
val adjacentVertex = adjList[vertex]?.first()
removeEdge(vertex, adjacentVertex!!)
}
adjList.remove(vertex)
}
// Removes an edge between two vertices
fun removeEdge(v1: Int, v2: Int) {
adjList[v1]?.remove(v2)
adjList[v2]?.remove(v1)
}
// Displays the graph
fun display() {
for ((vertex, edges) in adjList) {
println("$vertex -> $edges")
}
}
}
// client code to test Graph Implementation in Kotlin
fun main() {
val graph = Graph()
graph.addVertex(1)
graph.addVertex(2)
graph.addVertex(3)
graph.addVertex(4)
graph.addEdge(1, 2)
graph.addEdge(1, 3)
graph.addEdge(2, 4)
graph.addEdge(3, 4)
graph.display()
graph.removeEdge(2, 4)
graph.removeVertex(3)
graph.display()
}