11. Implement a function that converts a given JSON object to a query string.
Required Input:
{
name: "John Doe",
age: 30,
city: "New York" }
Expected Output:
name=John%20Doe&age=30&city=New%20York
Code In Javascript
function toQueryString(obj) {
// Map object keys and values to key=value pairs
// Use encodeURIComponent to handle special characters
}
const params = { name: "John Doe", age: 30, city: "New York" };
console.log(toQueryString(params));
Run Code?
Click Run Button to view compiled output
12. Write a function to evaluate a mathematical expression string (e.g., "3 + 2 * (4 - 1)").
Required Input:
"3 + 2 * (4 - 1)"
Expected Output:
9
Code In Javascript
// Evaluate a mathematical expression from a string
function evaluateExpression(expr) {
// Use Function constructor to evaluate the string as code
}
console.log(evaluateExpression("3 + 2 * (4 - 1)"));
Run Code?
Click Run Button to view compiled output
13. Implement a function to flatten a deeply nested object, converting nested keys to a single key path (e.g., { a: { b: 1 } } to { "a.b": 1 }).
Required Input:
{ a: { b: 1, c: { d: 2 } } }
Expected Output:
{ 'a.b': 1, 'a.c.d': 2 }
Code In Javascript
// Flatten a deeply nested object
function flattenObject(obj, prefix = '', result = {}) {
// Iterate through object keys
// Concatenate nested keys to form path
// Recursively flatten nested objects
}
const nestedObj = { a: { b: 1, c: { d: 2 } } };
console.log(flattenObject(nestedObj));
Run Code?
Click Run Button to view compiled output
14. Create a function that uses throttling to limit a function’s execution rate.
Required Input:
Function to throttle: logMessage
Throttle interval: 1000 ms
Number of calls to logMessage: 3
Expected Output:
Only one output every 1000ms.
Code In Javascript
// Implement throttling to limit function execution rate
function throttle(func, limit) {
// Track the last execution time and set a timeout for additional calls
}
// Example function to throttle
function logMessage() {
// Log a message to the console
}
const throttledLog = throttle(logMessage, 1000);
throttledLog();
throttledLog();
throttledLog();
Run Code?
Click Run Button to view compiled output
15. Write a function that converts a snake_case string to camelCase.
Required Input:
"hello_world"
Expected Output:
helloWorld
Code In Javascript
// Convert snake_case to camelCase
function snakeToCamel(str) {
// Replace underscores and capitalize letters after each underscore
}
console.log(snakeToCamel("hello_world"));
Run Code?
Click Run Button to view compiled output
16. Implement a function to find the longest common subsequence between two strings.
Required Input:
"abcdefg", "aceg"
Expected Output:
aceg
Code In Javascript
// Find the longest common subsequence between two strings
function longestCommonSubsequence(str1, str2) {
// Initialize a 2D array for dynamic programming
// Loop through both strings, comparing characters and updating the 2D array
// Return the constructed longest common subsequence
}
console.log(longestCommonSubsequence("abcdefg", "aceg"));
Run Code?
Click Run Button to view compiled output
17. Write a function that compresses a string using the Run-Length Encoding (RLE) algorithm.
Required Input:
"aaabccccdd"
Expected Output:
a3b1c4d2
Code In Javascript
// Compress a string using Run-Length Encoding
function runLengthEncode(str) {
// Initialize an encoded string and counter
// Iterate through characters, updating the counter for repeated characters
// Append the count and character when a different character is encountered
}
console.log(runLengthEncode("aaabccccdd"));
Run Code?
Click Run Button to view compiled output
18. Create a function to implement a basic pub/sub (publish-subscribe) pattern.
Required Input:
Topics and callback functions to subscribe and publish
Expected Output:
Topic 1 Published!
Code In Javascript
// Implement pub/sub pattern
function PubSub() {
// Initialize an object to store topics and subscribers
// Create methods for subscribe, unsubscribe, and publish
}
const pubsub = new PubSub();
pubsub.subscribe("topic1", () => console.log("Topic 1 Published!"));
pubsub.publish("topic1");
Run Code?
Click Run Button to view compiled output
19. Implement a function to parse a CSV string into an array of objects.
Required Input:
"Name,Age,City\nJohn,25,New York\nAlice,30,Los Angeles"
Expected Output:
[ { Name: 'John', Age: '25', City: 'New York' },
{ Name: 'Alice', Age: '30', City: 'Los Angeles' } ]
Code In Javascript
// Parse a CSV string into an array of objects
function parseCSV(csv) {
// Split CSV by newline to get rows
// Use the first row as keys for the objects
// Map remaining rows to objects with keys and values
}
console.log(parseCSV("Name,Age,City\nJohn,25,New York\nAlice,30,Los Angeles"));
Run Code?
Click Run Button to view compiled output
20. Write a function that finds the minimum number of coins needed to make a given amount with an array of available denominations.
Required Input:
amount = 11, coins = [ 1, 5, 6 ]
Expected Output:
2
Code In Javascript
// Find the minimum number of coins needed for a given amount
function minCoins(amount, coins) {
// Initialize an array to store the minimum coins for each amount up to the given amount
// Update the array using dynamic programming approach
}
console.log(minCoins(11, [ 1, 5, 6 ]));
Run Code?
Click Run Button to view compiled output