prompt stringlengths 45 9.08k | chosen listlengths 2 2 | rejected listlengths 2 2 | score_chosen int64 6 9 | score_rejected int64 1 4 |
|---|---|---|---|---|
Create a JavaScript function that reverse alphabetically sort an array of numbers while excluding any duplicates, and return the sorted array. The input array will have a length of at most 100 and will only contain positive integers ranging from 1 to 1000. | [
{
"content": "Create a JavaScript function that reverse alphabetically sort an array of numbers while excluding any duplicates, and return the sorted array. The input array will have a length of at most 100 and will only contain positive integers ranging from 1 to 1000.",
"role": "user"
},
{
"conten... | [
{
"content": "Create a JavaScript function that reverse alphabetically sort an array of numbers while excluding any duplicates, and return the sorted array. The input array will have a length of at most 100 and will only contain positive integers ranging from 1 to 1000.",
"role": "user"
},
{
"conten... | 7 | 4 |
Removes all odd elements from an array in JavaScript, while keeping the original order of the remaining elements intact. The resulting array should contain only even numbers.
let arr = [1, 2, 3, 4, 5]; | [
{
"content": "Removes all odd elements from an array in JavaScript, while keeping the original order of the remaining elements intact. The resulting array should contain only even numbers.\nlet arr = [1, 2, 3, 4, 5];",
"role": "user"
},
{
"content": "One possible solution to remove all odd elements ... | [
{
"content": "Removes all odd elements from an array in JavaScript, while keeping the original order of the remaining elements intact. The resulting array should contain only even numbers.\nlet arr = [1, 2, 3, 4, 5];",
"role": "user"
},
{
"content": "One possible solution to remove all odd elements ... | 9 | 1 |
Write a JavaScript Object that contains employee data such as name, age, contact number, and salary. Additionally, include a function that calculates the employee's yearly bonus based on their performance rating. The performance rating should be a value between 1 and 5, with 5 being the highest. | [
{
"content": "Write a JavaScript Object that contains employee data such as name, age, contact number, and salary. Additionally, include a function that calculates the employee's yearly bonus based on their performance rating. The performance rating should be a value between 1 and 5, with 5 being the highest.",... | [
{
"content": "Write a JavaScript Object that contains employee data such as name, age, contact number, and salary. Additionally, include a function that calculates the employee's yearly bonus based on their performance rating. The performance rating should be a value between 1 and 5, with 5 being the highest.",... | 6 | 3 |
Write a JavaScript function that takes an object as a parameter and prints out all its key-value pairs. Additionally, implement a check to ignore any nested objects and only iterate through the top level properties of the object. | [
{
"content": "Write a JavaScript function that takes an object as a parameter and prints out all its key-value pairs. Additionally, implement a check to ignore any nested objects and only iterate through the top level properties of the object.",
"role": "user"
},
{
"content": "Here's a JavaScript fu... | [
{
"content": "Write a JavaScript function that takes an object as a parameter and prints out all its key-value pairs. Additionally, implement a check to ignore any nested objects and only iterate through the top level properties of the object.",
"role": "user"
},
{
"content": "Here's a JavaScript fu... | 8 | 3 |
Create a JavaScript object representing a library, with properties such as name, location, and opening hours. Include methods to add books to the library, remove books from the library, and search for books by title or author. | [
{
"content": "Create a JavaScript object representing a library, with properties such as name, location, and opening hours. Include methods to add books to the library, remove books from the library, and search for books by title or author.",
"role": "user"
},
{
"content": "Here is an example of a J... | [
{
"content": "Create a JavaScript object representing a library, with properties such as name, location, and opening hours. Include methods to add books to the library, remove books from the library, and search for books by title or author.",
"role": "user"
},
{
"content": "Here is an example of a J... | 7 | 3 |
Write a jQuery command to add a classname to an element only if it doesn't already have that classname. | [
{
"content": "Write a jQuery command to add a classname to an element only if it doesn't already have that classname.",
"role": "user"
},
{
"content": "To add a classname to an element only if it doesn't already have that classname using jQuery, you can use the `addClass()` method combined with the ... | [
{
"content": "Write a jQuery command to add a classname to an element only if it doesn't already have that classname.",
"role": "user"
},
{
"content": "To add a classname to an element only if it doesn't already have that classname using jQuery, you can use the `addClass()` method combined with the ... | 8 | 3 |
Write a JavaScript code to list each item of a given array into a new array in reverse order. However, you are not allowed to use the built-in reverse() method.
arr = [1, 2, 3, 4, 5] | [
{
"content": "Write a JavaScript code to list each item of a given array into a new array in reverse order. However, you are not allowed to use the built-in reverse() method.\n\narr = [1, 2, 3, 4, 5]",
"role": "user"
},
{
"content": "Here is the JavaScript code to list each item of a given array int... | [
{
"content": "Write a JavaScript code to list each item of a given array into a new array in reverse order. However, you are not allowed to use the built-in reverse() method.\n\narr = [1, 2, 3, 4, 5]",
"role": "user"
},
{
"content": "Here is the JavaScript code to list each item of a given array int... | 9 | 1 |
Create a JavaScript function to format a given number with commas. The function should also handle decimal numbers and negative numbers. For example, the input 1000 should be formatted as "1,000", the input 1000000.12 should be formatted as "1,000,000.12", and the input -5000 should be formatted as "-5,000". | [
{
"content": "Create a JavaScript function to format a given number with commas. The function should also handle decimal numbers and negative numbers. For example, the input 1000 should be formatted as \"1,000\", the input 1000000.12 should be formatted as \"1,000,000.12\", and the input -5000 should be formatt... | [
{
"content": "Create a JavaScript function to format a given number with commas. The function should also handle decimal numbers and negative numbers. For example, the input 1000 should be formatted as \"1,000\", the input 1000000.12 should be formatted as \"1,000,000.12\", and the input -5000 should be formatt... | 7 | 2 |
Suggest a more efficient way to write the following for loop. Additionally, handle the case where the array is empty and throw an error.
for (i = 0; i <= arr.length; i++) {
console.log(arr[i]);
} | [
{
"content": "Suggest a more efficient way to write the following for loop. Additionally, handle the case where the array is empty and throw an error.\nfor (i = 0; i <= arr.length; i++) { \n console.log(arr[i]); \n}",
"role": "user"
},
{
"content": "You can write a more efficient for loop by using ... | [
{
"content": "Suggest a more efficient way to write the following for loop. Additionally, handle the case where the array is empty and throw an error.\nfor (i = 0; i <= arr.length; i++) { \n console.log(arr[i]); \n}",
"role": "user"
},
{
"content": "You can write a more efficient for loop by using ... | 9 | 2 |
You are asked to create an array of N random numbers between 1 to M, where N is a positive integer and M is a positive integer greater than 10. | [
{
"content": "You are asked to create an array of N random numbers between 1 to M, where N is a positive integer and M is a positive integer greater than 10.",
"role": "user"
},
{
"content": "To create an array of N random numbers between 1 to M, you can use the following steps:\n\n1. Initialize an ... | [
{
"content": "You are asked to create an array of N random numbers between 1 to M, where N is a positive integer and M is a positive integer greater than 10.",
"role": "user"
},
{
"content": "To create an array of N random numbers between 1 to M, you can use the following steps:\n\n1. Initialize an ... | 7 | 3 |
Write a code to extract the odd numbers from a given array in JavaScript. The extracted numbers should be sorted in ascending order. Additionally, you are not allowed to use any built-in array methods or sorting algorithms. The input array will have at most 1000 elements. [1, 2, 3, 4, 5, 6, 7, 8, 9] | [
{
"content": "Write a code to extract the odd numbers from a given array in JavaScript. The extracted numbers should be sorted in ascending order. Additionally, you are not allowed to use any built-in array methods or sorting algorithms. The input array will have at most 1000 elements. [1, 2, 3, 4, 5, 6, 7, 8, ... | [
{
"content": "Write a code to extract the odd numbers from a given array in JavaScript. The extracted numbers should be sorted in ascending order. Additionally, you are not allowed to use any built-in array methods or sorting algorithms. The input array will have at most 1000 elements. [1, 2, 3, 4, 5, 6, 7, 8, ... | 9 | 3 |
Solve the following problem and display the answer using JavaScript: Calculate the factorial of a given number.
Restrictions:
- You are not allowed to use any built-in factorial functions or libraries.
- The input number will be a positive integer.
- The expected time complexity should be O(n). | [
{
"content": "Solve the following problem and display the answer using JavaScript: Calculate the factorial of a given number.\n\nRestrictions:\n- You are not allowed to use any built-in factorial functions or libraries.\n- The input number will be a positive integer.\n- The expected time complexity should be O(... | [
{
"content": "Solve the following problem and display the answer using JavaScript: Calculate the factorial of a given number.\n\nRestrictions:\n- You are not allowed to use any built-in factorial functions or libraries.\n- The input number will be a positive integer.\n- The expected time complexity should be O(... | 7 | 2 |
Create a JavaScript object which will contain data about a person, but instead of simply including basic information such as name and age, the object should also include more specific details such as the person's blood type, genetic traits, and a detailed medical history. | [
{
"content": "Create a JavaScript object which will contain data about a person, but instead of simply including basic information such as name and age, the object should also include more specific details such as the person's blood type, genetic traits, and a detailed medical history.",
"role": "user"
},... | [
{
"content": "Create a JavaScript object which will contain data about a person, but instead of simply including basic information such as name and age, the object should also include more specific details such as the person's blood type, genetic traits, and a detailed medical history.",
"role": "user"
},... | 9 | 4 |
Write a JavaScript program to calculate the sum of each individual index value from two given arrays of equal length, where each value in the array must be greater than or equal to zero. Additionally, the program should return the sum as a string with a preceding '$' symbol.
let array1 = [1, 0, 2, 3, 4];
let array2 =... | [
{
"content": "Write a JavaScript program to calculate the sum of each individual index value from two given arrays of equal length, where each value in the array must be greater than or equal to zero. Additionally, the program should return the sum as a string with a preceding '$' symbol. \n\nlet array1 = [1, 0... | [
{
"content": "Write a JavaScript program to calculate the sum of each individual index value from two given arrays of equal length, where each value in the array must be greater than or equal to zero. Additionally, the program should return the sum as a string with a preceding '$' symbol. \n\nlet array1 = [1, 0... | 7 | 3 |
Write a JavaScript code snippet to query a given database table and return only the records that meet the following conditions:
- The 'users' table must have at least 1000 records.
- The 'users' table must have an additional column named 'age' with values ranging from 18 to 60 (inclusive).
- The returned records must b... | [
{
"content": "Write a JavaScript code snippet to query a given database table and return only the records that meet the following conditions:\n- The 'users' table must have at least 1000 records.\n- The 'users' table must have an additional column named 'age' with values ranging from 18 to 60 (inclusive).\n- Th... | [
{
"content": "Write a JavaScript code snippet to query a given database table and return only the records that meet the following conditions:\n- The 'users' table must have at least 1000 records.\n- The 'users' table must have an additional column named 'age' with values ranging from 18 to 60 (inclusive).\n- Th... | 9 | 1 |
Implement a function called "addClickEvent" that takes two arguments:
- "elementId" (a string representing the ID of an HTML element)
- "callback" (a function that will be executed when the element with the specified ID is clicked)
The "addClickEvent" function should add an 'onclick' event to the HTML element with th... | [
{
"content": "Implement a function called \"addClickEvent\" that takes two arguments: \n- \"elementId\" (a string representing the ID of an HTML element)\n- \"callback\" (a function that will be executed when the element with the specified ID is clicked)\n\nThe \"addClickEvent\" function should add an 'onclick'... | [
{
"content": "Implement a function called \"addClickEvent\" that takes two arguments: \n- \"elementId\" (a string representing the ID of an HTML element)\n- \"callback\" (a function that will be executed when the element with the specified ID is clicked)\n\nThe \"addClickEvent\" function should add an 'onclick'... | 9 | 4 |
Create a JavaScript function which takes a number as an argument and calculates the factorial of that number. Additionally, handle the scenario when the number is negative or not a valid number, and throw appropriate errors or return meaningful messages. | [
{
"content": "Create a JavaScript function which takes a number as an argument and calculates the factorial of that number. Additionally, handle the scenario when the number is negative or not a valid number, and throw appropriate errors or return meaningful messages.",
"role": "user"
},
{
"content"... | [
{
"content": "Create a JavaScript function which takes a number as an argument and calculates the factorial of that number. Additionally, handle the scenario when the number is negative or not a valid number, and throw appropriate errors or return meaningful messages.",
"role": "user"
},
{
"content"... | 8 | 4 |
Design a function in JavaScript to filter out the values of an array that are greater than the average value of the array.
[2, 10, 11, 19, 3, 4]
In this case, you need to calculate the average value of the array and then filter out the values that are greater than the average. This requires an additional step of calcu... | [
{
"content": "Design a function in JavaScript to filter out the values of an array that are greater than the average value of the array.\n[2, 10, 11, 19, 3, 4]\n\nIn this case, you need to calculate the average value of the array and then filter out the values that are greater than the average. This requires an... | [
{
"content": "Design a function in JavaScript to filter out the values of an array that are greater than the average value of the array.\n[2, 10, 11, 19, 3, 4]\n\nIn this case, you need to calculate the average value of the array and then filter out the values that are greater than the average. This requires an... | 6 | 2 |
Write a JavaScript function that generates a random number between 0 and 10, inclusive. The function should not use any built-in random number generator functions or libraries. | [
{
"content": "Write a JavaScript function that generates a random number between 0 and 10, inclusive. The function should not use any built-in random number generator functions or libraries.",
"role": "user"
},
{
"content": "Here is a JavaScript function that generates a random number between 0 and ... | [
{
"content": "Write a JavaScript function that generates a random number between 0 and 10, inclusive. The function should not use any built-in random number generator functions or libraries.",
"role": "user"
},
{
"content": "Here is a JavaScript function that generates a random number between 0 and ... | 7 | 3 |
Produce a JavaScript code snippet for opening a new browser window with the following requirements:
1. The new window should have a specific width and height.
2. The new window should have a specific position on the screen.
3. The new window should have a specific name/title.
4. The new window should be resizable and ... | [
{
"content": "Produce a JavaScript code snippet for opening a new browser window with the following requirements:\n\n1. The new window should have a specific width and height.\n2. The new window should have a specific position on the screen.\n3. The new window should have a specific name/title.\n4. The new wind... | [
{
"content": "Produce a JavaScript code snippet for opening a new browser window with the following requirements:\n\n1. The new window should have a specific width and height.\n2. The new window should have a specific position on the screen.\n3. The new window should have a specific name/title.\n4. The new wind... | 7 | 2 |
Write the code to set a global variable in JavaScript with the following requirements:
- The global variable must be of type "object".
- The global variable must have at least 5 properties.
- One of the properties must be a nested object.
- One of the properties must be an array.
- The nested object must have at least ... | [
{
"content": "Write the code to set a global variable in JavaScript with the following requirements:\n- The global variable must be of type \"object\".\n- The global variable must have at least 5 properties.\n- One of the properties must be a nested object.\n- One of the properties must be an array.\n- The nest... | [
{
"content": "Write the code to set a global variable in JavaScript with the following requirements:\n- The global variable must be of type \"object\".\n- The global variable must have at least 5 properties.\n- One of the properties must be a nested object.\n- One of the properties must be an array.\n- The nest... | 6 | 4 |
Write a JavaScript function to get the current day of the month, but you are not allowed to use the Date object or any built-in date functions. | [
{
"content": "Write a JavaScript function to get the current day of the month, but you are not allowed to use the Date object or any built-in date functions.",
"role": "user"
},
{
"content": "Here is a JavaScript function that uses the `new Date()` constructor and a few basic operations to get the c... | [
{
"content": "Write a JavaScript function to get the current day of the month, but you are not allowed to use the Date object or any built-in date functions.",
"role": "user"
},
{
"content": "Here is a JavaScript function that uses the `new Date()` constructor and a few basic operations to get the c... | 8 | 4 |
Write a code snippet to display a website's current URL and the timestamp when the page was last refreshed. | [
{
"content": "Write a code snippet to display a website's current URL and the timestamp when the page was last refreshed.",
"role": "user"
},
{
"content": "Sure! Here's a code snippet in JavaScript that you can use to display the current URL and the timestamp when the page was last refreshed:\n\n```... | [
{
"content": "Write a code snippet to display a website's current URL and the timestamp when the page was last refreshed.",
"role": "user"
},
{
"content": "Sure! Here's a code snippet in JavaScript that you can use to display the current URL and the timestamp when the page was last refreshed:\n\n```... | 9 | 2 |
Determine and output the last occurrence of the given number in an array. If the number does not exist in the array, output -1.
let my_array = [2, 4, 8, 6, 2];
let my_number = 2;
#Erroneous Code#
Determine and output the first occurrence of the given number in an array. If the number does not exist in the array, outpu... | [
{
"content": "Determine and output the last occurrence of the given number in an array. If the number does not exist in the array, output -1.\nlet my_array = [2, 4, 8, 6, 2];\nlet my_number = 2;\n\n#Erroneous Code#\nDetermine and output the first occurrence of the given number in an array. If the number does no... | [
{
"content": "Determine and output the last occurrence of the given number in an array. If the number does not exist in the array, output -1.\nlet my_array = [2, 4, 8, 6, 2];\nlet my_number = 2;\n\n#Erroneous Code#\nDetermine and output the first occurrence of the given number in an array. If the number does no... | 9 | 2 |
Write a code snippet to query the following json object using JavaScript. Implement the code in a way that it also retrieves and displays the values of any nested objects or arrays within the given json object. Additionally, ensure that the code handles any potential errors or exceptions that may occur during the query... | [
{
"content": "Write a code snippet to query the following json object using JavaScript. Implement the code in a way that it also retrieves and displays the values of any nested objects or arrays within the given json object. Additionally, ensure that the code handles any potential errors or exceptions that may ... | [
{
"content": "Write a code snippet to query the following json object using JavaScript. Implement the code in a way that it also retrieves and displays the values of any nested objects or arrays within the given json object. Additionally, ensure that the code handles any potential errors or exceptions that may ... | 9 | 3 |
Write a JavaScript function to flatten an array up to the given depth. The depth is specified as a parameter to the function. The input array can contain nested arrays up to the maximum depth of 100. The function should return a new flattened array. The original array should not be modified. | [
{
"content": "Write a JavaScript function to flatten an array up to the given depth. The depth is specified as a parameter to the function. The input array can contain nested arrays up to the maximum depth of 100. The function should return a new flattened array. The original array should not be modified.",
... | [
{
"content": "Write a JavaScript function to flatten an array up to the given depth. The depth is specified as a parameter to the function. The input array can contain nested arrays up to the maximum depth of 100. The function should return a new flattened array. The original array should not be modified.",
... | 9 | 4 |
Write a JavaScript function that takes in two parameters: a string and an array of numbers. The function should calculate the average of all the numbers in the array and return a new string that concatenates the original string parameter with the calculated average, rounded to two decimal places. The returned string sh... | [
{
"content": "Write a JavaScript function that takes in two parameters: a string and an array of numbers. The function should calculate the average of all the numbers in the array and return a new string that concatenates the original string parameter with the calculated average, rounded to two decimal places. ... | [
{
"content": "Write a JavaScript function that takes in two parameters: a string and an array of numbers. The function should calculate the average of all the numbers in the array and return a new string that concatenates the original string parameter with the calculated average, rounded to two decimal places. ... | 6 | 3 |
Write a Mongoose query to remove a document with a date field that has a value of 2021-01-05 and a nested object field called "status" with a value of "active". | [
{
"content": "Write a Mongoose query to remove a document with a date field that has a value of 2021-01-05 and a nested object field called \"status\" with a value of \"active\".",
"role": "user"
},
{
"content": "To remove a document with a date field equal to \"2021-01-05\" and a nested object fiel... | [
{
"content": "Write a Mongoose query to remove a document with a date field that has a value of 2021-01-05 and a nested object field called \"status\" with a value of \"active\".",
"role": "user"
},
{
"content": "To remove a document with a date field equal to \"2021-01-05\" and a nested object fiel... | 8 | 1 |
Write a JavaScript program for converting a two-dimensional array into a one-dimensional array, while also checking for any empty arrays in the input.
[
[1,2],
[],
[3,4],
[],
[5,6]
] | [
{
"content": "Write a JavaScript program for converting a two-dimensional array into a one-dimensional array, while also checking for any empty arrays in the input.\n\n[\n [1,2],\n [],\n [3,4],\n [],\n [5,6]\n]",
"role": "user"
},
{
"content": "Here's a JavaScript program that convert... | [
{
"content": "Write a JavaScript program for converting a two-dimensional array into a one-dimensional array, while also checking for any empty arrays in the input.\n\n[\n [1,2],\n [],\n [3,4],\n [],\n [5,6]\n]",
"role": "user"
},
{
"content": "Here's a JavaScript program that convert... | 7 | 2 |
Use the reduce method in JavaScript to add a given array of numbers together, but only for numbers that are odd.
[1, 2, 3, 4, 5] | [
{
"content": "Use the reduce method in JavaScript to add a given array of numbers together, but only for numbers that are odd.\n[1, 2, 3, 4, 5]",
"role": "user"
},
{
"content": "To add only the odd numbers in the given array together using the reduce method in JavaScript, you can use the following c... | [
{
"content": "Use the reduce method in JavaScript to add a given array of numbers together, but only for numbers that are odd.\n[1, 2, 3, 4, 5]",
"role": "user"
},
{
"content": "To add only the odd numbers in the given array together using the reduce method in JavaScript, you can use the following c... | 7 | 2 |
Create a Node.js module to return the highest value in a multidimensional array. Each element in the array can either be a number or another array. | [
{
"content": "Create a Node.js module to return the highest value in a multidimensional array. Each element in the array can either be a number or another array.",
"role": "user"
},
{
"content": "Sure! Here's an example of a Node.js module that returns the highest value in a multidimensional array:\... | [
{
"content": "Create a Node.js module to return the highest value in a multidimensional array. Each element in the array can either be a number or another array.",
"role": "user"
},
{
"content": "Sure! Here's an example of a Node.js module that returns the highest value in a multidimensional array:\... | 8 | 3 |
Write the code to connect a database in Node.JS and retrieve all the records from a specific table called "users". | [
{
"content": "Write the code to connect a database in Node.JS and retrieve all the records from a specific table called \"users\".",
"role": "user"
},
{
"content": "To connect to a database in Node.js and retrieve all the records from a specific table called \"users\", you will need to use a databas... | [
{
"content": "Write the code to connect a database in Node.JS and retrieve all the records from a specific table called \"users\".",
"role": "user"
},
{
"content": "To connect to a database in Node.js and retrieve all the records from a specific table called \"users\", you will need to use a databas... | 7 | 2 |
Write a function in JavaScript that takes 2 parameters of type string and concatenates them, without using the built-in string concatenation operator (+) or the string concatenation method (concat()). The function should have a time complexity of O(n), where n is the length of the resulting concatenated string. | [
{
"content": "Write a function in JavaScript that takes 2 parameters of type string and concatenates them, without using the built-in string concatenation operator (+) or the string concatenation method (concat()). The function should have a time complexity of O(n), where n is the length of the resulting concat... | [
{
"content": "Write a function in JavaScript that takes 2 parameters of type string and concatenates them, without using the built-in string concatenation operator (+) or the string concatenation method (concat()). The function should have a time complexity of O(n), where n is the length of the resulting concat... | 7 | 1 |
Write a JavaScript function that takes in two arguments: a positive integer and a string. The function should create an array with a size equal to the integer argument, and fill it with the characters given in the second argument. If the integer argument is less than 1, the function should return an empty array. If the... | [
{
"content": "Write a JavaScript function that takes in two arguments: a positive integer and a string. The function should create an array with a size equal to the integer argument, and fill it with the characters given in the second argument. If the integer argument is less than 1, the function should return ... | [
{
"content": "Write a JavaScript function that takes in two arguments: a positive integer and a string. The function should create an array with a size equal to the integer argument, and fill it with the characters given in the second argument. If the integer argument is less than 1, the function should return ... | 9 | 2 |
In JavaScript, construct a function that takes two string arguments and returns the concatenation of the two strings, but with each character repeated twice. | [
{
"content": "In JavaScript, construct a function that takes two string arguments and returns the concatenation of the two strings, but with each character repeated twice.",
"role": "user"
},
{
"content": "Here is the JavaScript function that takes two string arguments and returns the concatenation ... | [
{
"content": "In JavaScript, construct a function that takes two string arguments and returns the concatenation of the two strings, but with each character repeated twice.",
"role": "user"
},
{
"content": "Here is the JavaScript function that takes two string arguments and returns the concatenation ... | 6 | 1 |
Write a JavaScript function to format a given number of seconds into hours, minutes, and seconds. The function should return a string in the format "HH:MM:SS". The input number will always be a positive integer less than 24 hours (86400 seconds).
For example:
- If the input is 85, the output should be "00:01:25".
- If... | [
{
"content": "Write a JavaScript function to format a given number of seconds into hours, minutes, and seconds. The function should return a string in the format \"HH:MM:SS\". The input number will always be a positive integer less than 24 hours (86400 seconds).\n\nFor example:\n- If the input is 85, the output... | [
{
"content": "Write a JavaScript function to format a given number of seconds into hours, minutes, and seconds. The function should return a string in the format \"HH:MM:SS\". The input number will always be a positive integer less than 24 hours (86400 seconds).\n\nFor example:\n- If the input is 85, the output... | 8 | 1 |
Write a JavaScript code to calculate the factorial of a number. The code should handle negative numbers as input and return an error message for negative input. Additionally, the code should handle decimal numbers as input and return an error message for decimal input.
#Explanation#
Factorial is the product of all pos... | [
{
"content": "Write a JavaScript code to calculate the factorial of a number. The code should handle negative numbers as input and return an error message for negative input. Additionally, the code should handle decimal numbers as input and return an error message for decimal input.\n\n#Explanation#\nFactorial ... | [
{
"content": "Write a JavaScript code to calculate the factorial of a number. The code should handle negative numbers as input and return an error message for negative input. Additionally, the code should handle decimal numbers as input and return an error message for decimal input.\n\n#Explanation#\nFactorial ... | 7 | 1 |
Write a JavaScript function to check if an array contains a specific element and return the index of that element. If the element is not found, return -1. The array can contain both numbers and strings.
var arr = [1,2,3,4,"apple","banana","orange"];
Please provide a solution that has a time complexity of O(n), where ... | [
{
"content": "Write a JavaScript function to check if an array contains a specific element and return the index of that element. If the element is not found, return -1. The array can contain both numbers and strings.\n\nvar arr = [1,2,3,4,\"apple\",\"banana\",\"orange\"];\n\nPlease provide a solution that has a... | [
{
"content": "Write a JavaScript function to check if an array contains a specific element and return the index of that element. If the element is not found, return -1. The array can contain both numbers and strings.\n\nvar arr = [1,2,3,4,\"apple\",\"banana\",\"orange\"];\n\nPlease provide a solution that has a... | 9 | 3 |
Create a JavaScript function that takes in an array of integers and returns the sum of all the numbers in the array, excluding any duplicate numbers.
Function Signature: `function findSum(arr: number[]): number`
**Input**
- An array of integers `arr` (1 <= arr.length <= 10^5)
**Output**
- Returns the sum of all the... | [
{
"content": "Create a JavaScript function that takes in an array of integers and returns the sum of all the numbers in the array, excluding any duplicate numbers.\n\nFunction Signature: `function findSum(arr: number[]): number`\n\n**Input**\n- An array of integers `arr` (1 <= arr.length <= 10^5) \n\n**Output**... | [
{
"content": "Create a JavaScript function that takes in an array of integers and returns the sum of all the numbers in the array, excluding any duplicate numbers.\n\nFunction Signature: `function findSum(arr: number[]): number`\n\n**Input**\n- An array of integers `arr` (1 <= arr.length <= 10^5) \n\n**Output**... | 8 | 4 |
Write a JavaScript function to check whether a given number is a prime number or not. The input number will always be greater than 1 and less than 10^9.
num = 21 | [
{
"content": "Write a JavaScript function to check whether a given number is a prime number or not. The input number will always be greater than 1 and less than 10^9.\nnum = 21",
"role": "user"
},
{
"content": "Here is a JavaScript function that checks whether a given number is a prime number or not... | [
{
"content": "Write a JavaScript function to check whether a given number is a prime number or not. The input number will always be greater than 1 and less than 10^9.\nnum = 21",
"role": "user"
},
{
"content": "Here is a JavaScript function that checks whether a given number is a prime number or not... | 7 | 1 |
Create a JavaScript class that calculates and returns the area, circumference, diameter, and radius of a circle when given the radius.
The class should have the following methods:
1. `calculateArea()`: This method should calculate and return the area of the circle using the formula: area = π * radius^2. The value of ... | [
{
"content": "Create a JavaScript class that calculates and returns the area, circumference, diameter, and radius of a circle when given the radius.\n\nThe class should have the following methods:\n\n1. `calculateArea()`: This method should calculate and return the area of the circle using the formula: area = π... | [
{
"content": "Create a JavaScript class that calculates and returns the area, circumference, diameter, and radius of a circle when given the radius.\n\nThe class should have the following methods:\n\n1. `calculateArea()`: This method should calculate and return the area of the circle using the formula: area = π... | 9 | 2 |
Create a loop in JavaScript that prints the numbers from 1 to 100 in the console. Additionally, for every number that is divisible by 3, print "Fizz" instead of the number. For every number that is divisible by 5, print "Buzz" instead of the number. For numbers that are divisible by both 3 and 5, print "FizzBuzz". Opti... | [
{
"content": "Create a loop in JavaScript that prints the numbers from 1 to 100 in the console. Additionally, for every number that is divisible by 3, print \"Fizz\" instead of the number. For every number that is divisible by 5, print \"Buzz\" instead of the number. For numbers that are divisible by both 3 and... | [
{
"content": "Create a loop in JavaScript that prints the numbers from 1 to 100 in the console. Additionally, for every number that is divisible by 3, print \"Fizz\" instead of the number. For every number that is divisible by 5, print \"Buzz\" instead of the number. For numbers that are divisible by both 3 and... | 6 | 2 |
Create an array of the numbers from 1 to 10, ensuring that the array is sorted in descending order. Additionally, write a function to reverse the elements in the array without using any built-in functions or methods. | [
{
"content": "Create an array of the numbers from 1 to 10, ensuring that the array is sorted in descending order. Additionally, write a function to reverse the elements in the array without using any built-in functions or methods.",
"role": "user"
},
{
"content": "To create an array of numbers from ... | [
{
"content": "Create an array of the numbers from 1 to 10, ensuring that the array is sorted in descending order. Additionally, write a function to reverse the elements in the array without using any built-in functions or methods.",
"role": "user"
},
{
"content": "To create an array of numbers from ... | 9 | 4 |
Write a JavaScript code snippet to retrieve the value of an input field with an id of "name" and display it in uppercase letters. Additionally, check if the input is a valid string containing only alphabetical characters before converting it to uppercase. If the input is not valid, display an error message. | [
{
"content": "Write a JavaScript code snippet to retrieve the value of an input field with an id of \"name\" and display it in uppercase letters. Additionally, check if the input is a valid string containing only alphabetical characters before converting it to uppercase. If the input is not valid, display an er... | [
{
"content": "Write a JavaScript code snippet to retrieve the value of an input field with an id of \"name\" and display it in uppercase letters. Additionally, check if the input is a valid string containing only alphabetical characters before converting it to uppercase. If the input is not valid, display an er... | 9 | 1 |
Write a JavaScript function to convert a number from octal to decimal.
*Example:*
```javascript
console.log(convertOctalToDecimal(014)); // Output: 12
console.log(convertOctalToDecimal(015)); // Output: 13
```
*Notes:*
- Octal numbers are represented with a leading zero (e.g. 014).
- The octal number system uses onl... | [
{
"content": "Write a JavaScript function to convert a number from octal to decimal. \n\n*Example:*\n```javascript\nconsole.log(convertOctalToDecimal(014)); // Output: 12\nconsole.log(convertOctalToDecimal(015)); // Output: 13\n```\n\n*Notes:*\n- Octal numbers are represented with a leading zero (e.g. 014).\n- ... | [
{
"content": "Write a JavaScript function to convert a number from octal to decimal. \n\n*Example:*\n```javascript\nconsole.log(convertOctalToDecimal(014)); // Output: 12\nconsole.log(convertOctalToDecimal(015)); // Output: 13\n```\n\n*Notes:*\n- Octal numbers are represented with a leading zero (e.g. 014).\n- ... | 8 | 3 |
Write a function in JavaScript that takes in an array and returns a new array containing only unique values, while ensuring that the new array has a maximum length of 10 elements. | [
{
"content": "Write a function in JavaScript that takes in an array and returns a new array containing only unique values, while ensuring that the new array has a maximum length of 10 elements.",
"role": "user"
},
{
"content": "Here is an example of a JavaScript function that takes in an array and r... | [
{
"content": "Write a function in JavaScript that takes in an array and returns a new array containing only unique values, while ensuring that the new array has a maximum length of 10 elements.",
"role": "user"
},
{
"content": "Here is an example of a JavaScript function that takes in an array and r... | 8 | 1 |
Suggest an efficient approach to add items to an array in JavaScript, while maintaining a constant time complexity. Additionally, ensure that the array has a maximum capacity of 100 elements and implement a check to prevent adding duplicate items. | [
{
"content": "Suggest an efficient approach to add items to an array in JavaScript, while maintaining a constant time complexity. Additionally, ensure that the array has a maximum capacity of 100 elements and implement a check to prevent adding duplicate items.",
"role": "user"
},
{
"content": "One ... | [
{
"content": "Suggest an efficient approach to add items to an array in JavaScript, while maintaining a constant time complexity. Additionally, ensure that the array has a maximum capacity of 100 elements and implement a check to prevent adding duplicate items.",
"role": "user"
},
{
"content": "One ... | 9 | 3 |
Write a JavaScript program to convert a Celsius temperature to Fahrenheit, but this time, you need to implement the conversion formula without using the '*' operator and using only a maximum of three mathematical operators in your solution. | [
{
"content": "Write a JavaScript program to convert a Celsius temperature to Fahrenheit, but this time, you need to implement the conversion formula without using the '*' operator and using only a maximum of three mathematical operators in your solution.",
"role": "user"
},
{
"content": "Here's a Ja... | [
{
"content": "Write a JavaScript program to convert a Celsius temperature to Fahrenheit, but this time, you need to implement the conversion formula without using the '*' operator and using only a maximum of three mathematical operators in your solution.",
"role": "user"
},
{
"content": "Here's a Ja... | 8 | 2 |
Create a JavaScript function to simulate rolling a pair of fair dice with a random number generator, but restrict the outcome to only odd numbers. The function should return an array of all possible outcomes as pairs of odd numbers.
For example, if the dice are rolled 3 times, the function may return [[1,3], [5,7], [9... | [
{
"content": "Create a JavaScript function to simulate rolling a pair of fair dice with a random number generator, but restrict the outcome to only odd numbers. The function should return an array of all possible outcomes as pairs of odd numbers.\n\nFor example, if the dice are rolled 3 times, the function may ... | [
{
"content": "Create a JavaScript function to simulate rolling a pair of fair dice with a random number generator, but restrict the outcome to only odd numbers. The function should return an array of all possible outcomes as pairs of odd numbers.\n\nFor example, if the dice are rolled 3 times, the function may ... | 9 | 1 |
Convert the following Celsius temperature to Fahrenheit using a JavaScript function. The temperature should be rounded to the nearest whole number and the result should be displayed with exactly 2 decimal places.
temperature = -4.5
# Reference Error #
```javascript
function celsiusToFahrenheit(temperature) {
retur... | [
{
"content": "Convert the following Celsius temperature to Fahrenheit using a JavaScript function. The temperature should be rounded to the nearest whole number and the result should be displayed with exactly 2 decimal places.\n\ntemperature = -4.5\n\n# Reference Error #\n\n```javascript\nfunction celsiusToFahr... | [
{
"content": "Convert the following Celsius temperature to Fahrenheit using a JavaScript function. The temperature should be rounded to the nearest whole number and the result should be displayed with exactly 2 decimal places.\n\ntemperature = -4.5\n\n# Reference Error #\n\n```javascript\nfunction celsiusToFahr... | 9 | 1 |
Write a JavaScript program to detect the browser type and version, and also determine if the browser supports certain features like HTML5 and CSS3. Additionally, the program should be able to handle any future browser types that may be released.
Provide a piece of erroneous code that tries to detect the browser type a... | [
{
"content": "Write a JavaScript program to detect the browser type and version, and also determine if the browser supports certain features like HTML5 and CSS3. Additionally, the program should be able to handle any future browser types that may be released.\n\nProvide a piece of erroneous code that tries to d... | [
{
"content": "Write a JavaScript program to detect the browser type and version, and also determine if the browser supports certain features like HTML5 and CSS3. Additionally, the program should be able to handle any future browser types that may be released.\n\nProvide a piece of erroneous code that tries to d... | 6 | 2 |
Sum up an array of numbers to a single number using JavaScript. The array may contain negative numbers and floating-point numbers as well. The resulting sum should be rounded to the nearest integer. The implementation should have a time complexity of O(n), where n is the size of the input array. Additionally, the imple... | [
{
"content": "Sum up an array of numbers to a single number using JavaScript. The array may contain negative numbers and floating-point numbers as well. The resulting sum should be rounded to the nearest integer. The implementation should have a time complexity of O(n), where n is the size of the input array. A... | [
{
"content": "Sum up an array of numbers to a single number using JavaScript. The array may contain negative numbers and floating-point numbers as well. The resulting sum should be rounded to the nearest integer. The implementation should have a time complexity of O(n), where n is the size of the input array. A... | 7 | 1 |
Create a JavaScript object for employee records with the following properties: name, age, department, and salary. Implement the following methods for the object:
1. getDetails(): This method should return a string with the employee's name, age, department, and salary.
2. changeDepartment(newDepartment): This method s... | [
{
"content": "Create a JavaScript object for employee records with the following properties: name, age, department, and salary. Implement the following methods for the object:\n\n1. getDetails(): This method should return a string with the employee's name, age, department, and salary.\n\n2. changeDepartment(new... | [
{
"content": "Create a JavaScript object for employee records with the following properties: name, age, department, and salary. Implement the following methods for the object:\n\n1. getDetails(): This method should return a string with the employee's name, age, department, and salary.\n\n2. changeDepartment(new... | 9 | 2 |
Given an array of integers, find the maximum value in the array and output it in JavaScript. However, you are not allowed to use any built-in JavaScript functions or methods that directly solve this problem, such as Math.max() or Array.prototype.reduce(). You must implement your own solution.
#Additional Information#
... | [
{
"content": "Given an array of integers, find the maximum value in the array and output it in JavaScript. However, you are not allowed to use any built-in JavaScript functions or methods that directly solve this problem, such as Math.max() or Array.prototype.reduce(). You must implement your own solution.\n\n#... | [
{
"content": "Given an array of integers, find the maximum value in the array and output it in JavaScript. However, you are not allowed to use any built-in JavaScript functions or methods that directly solve this problem, such as Math.max() or Array.prototype.reduce(). You must implement your own solution.\n\n#... | 9 | 4 |
Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 100 in ascending order. | [
{
"content": "Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 100 in ascending order.",
"role": "user"
},
{
"content": "Here is an example of a while loop in JavaScript that prints the prime numbers from 1 to 100 in ascending order:\n\n```j... | [
{
"content": "Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 100 in ascending order.",
"role": "user"
},
{
"content": "Here is an example of a while loop in JavaScript that prints the prime numbers from 1 to 100 in ascending order:\n\n```j... | 9 | 2 |
Rewrite the HTML element with the id "container" to have 2 classes: "class1" and "class2". Additionally, add a new attribute called "data-id" with a value of "123". Ensure that the changes are made dynamically using JavaScript. | [
{
"content": "Rewrite the HTML element with the id \"container\" to have 2 classes: \"class1\" and \"class2\". Additionally, add a new attribute called \"data-id\" with a value of \"123\". Ensure that the changes are made dynamically using JavaScript.",
"role": "user"
},
{
"content": "To dynamically... | [
{
"content": "Rewrite the HTML element with the id \"container\" to have 2 classes: \"class1\" and \"class2\". Additionally, add a new attribute called \"data-id\" with a value of \"123\". Ensure that the changes are made dynamically using JavaScript.",
"role": "user"
},
{
"content": "To dynamically... | 7 | 2 |
Write a Node.js function that takes an array of positive integers and returns the maximum value. The array should contain at least 5 elements and at most 10,000 elements. The function should handle arrays with up to 10,000 elements efficiently by implementing an optimized algorithm that avoids unnecessary iterations. A... | [
{
"content": "Write a Node.js function that takes an array of positive integers and returns the maximum value. The array should contain at least 5 elements and at most 10,000 elements. The function should handle arrays with up to 10,000 elements efficiently by implementing an optimized algorithm that avoids unn... | [
{
"content": "Write a Node.js function that takes an array of positive integers and returns the maximum value. The array should contain at least 5 elements and at most 10,000 elements. The function should handle arrays with up to 10,000 elements efficiently by implementing an optimized algorithm that avoids unn... | 7 | 1 |
Create a JavaScript Object for a Student with key-value pairs for their id, name, nationality, and age. Additionally, ensure that the id is a unique identifier and cannot be changed once set. Implement a method that calculates and stores the average grade of the student, where the grade is a number between 0 and 100. | [
{
"content": "Create a JavaScript Object for a Student with key-value pairs for their id, name, nationality, and age. Additionally, ensure that the id is a unique identifier and cannot be changed once set. Implement a method that calculates and stores the average grade of the student, where the grade is a numbe... | [
{
"content": "Create a JavaScript Object for a Student with key-value pairs for their id, name, nationality, and age. Additionally, ensure that the id is a unique identifier and cannot be changed once set. Implement a method that calculates and stores the average grade of the student, where the grade is a numbe... | 7 | 2 |
Write a JavaScript function that converts a given time in the format HH:MM:SS (24-hour format) to the 12-hour format, including the AM/PM indicator. The function should return the converted time as a string.
For example, if the input is "15:30:45", the function should return "3:30:45 PM".
#Erroneous Code Reference#
W... | [
{
"content": "Write a JavaScript function that converts a given time in the format HH:MM:SS (24-hour format) to the 12-hour format, including the AM/PM indicator. The function should return the converted time as a string.\n\nFor example, if the input is \"15:30:45\", the function should return \"3:30:45 PM\".\n... | [
{
"content": "Write a JavaScript function that converts a given time in the format HH:MM:SS (24-hour format) to the 12-hour format, including the AM/PM indicator. The function should return the converted time as a string.\n\nFor example, if the input is \"15:30:45\", the function should return \"3:30:45 PM\".\n... | 6 | 1 |
Design a JavaScript function that checks if a number is between -100 and 100, inclusive, and is divisible by both 5 and 3. | [
{
"content": "Design a JavaScript function that checks if a number is between -100 and 100, inclusive, and is divisible by both 5 and 3.",
"role": "user"
},
{
"content": "Here's an example of a JavaScript function that checks if a number meets the given criteria:\n\n```javascriptfunction isDivisible... | [
{
"content": "Design a JavaScript function that checks if a number is between -100 and 100, inclusive, and is divisible by both 5 and 3.",
"role": "user"
},
{
"content": "Here's an example of a JavaScript function that checks if a number meets the given criteria:\n\n```javascript\nfunction isDivisib... | 8 | 1 |
Write a JavaScript function that takes in a string as input and returns the total number of non-whitespace characters in the string. The function should ignore any leading or trailing whitespace and should only count characters within the string itself. The input string can contain alphanumeric characters, special char... | [
{
"content": "Write a JavaScript function that takes in a string as input and returns the total number of non-whitespace characters in the string. The function should ignore any leading or trailing whitespace and should only count characters within the string itself. The input string can contain alphanumeric ch... | [
{
"content": "Write a JavaScript function that takes in a string as input and returns the total number of non-whitespace characters in the string. The function should ignore any leading or trailing whitespace and should only count characters within the string itself. The input string can contain alphanumeric ch... | 7 | 3 |
Create a for loop in JavaScript that iterates through a given array and prints each element. However, you are not allowed to use the built-in `forEach` method or any other array method for iteration.
arr = ["a","b","c","d"]
In addition to printing each element, you need to calculate and print the index of each elemen... | [
{
"content": "Create a for loop in JavaScript that iterates through a given array and prints each element. However, you are not allowed to use the built-in `forEach` method or any other array method for iteration.\n\narr = [\"a\",\"b\",\"c\",\"d\"]\n\nIn addition to printing each element, you need to calculate ... | [
{
"content": "Create a for loop in JavaScript that iterates through a given array and prints each element. However, you are not allowed to use the built-in `forEach` method or any other array method for iteration.\n\narr = [\"a\",\"b\",\"c\",\"d\"]\n\nIn addition to printing each element, you need to calculate ... | 7 | 3 |
Develop a JavaScript program to print the average of a given set of numbers. The program should also handle cases where the set of numbers may contain negative values. Additionally, the program should only consider numbers that are divisible by 3 in the calculation of the average. | [
{
"content": "Develop a JavaScript program to print the average of a given set of numbers. The program should also handle cases where the set of numbers may contain negative values. Additionally, the program should only consider numbers that are divisible by 3 in the calculation of the average.",
"role": "u... | [
{
"content": "Develop a JavaScript program to print the average of a given set of numbers. The program should also handle cases where the set of numbers may contain negative values. Additionally, the program should only consider numbers that are divisible by 3 in the calculation of the average.",
"role": "u... | 9 | 2 |
Write a JavaScript function that takes in a JSON object and a key as parameters and returns the corresponding value. If the key does not exist in the object, the function should return null. The time complexity of the function should be O(1).
Additionally, the function should handle nested keys and return the value fo... | [
{
"content": "Write a JavaScript function that takes in a JSON object and a key as parameters and returns the corresponding value. If the key does not exist in the object, the function should return null. The time complexity of the function should be O(1).\n\nAdditionally, the function should handle nested keys... | [
{
"content": "Write a JavaScript function that takes in a JSON object and a key as parameters and returns the corresponding value. If the key does not exist in the object, the function should return null. The time complexity of the function should be O(1).\n\nAdditionally, the function should handle nested keys... | 7 | 2 |
Write a function in JavaScript for computing the average value of a given array. The array may contain both positive and negative integers, and its length should be at least 10. The function should handle cases where the array contains floating-point numbers and return the average value rounded to two decimal places.
... | [
{
"content": "Write a function in JavaScript for computing the average value of a given array. The array may contain both positive and negative integers, and its length should be at least 10. The function should handle cases where the array contains floating-point numbers and return the average value rounded to... | [
{
"content": "Write a function in JavaScript for computing the average value of a given array. The array may contain both positive and negative integers, and its length should be at least 10. The function should handle cases where the array contains floating-point numbers and return the average value rounded to... | 7 | 1 |
Please implement a function in JavaScript that finds the maximum value in an array of numbers. The time complexity should be O(n) and the space complexity should be O(1).
The function should also return the index of the maximum value in addition to the value itself. | [
{
"content": "Please implement a function in JavaScript that finds the maximum value in an array of numbers. The time complexity should be O(n) and the space complexity should be O(1).\n\nThe function should also return the index of the maximum value in addition to the value itself.",
"role": "user"
},
... | [
{
"content": "Please implement a function in JavaScript that finds the maximum value in an array of numbers. The time complexity should be O(n) and the space complexity should be O(1).\n\nThe function should also return the index of the maximum value in addition to the value itself.",
"role": "user"
},
... | 9 | 2 |
Write a JavaScript program to check whether two or more arrays have the same elements and the same number of occurrences of each element.
array1 = [1, 2, 2, 4];
array2 = [1, 2, 2, 4];
#Erroneous Code#
Write a JavaScript program to check whether two or more arrays have the same elements and the same number of occurren... | [
{
"content": "Write a JavaScript program to check whether two or more arrays have the same elements and the same number of occurrences of each element.\n\narray1 = [1, 2, 2, 4];\narray2 = [1, 2, 2, 4];\n\n#Erroneous Code#\nWrite a JavaScript program to check whether two or more arrays have the same elements and... | [
{
"content": "Write a JavaScript program to check whether two or more arrays have the same elements and the same number of occurrences of each element.\n\narray1 = [1, 2, 2, 4];\narray2 = [1, 2, 2, 4];\n\n#Erroneous Code#\nWrite a JavaScript program to check whether two or more arrays have the same elements and... | 9 | 3 |
Write a JavaScript function that takes in an array of numbers and checks if there are any duplicate numbers. If there are, return the string "Duplicate numbers found", otherwise return the string "No duplicate numbers found".
#Additional Requirements#
1. The function should have a time complexity of O(n), where n is t... | [
{
"content": "Write a JavaScript function that takes in an array of numbers and checks if there are any duplicate numbers. If there are, return the string \"Duplicate numbers found\", otherwise return the string \"No duplicate numbers found\".\n\n#Additional Requirements#\n1. The function should have a time com... | [
{
"content": "Write a JavaScript function that takes in an array of numbers and checks if there are any duplicate numbers. If there are, return the string \"Duplicate numbers found\", otherwise return the string \"No duplicate numbers found\".\n\n#Additional Requirements#\n1. The function should have a time com... | 7 | 1 |
Write a function in JavaScript to perform the following operation:
Multiply two numbers a and b. The function should have a time complexity of O(1) and should not use the * operator. | [
{
"content": "Write a function in JavaScript to perform the following operation:\nMultiply two numbers a and b. The function should have a time complexity of O(1) and should not use the * operator.",
"role": "user"
},
{
"content": "To multiply two numbers a and b without using the * operator and wit... | [
{
"content": "Write a function in JavaScript to perform the following operation:\nMultiply two numbers a and b. The function should have a time complexity of O(1) and should not use the * operator.",
"role": "user"
},
{
"content": "To multiply two numbers a and b without using the * operator and wit... | 6 | 1 |
Write a JavaScript function that takes a string argument and returns a count of all the times the letter 'a' appears in the string. Additionally, the function should also count the number of times the letter 'A' appears in the string, treating it as a different character than 'a'. The function should ignore any occurre... | [
{
"content": "Write a JavaScript function that takes a string argument and returns a count of all the times the letter 'a' appears in the string. Additionally, the function should also count the number of times the letter 'A' appears in the string, treating it as a different character than 'a'. The function sho... | [
{
"content": "Write a JavaScript function that takes a string argument and returns a count of all the times the letter 'a' appears in the string. Additionally, the function should also count the number of times the letter 'A' appears in the string, treating it as a different character than 'a'. The function sho... | 8 | 4 |
Construct an object in JavaScript to model a car with make, model, year, color, price, and mileage as attributes. Implement a method to calculate the average fuel consumption of the car based on the mileage and fuel tank capacity. Additionally, create a function to compare the fuel efficiency of two cars and determine ... | [
{
"content": "Construct an object in JavaScript to model a car with make, model, year, color, price, and mileage as attributes. Implement a method to calculate the average fuel consumption of the car based on the mileage and fuel tank capacity. Additionally, create a function to compare the fuel efficiency of t... | [
{
"content": "Construct an object in JavaScript to model a car with make, model, year, color, price, and mileage as attributes. Implement a method to calculate the average fuel consumption of the car based on the mileage and fuel tank capacity. Additionally, create a function to compare the fuel efficiency of t... | 8 | 1 |
Create a JavaScript function that takes in an array of numbers and returns a new array where each number is rounded up to the nearest whole number. The function should not use any built-in JavaScript rounding functions. | [
{
"content": "Create a JavaScript function that takes in an array of numbers and returns a new array where each number is rounded up to the nearest whole number. The function should not use any built-in JavaScript rounding functions.",
"role": "user"
},
{
"content": "Here's a JavaScript function tha... | [
{
"content": "Create a JavaScript function that takes in an array of numbers and returns a new array where each number is rounded up to the nearest whole number. The function should not use any built-in JavaScript rounding functions.",
"role": "user"
},
{
"content": "Here's a JavaScript function tha... | 6 | 2 |
Find the sum of all numbers in the following array of objects in JavaScript. However, you must use the reduce() method to calculate the sum instead of using a loop. Additionally, you should only include numbers that are greater than 10 in the calculation. | [
{
"content": "Find the sum of all numbers in the following array of objects in JavaScript. However, you must use the reduce() method to calculate the sum instead of using a loop. Additionally, you should only include numbers that are greater than 10 in the calculation.",
"role": "user"
},
{
"content... | [
{
"content": "Find the sum of all numbers in the following array of objects in JavaScript. However, you must use the reduce() method to calculate the sum instead of using a loop. Additionally, you should only include numbers that are greater than 10 in the calculation.",
"role": "user"
},
{
"content... | 6 | 2 |
Create a Date object containing the current date and time in UTC, add 1 hour and 30 minutes to it, and then convert it to a string in the format "YYYY-MM-DDTHH:MM:SSZ". | [
{
"content": "Create a Date object containing the current date and time in UTC, add 1 hour and 30 minutes to it, and then convert it to a string in the format \"YYYY-MM-DDTHH:MM:SSZ\".",
"role": "user"
},
{
"content": "Here is an example code snippet in JavaScript to create a Date object, add 1 hour... | [
{
"content": "Create a Date object containing the current date and time in UTC, add 1 hour and 30 minutes to it, and then convert it to a string in the format \"YYYY-MM-DDTHH:MM:SSZ\".",
"role": "user"
},
{
"content": "Here is an example code snippet in JavaScript to create a Date object, add 1 hour... | 9 | 1 |
Create a while loop in JavaScript that reverses an array and prints each element to the console. The array to be reversed is ["apple", "banana", "orange", "grape"]. However, you are not allowed to use any built-in array manipulation methods such as reverse() or sort(). | [
{
"content": "Create a while loop in JavaScript that reverses an array and prints each element to the console. The array to be reversed is [\"apple\", \"banana\", \"orange\", \"grape\"]. However, you are not allowed to use any built-in array manipulation methods such as reverse() or sort().",
"role": "user"... | [
{
"content": "Create a while loop in JavaScript that reverses an array and prints each element to the console. The array to be reversed is [\"apple\", \"banana\", \"orange\", \"grape\"]. However, you are not allowed to use any built-in array manipulation methods such as reverse() or sort().",
"role": "user"... | 8 | 2 |
Write a JavaScript program to display the current date and time in the format "DD/MM/YYYY HH:MM:SS" for a specific timezone, taking into account daylight saving time adjustments if applicable. | [
{
"content": "Write a JavaScript program to display the current date and time in the format \"DD/MM/YYYY HH:MM:SS\" for a specific timezone, taking into account daylight saving time adjustments if applicable.",
"role": "user"
},
{
"content": "To display the current date and time in the specified for... | [
{
"content": "Write a JavaScript program to display the current date and time in the format \"DD/MM/YYYY HH:MM:SS\" for a specific timezone, taking into account daylight saving time adjustments if applicable.",
"role": "user"
},
{
"content": "To display the current date and time in the specified for... | 9 | 2 |
Create a boolean expression in JavaScript, which returns false if a variable named "result" is an empty string or a string consisting of only whitespace characters. The solution should have a time complexity of O(1) and should not use any built-in JavaScript methods or regular expressions. | [
{
"content": "Create a boolean expression in JavaScript, which returns false if a variable named \"result\" is an empty string or a string consisting of only whitespace characters. The solution should have a time complexity of O(1) and should not use any built-in JavaScript methods or regular expressions.",
... | [
{
"content": "Create a boolean expression in JavaScript, which returns false if a variable named \"result\" is an empty string or a string consisting of only whitespace characters. The solution should have a time complexity of O(1) and should not use any built-in JavaScript methods or regular expressions.",
... | 6 | 2 |
Create a JavaScript function to find the third smallest number in an array that may contain duplicate numbers. The function should return the third smallest distinct number. If there are not enough distinct numbers in the array to find the third smallest, the function should return undefined.
For example, given the ar... | [
{
"content": "Create a JavaScript function to find the third smallest number in an array that may contain duplicate numbers. The function should return the third smallest distinct number. If there are not enough distinct numbers in the array to find the third smallest, the function should return undefined.\n\nF... | [
{
"content": "Create a JavaScript function to find the third smallest number in an array that may contain duplicate numbers. The function should return the third smallest distinct number. If there are not enough distinct numbers in the array to find the third smallest, the function should return undefined.\n\nF... | 8 | 3 |
Write a JavaScript code snippet that checks if a cookie named "username" exists in the browser. If the cookie exists, retrieve the content of the cookie and print it to the console. Then, remove the cookie from the browser. If the cookie does not exist, display an error message "No username cookie found". | [
{
"content": "Write a JavaScript code snippet that checks if a cookie named \"username\" exists in the browser. If the cookie exists, retrieve the content of the cookie and print it to the console. Then, remove the cookie from the browser. If the cookie does not exist, display an error message \"No username coo... | [
{
"content": "Write a JavaScript code snippet that checks if a cookie named \"username\" exists in the browser. If the cookie exists, retrieve the content of the cookie and print it to the console. Then, remove the cookie from the browser. If the cookie does not exist, display an error message \"No username coo... | 9 | 3 |
Using JavaScript, create a function to find the longest word in a string, while ignoring any punctuation marks or special characters. Additionally, the function should return an array containing all the words that have the same length as the longest word. | [
{
"content": "Using JavaScript, create a function to find the longest word in a string, while ignoring any punctuation marks or special characters. Additionally, the function should return an array containing all the words that have the same length as the longest word.",
"role": "user"
},
{
"content... | [
{
"content": "Using JavaScript, create a function to find the longest word in a string, while ignoring any punctuation marks or special characters. Additionally, the function should return an array containing all the words that have the same length as the longest word.",
"role": "user"
},
{
"content... | 6 | 2 |
Write a JavaScript function that takes an array of numbers and returns the sum of all the numbers in the array. The function should also check if the array is sorted in ascending order and if not, it should sort the array before calculating the sum. Additionally, the function should ignore any negative numbers in the a... | [
{
"content": "Write a JavaScript function that takes an array of numbers and returns the sum of all the numbers in the array. The function should also check if the array is sorted in ascending order and if not, it should sort the array before calculating the sum. Additionally, the function should ignore any neg... | [
{
"content": "Write a JavaScript function that takes an array of numbers and returns the sum of all the numbers in the array. The function should also check if the array is sorted in ascending order and if not, it should sort the array before calculating the sum. Additionally, the function should ignore any neg... | 9 | 1 |
Create a Javascript function that will loop through an array of numbers and calculate the sum of all the elements. However, you are not allowed to use the '+' operator or any built-in functions for addition. The function should return the sum. The time complexity of the function should still be O(n), where n is the num... | [
{
"content": "Create a Javascript function that will loop through an array of numbers and calculate the sum of all the elements. However, you are not allowed to use the '+' operator or any built-in functions for addition. The function should return the sum. The time complexity of the function should still be O(... | [
{
"content": "Create a Javascript function that will loop through an array of numbers and calculate the sum of all the elements. However, you are not allowed to use the '+' operator or any built-in functions for addition. The function should return the sum. The time complexity of the function should still be O(... | 9 | 3 |
Create a JavaScript program that takes an array of numbers and returns the sum of the squares of all the positive numbers in the array. If there are no positive numbers in the array, the program should return 0.
Additionally, the program should exclude any duplicate positive numbers from the calculation. Only one inst... | [
{
"content": "Create a JavaScript program that takes an array of numbers and returns the sum of the squares of all the positive numbers in the array. If there are no positive numbers in the array, the program should return 0.\n\nAdditionally, the program should exclude any duplicate positive numbers from the ca... | [
{
"content": "Create a JavaScript program that takes an array of numbers and returns the sum of the squares of all the positive numbers in the array. If there are no positive numbers in the array, the program should return 0.\n\nAdditionally, the program should exclude any duplicate positive numbers from the ca... | 8 | 4 |
Create a function in JavaScript that takes in an array of numbers and returns the sum of all unique pairs of numbers in the array that add up to a given target number.
The function should have a time complexity of O(n^2), where n is the length of the array. | [
{
"content": "Create a function in JavaScript that takes in an array of numbers and returns the sum of all unique pairs of numbers in the array that add up to a given target number.\n\nThe function should have a time complexity of O(n^2), where n is the length of the array.",
"role": "user"
},
{
"co... | [
{
"content": "Create a function in JavaScript that takes in an array of numbers and returns the sum of all unique pairs of numbers in the array that add up to a given target number.\n\nThe function should have a time complexity of O(n^2), where n is the length of the array.",
"role": "user"
},
{
"co... | 9 | 3 |
Use JavaScript to find the area of a circle with a given radius. Additionally, you are required to implement error handling for negative radius inputs and display a custom error message. The program should also round the calculated area to two decimal places. | [
{
"content": "Use JavaScript to find the area of a circle with a given radius. Additionally, you are required to implement error handling for negative radius inputs and display a custom error message. The program should also round the calculated area to two decimal places.",
"role": "user"
},
{
"con... | [
{
"content": "Use JavaScript to find the area of a circle with a given radius. Additionally, you are required to implement error handling for negative radius inputs and display a custom error message. The program should also round the calculated area to two decimal places.",
"role": "user"
},
{
"con... | 9 | 3 |
Create a generic function that implements a for-each loop in JavaScript. The function should take an array and a callback function as parameters. It should iterate over each element in the array, but only call the callback function if the element meets a certain condition. The condition should be defined as an addition... | [
{
"content": "Create a generic function that implements a for-each loop in JavaScript. The function should take an array and a callback function as parameters. It should iterate over each element in the array, but only call the callback function if the element meets a certain condition. The condition should be ... | [
{
"content": "Create a generic function that implements a for-each loop in JavaScript. The function should take an array and a callback function as parameters. It should iterate over each element in the array, but only call the callback function if the element meets a certain condition. The condition should be ... | 8 | 4 |
Create a for loop for iterating over the elements of an array in JavaScript and calculate the sum of the elements. However, the code contains a logical error that causes the sum to be multiplied by 2 instead of added to.
Array: [1, 2, 3, 4]
Time Complexity Requirement: O(n), where n is the number of elements in the a... | [
{
"content": "Create a for loop for iterating over the elements of an array in JavaScript and calculate the sum of the elements. However, the code contains a logical error that causes the sum to be multiplied by 2 instead of added to.\n\nArray: [1, 2, 3, 4]\n\nTime Complexity Requirement: O(n), where n is the n... | [
{
"content": "Create a for loop for iterating over the elements of an array in JavaScript and calculate the sum of the elements. However, the code contains a logical error that causes the sum to be multiplied by 2 instead of added to.\n\nArray: [1, 2, 3, 4]\n\nTime Complexity Requirement: O(n), where n is the n... | 6 | 4 |
Write Javascript code to create a timeout to set a message after a random duration between 3 and 7 seconds. Additionally, the message should be displayed in a randomly selected font from a list of five pre-defined fonts. | [
{
"content": "Write Javascript code to create a timeout to set a message after a random duration between 3 and 7 seconds. Additionally, the message should be displayed in a randomly selected font from a list of five pre-defined fonts.",
"role": "user"
},
{
"content": "```javascriptconst a0_0x1d006d ... | [
{
"content": "Write Javascript code to create a timeout to set a message after a random duration between 3 and 7 seconds. Additionally, the message should be displayed in a randomly selected font from a list of five pre-defined fonts.",
"role": "user"
},
{
"content": "```javascript\nconst fonts = ['... | 6 | 4 |
Write a JavaScript program to compute the sum of all the given values. The program should also handle negative values and decimals in the input array. The final sum should be rounded to the nearest whole number.
Additionally, the program should ignore any non-numeric values in the input array and only consider the va... | [
{
"content": "Write a JavaScript program to compute the sum of all the given values. The program should also handle negative values and decimals in the input array. The final sum should be rounded to the nearest whole number. \n\nAdditionally, the program should ignore any non-numeric values in the input array ... | [
{
"content": "Write a JavaScript program to compute the sum of all the given values. The program should also handle negative values and decimals in the input array. The final sum should be rounded to the nearest whole number. \n\nAdditionally, the program should ignore any non-numeric values in the input array ... | 6 | 4 |
Use a switch statement to check if a number is between 1 and 10, and also determine if it is divisible by both 2 and 3. If the number is divisible by both 2 and 3, display "Even and Divisible by 3", if it is divisible only by 2, display "Even", if it is divisible only by 3, display "Divisible by 3", and if it is not di... | [
{
"content": "Use a switch statement to check if a number is between 1 and 10, and also determine if it is divisible by both 2 and 3. If the number is divisible by both 2 and 3, display \"Even and Divisible by 3\", if it is divisible only by 2, display \"Even\", if it is divisible only by 3, display \"Divisible... | [
{
"content": "Use a switch statement to check if a number is between 1 and 10, and also determine if it is divisible by both 2 and 3. If the number is divisible by both 2 and 3, display \"Even and Divisible by 3\", if it is divisible only by 2, display \"Even\", if it is divisible only by 3, display \"Divisible... | 7 | 1 |
Write a JavaScript code to create a function that checks if a given number is divisible by both 2 and 3, and also check if the sum of its digits is divisible by 5. | [
{
"content": "Write a JavaScript code to create a function that checks if a given number is divisible by both 2 and 3, and also check if the sum of its digits is divisible by 5.",
"role": "user"
},
{
"content": "Here's a JavaScript code that creates a function to check if a given number is divisible... | [
{
"content": "Write a JavaScript code to create a function that checks if a given number is divisible by both 2 and 3, and also check if the sum of its digits is divisible by 5.",
"role": "user"
},
{
"content": "Here's a JavaScript code that creates a function to check if a given number is divisible... | 7 | 1 |
Implement a function in Node.js that takes in a string as input and returns the number of unique words in the string. However, the function should not consider any words that are shorter than 3 characters in length as part of the unique word count. | [
{
"content": "Implement a function in Node.js that takes in a string as input and returns the number of unique words in the string. However, the function should not consider any words that are shorter than 3 characters in length as part of the unique word count.",
"role": "user"
},
{
"content": "Her... | [
{
"content": "Implement a function in Node.js that takes in a string as input and returns the number of unique words in the string. However, the function should not consider any words that are shorter than 3 characters in length as part of the unique word count.",
"role": "user"
},
{
"content": "Her... | 7 | 2 |
Explain the various attributes used in the following HTML code and how they contribute to creating a clickable link that dynamically changes its appearance when hovered over:
<a href="www.example.com" target="_blank" rel="nofollow" id="clickable-link" class="hover-effect">Click here</a>
Additionally, provide an expla... | [
{
"content": "Explain the various attributes used in the following HTML code and how they contribute to creating a clickable link that dynamically changes its appearance when hovered over:\n\n<a href=\"www.example.com\" target=\"_blank\" rel=\"nofollow\" id=\"clickable-link\" class=\"hover-effect\">Click here</... | [
{
"content": "Explain the various attributes used in the following HTML code and how they contribute to creating a clickable link that dynamically changes its appearance when hovered over:\n\n<a href=\"www.example.com\" target=\"_blank\" rel=\"nofollow\" id=\"clickable-link\" class=\"hover-effect\">Click here</... | 8 | 1 |
Analyze the given JavaScript code, and identify the logical operator being used.
Write a function called `checkAgeRange` that takes an argument `age` (a number) and returns a boolean value indicating whether the age is within the range of 18 to 65 (inclusive).
The function should not use the `&&` logical operator d... | [
{
"content": "Analyze the given JavaScript code, and identify the logical operator being used. \n\nWrite a function called `checkAgeRange` that takes an argument `age` (a number) and returns a boolean value indicating whether the age is within the range of 18 to 65 (inclusive). \n\nThe function should not use t... | [
{
"content": "Analyze the given JavaScript code, and identify the logical operator being used. \n\nWrite a function called `checkAgeRange` that takes an argument `age` (a number) and returns a boolean value indicating whether the age is within the range of 18 to 65 (inclusive). \n\nThe function should not use t... | 8 | 2 |
Suggest an alternate solution to the following JavaScript code.
Given an array of names (length <= 1000) and a maximum length of each name (<= 20), rewrite the above code to use a higher-order function instead of a for loop. The solution should filter out any names that contain special characters and only display name... | [
{
"content": "Suggest an alternate solution to the following JavaScript code.\n\nGiven an array of names (length <= 1000) and a maximum length of each name (<= 20), rewrite the above code to use a higher-order function instead of a for loop. The solution should filter out any names that contain special characte... | [
{
"content": "Suggest an alternate solution to the following JavaScript code.\n\nGiven an array of names (length <= 1000) and a maximum length of each name (<= 20), rewrite the above code to use a higher-order function instead of a for loop. The solution should filter out any names that contain special characte... | 7 | 1 |
How can we use the reduce() function in JavaScript to find the sum of an array of numbers, but only considering the numbers that are divisible by 3, greater than 10, and less than 100? | [
{
"content": "How can we use the reduce() function in JavaScript to find the sum of an array of numbers, but only considering the numbers that are divisible by 3, greater than 10, and less than 100?",
"role": "user"
},
{
"content": "To find the sum of an array of numbers that meet the given conditio... | [
{
"content": "How can we use the reduce() function in JavaScript to find the sum of an array of numbers, but only considering the numbers that are divisible by 3, greater than 10, and less than 100?",
"role": "user"
},
{
"content": "To find the sum of an array of numbers that meet the given conditio... | 9 | 1 |
Write a JavaScript program to replace a specific word in a string.
The given string is: "Welcome to my website!"
To replace the word “website” with “blog”, but only if it is preceded by the word "my" and followed by the word "!". Ignore case sensitivity.
For example, if the input string is "Welcome to my website!", t... | [
{
"content": "Write a JavaScript program to replace a specific word in a string.\nThe given string is: \"Welcome to my website!\"\n\nTo replace the word “website” with “blog”, but only if it is preceded by the word \"my\" and followed by the word \"!\". Ignore case sensitivity.\n\nFor example, if the input stri... | [
{
"content": "Write a JavaScript program to replace a specific word in a string.\nThe given string is: \"Welcome to my website!\"\n\nTo replace the word “website” with “blog”, but only if it is preceded by the word \"my\" and followed by the word \"!\". Ignore case sensitivity.\n\nFor example, if the input stri... | 7 | 1 |
Create a Redux reducer to handle a state named "counter" with an initial value of 0. Additionally, add functionality to increment and decrement the counter by a variable amount with separate action types.
The increment action should take in a payload that specifies the amount by which the counter should be incremented... | [
{
"content": "Create a Redux reducer to handle a state named \"counter\" with an initial value of 0. Additionally, add functionality to increment and decrement the counter by a variable amount with separate action types.\n\nThe increment action should take in a payload that specifies the amount by which the cou... | [
{
"content": "Create a Redux reducer to handle a state named \"counter\" with an initial value of 0. Additionally, add functionality to increment and decrement the counter by a variable amount with separate action types.\n\nThe increment action should take in a payload that specifies the amount by which the cou... | 7 | 4 |
Using the comment as a guide, write code in JavaScript to generate a random number between 1 and 100, but ensure that the generated number is divisible by both 7 and 3, and is not divisible by 5.
// TODO | [
{
"content": "Using the comment as a guide, write code in JavaScript to generate a random number between 1 and 100, but ensure that the generated number is divisible by both 7 and 3, and is not divisible by 5.\n// TODO",
"role": "user"
},
{
"content": "```javascriptfunction a0_0x3e4e(_0x13e042, _0x3... | [
{
"content": "Using the comment as a guide, write code in JavaScript to generate a random number between 1 and 100, but ensure that the generated number is divisible by both 7 and 3, and is not divisible by 5.\n// TODO",
"role": "user"
},
{
"content": "```javascript\nfunction generateRandomNumber() ... | 6 | 3 |
Create a JavaScript object with three properties, `firstName`, `birthYear`, and `gender`, and two methods `sayHello`, which logs "Hello" to the console, and `calculateAge`, which calculates and returns the age based on the `birthYear`. The `firstName` property should only accept strings with a length between 3 and 20 c... | [
{
"content": "Create a JavaScript object with three properties, `firstName`, `birthYear`, and `gender`, and two methods `sayHello`, which logs \"Hello\" to the console, and `calculateAge`, which calculates and returns the age based on the `birthYear`. The `firstName` property should only accept strings with a l... | [
{
"content": "Create a JavaScript object with three properties, `firstName`, `birthYear`, and `gender`, and two methods `sayHello`, which logs \"Hello\" to the console, and `calculateAge`, which calculates and returns the age based on the `birthYear`. The `firstName` property should only accept strings with a l... | 6 | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.