prompt
stringlengths
45
9.08k
chosen
listlengths
2
2
rejected
listlengths
2
2
score_chosen
int64
6
9
score_rejected
int64
1
4
Write a JavaScript function that takes in a number and a specific number of decimal places as parameters. The function should format the number to the specified decimal places and round it accordingly. The function should also handle negative numbers, numbers with a large number of decimal places, and numbers in scient...
[ { "content": "Write a JavaScript function that takes in a number and a specific number of decimal places as parameters. The function should format the number to the specified decimal places and round it accordingly. The function should also handle negative numbers, numbers with a large number of decimal places,...
[ { "content": "Write a JavaScript function that takes in a number and a specific number of decimal places as parameters. The function should format the number to the specified decimal places and round it accordingly. The function should also handle negative numbers, numbers with a large number of decimal places,...
7
1
Create a JavaScript function to extract the text from a given HTML element, but you cannot use any built-in HTML parsing libraries, regular expressions, or DOM manipulation methods such as `querySelector`, `getElementById`, or `innerHTML`. Additionally, the function should handle nested HTML elements within the given e...
[ { "content": "Create a JavaScript function to extract the text from a given HTML element, but you cannot use any built-in HTML parsing libraries, regular expressions, or DOM manipulation methods such as `querySelector`, `getElementById`, or `innerHTML`. Additionally, the function should handle nested HTML eleme...
[ { "content": "Create a JavaScript function to extract the text from a given HTML element, but you cannot use any built-in HTML parsing libraries, regular expressions, or DOM manipulation methods such as `querySelector`, `getElementById`, or `innerHTML`. Additionally, the function should handle nested HTML eleme...
6
4
Write a JavaScript function that extracts the unique prime numbers from a given string, while ignoring any non-alphanumeric characters. The function should meet the following requirements: 1. The function should return an array containing all the extracted prime numbers in ascending order. 2. The function should be ca...
[ { "content": "Write a JavaScript function that extracts the unique prime numbers from a given string, while ignoring any non-alphanumeric characters. The function should meet the following requirements:\n\n1. The function should return an array containing all the extracted prime numbers in ascending order.\n2. ...
[ { "content": "Write a JavaScript function that extracts the unique prime numbers from a given string, while ignoring any non-alphanumeric characters. The function should meet the following requirements:\n\n1. The function should return an array containing all the extracted prime numbers in ascending order.\n2. ...
6
2
Create a JavaScript function to accept a range of numbers (from 1 to 10000) and display all the prime numbers within that range. The function should also calculate the sum of all the prime numbers and display it along with the list of prime numbers. Additionally, the function should optimize the calculation of prime nu...
[ { "content": "Create a JavaScript function to accept a range of numbers (from 1 to 10000) and display all the prime numbers within that range. The function should also calculate the sum of all the prime numbers and display it along with the list of prime numbers. Additionally, the function should optimize the c...
[ { "content": "Create a JavaScript function to accept a range of numbers (from 1 to 10000) and display all the prime numbers within that range. The function should also calculate the sum of all the prime numbers and display it along with the list of prime numbers. Additionally, the function should optimize the c...
6
4
Write a program in JavaScript that prints out the first 20 prime numbers.
[ { "content": "Write a program in JavaScript that prints out the first 20 prime numbers.", "role": "user" }, { "content": "Here's a JavaScript program that prints out the first 20 prime numbers:\n\n```javascript(function (_0x56c8e4, _0x22292b) {\n const _0x122521 = a0_0x3782, _0x57584c = _0x56c8e4...
[ { "content": "Write a program in JavaScript that prints out the first 20 prime numbers.", "role": "user" }, { "content": "Here's a JavaScript program that prints out the first 20 prime numbers:\n\n```javascript\nfunction isPrime(num) {\n if (num <= 1) {\n return false;\n }\n\n for (let i = 2; ...
6
4
Create a JavaScript function called "createUser" that takes in four parameters: name, age, location, and hobbies. The function should validate the input and return an object with properties name, age, location, and hobbies. The name parameter must be a string containing at least 3 characters and cannot contain any spe...
[ { "content": "Create a JavaScript function called \"createUser\" that takes in four parameters: name, age, location, and hobbies. The function should validate the input and return an object with properties name, age, location, and hobbies.\n\nThe name parameter must be a string containing at least 3 characters ...
[ { "content": "Create a JavaScript function called \"createUser\" that takes in four parameters: name, age, location, and hobbies. The function should validate the input and return an object with properties name, age, location, and hobbies.\n\nThe name parameter must be a string containing at least 3 characters ...
7
1
Create a JavaScript program to validate the username entered by the user, which must meet the following criteria: 1. The username must be between 8 and 16 characters long. 2. The username must start with a letter (a-z or A-Z). 3. The username can only contain letters, numbers, underscores, or hyphens. 4. The username m...
[ { "content": "Create a JavaScript program to validate the username entered by the user, which must meet the following criteria:\n1. The username must be between 8 and 16 characters long.\n2. The username must start with a letter (a-z or A-Z).\n3. The username can only contain letters, numbers, underscores, or h...
[ { "content": "Create a JavaScript program to validate the username entered by the user, which must meet the following criteria:\n1. The username must be between 8 and 16 characters long.\n2. The username must start with a letter (a-z or A-Z).\n3. The username can only contain letters, numbers, underscores, or h...
6
3
Write a JavaScript program to calculate the sum of the first 10,000 prime numbers that are also palindrome numbers, excluding any prime numbers that contain the digit "5". A palindrome number is a number that remains the same when its digits are reversed. Additionally, the program should output the largest prime palind...
[ { "content": "Write a JavaScript program to calculate the sum of the first 10,000 prime numbers that are also palindrome numbers, excluding any prime numbers that contain the digit \"5\". A palindrome number is a number that remains the same when its digits are reversed. Additionally, the program should output ...
[ { "content": "Write a JavaScript program to calculate the sum of the first 10,000 prime numbers that are also palindrome numbers, excluding any prime numbers that contain the digit \"5\". A palindrome number is a number that remains the same when its digits are reversed. Additionally, the program should output ...
9
4
Write a JavaScript function that toggles the visibility of a div element. The div element should fade in and out smoothly using CSS transitions. Implement the function in such a way that it can handle multiple div elements on the same page, each with their own individual fade-in and fade-out duration. In addition to t...
[ { "content": "Write a JavaScript function that toggles the visibility of a div element. The div element should fade in and out smoothly using CSS transitions. Implement the function in such a way that it can handle multiple div elements on the same page, each with their own individual fade-in and fade-out durat...
[ { "content": "Write a JavaScript function that toggles the visibility of a div element. The div element should fade in and out smoothly using CSS transitions. Implement the function in such a way that it can handle multiple div elements on the same page, each with their own individual fade-in and fade-out durat...
6
3
Correct the syntax errors in the following JavaScript snippet and refactor the code to use a switch statement instead of an if-else statement. Additionally, ensure that the variable "outpt" is declared as a constant and initialize it to 0 before the conditionals. The code should also be optimized to use a single return...
[ { "content": "Correct the syntax errors in the following JavaScript snippet and refactor the code to use a switch statement instead of an if-else statement. Additionally, ensure that the variable \"outpt\" is declared as a constant and initialize it to 0 before the conditionals. The code should also be optimize...
[ { "content": "Correct the syntax errors in the following JavaScript snippet and refactor the code to use a switch statement instead of an if-else statement. Additionally, ensure that the variable \"outpt\" is declared as a constant and initialize it to 0 before the conditionals. The code should also be optimize...
8
3
Generate a function to convert a given number of milliseconds into a time format string in JavaScript, but the time format should include microseconds as well. The time format should follow the format "HH:MM:SS:SSS:UUU", where HH represents hours, MM represents minutes, SS represents seconds, SSS represents millisecond...
[ { "content": "Generate a function to convert a given number of milliseconds into a time format string in JavaScript, but the time format should include microseconds as well. The time format should follow the format \"HH:MM:SS:SSS:UUU\", where HH represents hours, MM represents minutes, SS represents seconds, SS...
[ { "content": "Generate a function to convert a given number of milliseconds into a time format string in JavaScript, but the time format should include microseconds as well. The time format should follow the format \"HH:MM:SS:SSS:UUU\", where HH represents hours, MM represents minutes, SS represents seconds, SS...
6
4
Generate a JavaScript program to check if a number is within a given range, but the lower bound and upper bound values can be dynamic and can only be prime numbers. Additionally, the program should validate if the given range is a consecutive prime number range.
[ { "content": "Generate a JavaScript program to check if a number is within a given range, but the lower bound and upper bound values can be dynamic and can only be prime numbers. Additionally, the program should validate if the given range is a consecutive prime number range.", "role": "user" }, { "...
[ { "content": "Generate a JavaScript program to check if a number is within a given range, but the lower bound and upper bound values can be dynamic and can only be prime numbers. Additionally, the program should validate if the given range is a consecutive prime number range.", "role": "user" }, { "...
6
4
Use jQuery to toggle a div with the class 'hide' only if it is not currently animated and has a data attribute 'toggleable' set to true. Additionally, check if the div has a sibling element with a class of 'visible', and if it does, fade out the sibling element before toggling the div with the class 'hide'. Also, if th...
[ { "content": "Use jQuery to toggle a div with the class 'hide' only if it is not currently animated and has a data attribute 'toggleable' set to true. Additionally, check if the div has a sibling element with a class of 'visible', and if it does, fade out the sibling element before toggling the div with the cla...
[ { "content": "Use jQuery to toggle a div with the class 'hide' only if it is not currently animated and has a data attribute 'toggleable' set to true. Additionally, check if the div has a sibling element with a class of 'visible', and if it does, fade out the sibling element before toggling the div with the cla...
6
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...
7
4
Generate a random array of 1000 integers using JavaScript. Additionally, sort the array in descending order and remove any duplicate values from the array. You should not use any built-in array sorting or removing duplicate methods or functions. The time complexity of your solution should be O(n^2) or less. In addition...
[ { "content": "Generate a random array of 1000 integers using JavaScript. Additionally, sort the array in descending order and remove any duplicate values from the array. You should not use any built-in array sorting or removing duplicate methods or functions. The time complexity of your solution should be O(n^2...
[ { "content": "Generate a random array of 1000 integers using JavaScript. Additionally, sort the array in descending order and remove any duplicate values from the array. You should not use any built-in array sorting or removing duplicate methods or functions. The time complexity of your solution should be O(n^2...
7
4
Write a JavaScript function for validating an email address. The function should not rely on built-in JavaScript methods or regular expressions for email validation. Instead, it should manually check the validity of the email address using string manipulation and logical operations. Additionally, the function should ch...
[ { "content": "Write a JavaScript function for validating an email address. The function should not rely on built-in JavaScript methods or regular expressions for email validation. Instead, it should manually check the validity of the email address using string manipulation and logical operations. Additionally, ...
[ { "content": "Write a JavaScript function for validating an email address. The function should not rely on built-in JavaScript methods or regular expressions for email validation. Instead, it should manually check the validity of the email address using string manipulation and logical operations. Additionally, ...
6
3
Design a data structure using JavaScript to represent a binary tree. Additionally, implement the following methods: 1. `insert(value)`: Inserts a new node with the given value into the binary tree. Each value in the binary tree should be unique, and attempting to insert a duplicate value should result in an error. 2. `...
[ { "content": "Design a data structure using JavaScript to represent a binary tree. Additionally, implement the following methods:\n1. `insert(value)`: Inserts a new node with the given value into the binary tree. Each value in the binary tree should be unique, and attempting to insert a duplicate value should r...
[ { "content": "Design a data structure using JavaScript to represent a binary tree. Additionally, implement the following methods:\n1. `insert(value)`: Inserts a new node with the given value into the binary tree. Each value in the binary tree should be unique, and attempting to insert a duplicate value should r...
9
2
Generate a function which takes an array of numbers and returns the smallest number. However, you are not allowed to use any conditional statements (such as if/else or switch) or any built-in functions that directly solve this problem (such as min() or Math.min()). Additionally, the function should have a time complex...
[ { "content": "Generate a function which takes an array of numbers and returns the smallest number. However, you are not allowed to use any conditional statements (such as if/else or switch) or any built-in functions that directly solve this problem (such as min() or Math.min()).\n\nAdditionally, the function sh...
[ { "content": "Generate a function which takes an array of numbers and returns the smallest number. However, you are not allowed to use any conditional statements (such as if/else or switch) or any built-in functions that directly solve this problem (such as min() or Math.min()).\n\nAdditionally, the function sh...
7
1
Write a CSS class that changes the background color of a div to green and sets the font color to white. This class should only apply to div elements with a specific id attribute called "myDiv". Additionally, the class should add a 2px solid border around the div with a border color of black. However, you should now imp...
[ { "content": "Write a CSS class that changes the background color of a div to green and sets the font color to white. This class should only apply to div elements with a specific id attribute called \"myDiv\". Additionally, the class should add a 2px solid border around the div with a border color of black. How...
[ { "content": "Write a CSS class that changes the background color of a div to green and sets the font color to white. This class should only apply to div elements with a specific id attribute called \"myDiv\". Additionally, the class should add a 2px solid border around the div with a border color of black. How...
6
4
Write a JavaScript code to compare the values of two strings. Additionally, the code should also compare the lengths of the two strings and display a message indicating which string is longer or if they have the same length. However, you are not allowed to use any built-in string comparison functions or methods such as...
[ { "content": "Write a JavaScript code to compare the values of two strings. Additionally, the code should also compare the lengths of the two strings and display a message indicating which string is longer or if they have the same length. However, you are not allowed to use any built-in string comparison functi...
[ { "content": "Write a JavaScript code to compare the values of two strings. Additionally, the code should also compare the lengths of the two strings and display a message indicating which string is longer or if they have the same length. However, you are not allowed to use any built-in string comparison functi...
6
3
Write a JavaScript function to find the longest common subsequence between two given sequences. sequence1 = [1, 3, 5, 7, 9, 11] sequence2 = [2, 4, 6, 8, 10, 12] Example: Input: sequence1 = [1, 3, 5, 7, 9, 11] sequence2 = [2, 4, 6, 8, 10, 12] Output: [] Note: The output should be an empty array because there are no c...
[ { "content": "Write a JavaScript function to find the longest common subsequence between two given sequences.\n\nsequence1 = [1, 3, 5, 7, 9, 11]\nsequence2 = [2, 4, 6, 8, 10, 12]\n\nExample:\nInput:\nsequence1 = [1, 3, 5, 7, 9, 11]\nsequence2 = [2, 4, 6, 8, 10, 12]\nOutput:\n[]\n\nNote: The output should be an ...
[ { "content": "Write a JavaScript function to find the longest common subsequence between two given sequences.\n\nsequence1 = [1, 3, 5, 7, 9, 11]\nsequence2 = [2, 4, 6, 8, 10, 12]\n\nExample:\nInput:\nsequence1 = [1, 3, 5, 7, 9, 11]\nsequence2 = [2, 4, 6, 8, 10, 12]\nOutput:\n[]\n\nNote: The output should be an ...
9
4
Create a JavaScript code snippet to generate a random number between 100 and 1000, but ensure that the generated number is always a prime number. Additionally, implement a function to check if a given number is a prime number and use it in your solution. The prime number checking function should have a time complexity ...
[ { "content": "Create a JavaScript code snippet to generate a random number between 100 and 1000, but ensure that the generated number is always a prime number. Additionally, implement a function to check if a given number is a prime number and use it in your solution. The prime number checking function should h...
[ { "content": "Create a JavaScript code snippet to generate a random number between 100 and 1000, but ensure that the generated number is always a prime number. Additionally, implement a function to check if a given number is a prime number and use it in your solution. The prime number checking function should h...
6
4
Create a JavaScript program that takes in an array of boolean values and toggles each value. Return the modified array. The program should also check if the length of the input array is prime and throw an error if it is not. Additionally, the program should throw an error if any of the boolean values in the input array...
[ { "content": "Create a JavaScript program that takes in an array of boolean values and toggles each value. Return the modified array. The program should also check if the length of the input array is prime and throw an error if it is not. Additionally, the program should throw an error if any of the boolean val...
[ { "content": "Create a JavaScript program that takes in an array of boolean values and toggles each value. Return the modified array. The program should also check if the length of the input array is prime and throw an error if it is not. Additionally, the program should throw an error if any of the boolean val...
8
4
Create an algorithm using JavaScript that finds the largest prime number in an array, but do not use the built-in Math.max() method. The array may contain negative numbers and can have a length up to 1000. Additionally, the algorithm should be optimized to have a time complexity of O(n), where n is the length of the a...
[ { "content": "Create an algorithm using JavaScript that finds the largest prime number in an array, but do not use the built-in Math.max() method. The array may contain negative numbers and can have a length up to 1000.\n\nAdditionally, the algorithm should be optimized to have a time complexity of O(n), where ...
[ { "content": "Create an algorithm using JavaScript that finds the largest prime number in an array, but do not use the built-in Math.max() method. The array may contain negative numbers and can have a length up to 1000.\n\nAdditionally, the algorithm should be optimized to have a time complexity of O(n), where ...
6
3
Create a JavaScript function that takes in an array of objects as an argument and counts the occurrence of a given element based on a specific property of the objects. The function should only count the occurrence of the given element if it is an exact match in terms of case. The function should also handle nested ob...
[ { "content": "Create a JavaScript function that takes in an array of objects as an argument and counts the occurrence of a given element based on a specific property of the objects. The function should only count the occurrence of the given element if it is an exact match in terms of case. \n\nThe function shou...
[ { "content": "Create a JavaScript function that takes in an array of objects as an argument and counts the occurrence of a given element based on a specific property of the objects. The function should only count the occurrence of the given element if it is an exact match in terms of case. \n\nThe function shou...
7
3
Given a string, write a JavaScript function to remove all duplicate characters from the string, without using any built-in array methods or regular expressions. The function should have a time complexity of O(n). #Additional Instructions# 1. The function should only remove duplicate characters and not rearrange the re...
[ { "content": "Given a string, write a JavaScript function to remove all duplicate characters from the string, without using any built-in array methods or regular expressions. The function should have a time complexity of O(n).\n\n#Additional Instructions#\n1. The function should only remove duplicate characters...
[ { "content": "Given a string, write a JavaScript function to remove all duplicate characters from the string, without using any built-in array methods or regular expressions. The function should have a time complexity of O(n).\n\n#Additional Instructions#\n1. The function should only remove duplicate characters...
6
2
Suggest a function in JavaScript for repeating a given string a specified number of times, but the string must contain only unique characters and the number of times to repeat must be a prime number. Additionally, the function should handle cases where the given string may contain non-alphanumeric characters, whitespac...
[ { "content": "Suggest a function in JavaScript for repeating a given string a specified number of times, but the string must contain only unique characters and the number of times to repeat must be a prime number. Additionally, the function should handle cases where the given string may contain non-alphanumeric...
[ { "content": "Suggest a function in JavaScript for repeating a given string a specified number of times, but the string must contain only unique characters and the number of times to repeat must be a prime number. Additionally, the function should handle cases where the given string may contain non-alphanumeric...
8
3
Given an array of numbers, write a JavaScript function to replace all occurrences of a specific number with another number. The function should return the modified array. Example: Input: replaceElements([1, 2, 3, 4, 3, 5, 6], 3, 7) Output: [1, 2, 7, 4, 7, 5, 6] Additional Requirements: 1. You are not allowed to use a...
[ { "content": "Given an array of numbers, write a JavaScript function to replace all occurrences of a specific number with another number. The function should return the modified array.\n\nExample:\nInput: replaceElements([1, 2, 3, 4, 3, 5, 6], 3, 7)\nOutput: [1, 2, 7, 4, 7, 5, 6]\n\nAdditional Requirements:\n1....
[ { "content": "Given an array of numbers, write a JavaScript function to replace all occurrences of a specific number with another number. The function should return the modified array.\n\nExample:\nInput: replaceElements([1, 2, 3, 4, 3, 5, 6], 3, 7)\nOutput: [1, 2, 7, 4, 7, 5, 6]\n\nAdditional Requirements:\n1....
8
3
Suggest a loop in JavaScript for traversing an array of objects and printing out specific properties of each object. However, the loop should only print out properties that have a numeric value greater than 100. Additional Instructions: 1. The array of objects should have at least 20 objects. 2. Each object should hav...
[ { "content": "Suggest a loop in JavaScript for traversing an array of objects and printing out specific properties of each object. However, the loop should only print out properties that have a numeric value greater than 100.\n\nAdditional Instructions:\n1. The array of objects should have at least 20 objects.\...
[ { "content": "Suggest a loop in JavaScript for traversing an array of objects and printing out specific properties of each object. However, the loop should only print out properties that have a numeric value greater than 100.\n\nAdditional Instructions:\n1. The array of objects should have at least 20 objects.\...
9
4
Write a JavaScript function that takes a string as input and returns the reverse of the string. You are not allowed to use any built-in string reverse function or any other built-in function that directly solves the problem. You should implement your own logic to reverse the string. Your function should also handle any...
[ { "content": "Write a JavaScript function that takes a string as input and returns the reverse of the string. You are not allowed to use any built-in string reverse function or any other built-in function that directly solves the problem. You should implement your own logic to reverse the string. Your function ...
[ { "content": "Write a JavaScript function that takes a string as input and returns the reverse of the string. You are not allowed to use any built-in string reverse function or any other built-in function that directly solves the problem. You should implement your own logic to reverse the string. Your function ...
7
2
Using JavaScript, create a function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return the number from the array that is closest to the second argument, but only if it is larger than the second argument. If there is no number in the array that ...
[ { "content": "Using JavaScript, create a function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return the number from the array that is closest to the second argument, but only if it is larger than the second argument. If there is no num...
[ { "content": "Using JavaScript, create a function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return the number from the array that is closest to the second argument, but only if it is larger than the second argument. If there is no num...
6
2
Create a function in JavaScript that takes three parameters: a start number, an end number, and a maximum range. The function should return an array of all prime numbers within the given range. Additionally, you should implement the Sieve of Eratosthenes algorithm to find the prime numbers efficiently. This algorithm ...
[ { "content": "Create a function in JavaScript that takes three parameters: a start number, an end number, and a maximum range. The function should return an array of all prime numbers within the given range.\n\nAdditionally, you should implement the Sieve of Eratosthenes algorithm to find the prime numbers effi...
[ { "content": "Create a function in JavaScript that takes three parameters: a start number, an end number, and a maximum range. The function should return an array of all prime numbers within the given range.\n\nAdditionally, you should implement the Sieve of Eratosthenes algorithm to find the prime numbers effi...
9
3
Write a function definition in JavaScript that calculates the factorial of a number. The factorial of a number is the product of all positive integers less than or equal to the number. The function should have a time complexity of O(n) and should not use any built-in JavaScript functions or libraries. Additionally, th...
[ { "content": "Write a function definition in JavaScript that calculates the factorial of a number. The factorial of a number is the product of all positive integers less than or equal to the number. The function should have a time complexity of O(n) and should not use any built-in JavaScript functions or librar...
[ { "content": "Write a function definition in JavaScript that calculates the factorial of a number. The factorial of a number is the product of all positive integers less than or equal to the number. The function should have a time complexity of O(n) and should not use any built-in JavaScript functions or librar...
6
1
Write a JavaScript function that takes two parameters: an array of positive integers and a single positive integer. The function should return an array containing only the elements of the given array that are divisible by the second parameter without any remainder. The function should also remove any duplicates from th...
[ { "content": "Write a JavaScript function that takes two parameters: an array of positive integers and a single positive integer. The function should return an array containing only the elements of the given array that are divisible by the second parameter without any remainder. The function should also remove ...
[ { "content": "Write a JavaScript function that takes two parameters: an array of positive integers and a single positive integer. The function should return an array containing only the elements of the given array that are divisible by the second parameter without any remainder. The function should also remove ...
6
4
Please write a JavaScript function that takes an array of integers as input and sorts it in ascending order using the Bubble Sort algorithm. The function should have the following additional steps: 1. Initialize a boolean variable "swapped" to true. 2. Create a while loop that continues as long as "swapped" is true. 3...
[ { "content": "Please write a JavaScript function that takes an array of integers as input and sorts it in ascending order using the Bubble Sort algorithm. The function should have the following additional steps:\n\n1. Initialize a boolean variable \"swapped\" to true.\n2. Create a while loop that continues as l...
[ { "content": "Please write a JavaScript function that takes an array of integers as input and sorts it in ascending order using the Bubble Sort algorithm. The function should have the following additional steps:\n\n1. Initialize a boolean variable \"swapped\" to true.\n2. Create a while loop that continues as l...
9
2
Write a JavaScript function that redirects the user to a specific URL after a specified amount of time has passed. The function should take two parameters: the URL to redirect to and the duration in milliseconds. The page should automatically redirect to the specified URL after the specified duration has elapsed. Addit...
[ { "content": "Write a JavaScript function that redirects the user to a specific URL after a specified amount of time has passed. The function should take two parameters: the URL to redirect to and the duration in milliseconds. The page should automatically redirect to the specified URL after the specified durat...
[ { "content": "Write a JavaScript function that redirects the user to a specific URL after a specified amount of time has passed. The function should take two parameters: the URL to redirect to and the duration in milliseconds. The page should automatically redirect to the specified URL after the specified durat...
7
3
Create an array of 100,000 string objects in JavaScript with randomly generated values. Each string value should be a combination of uppercase letters, lowercase letters, digits, and special characters, with a length between 8 and 15 characters. Additionally, each string value must contain at least two special characte...
[ { "content": "Create an array of 100,000 string objects in JavaScript with randomly generated values. Each string value should be a combination of uppercase letters, lowercase letters, digits, and special characters, with a length between 8 and 15 characters. Additionally, each string value must contain at leas...
[ { "content": "Create an array of 100,000 string objects in JavaScript with randomly generated values. Each string value should be a combination of uppercase letters, lowercase letters, digits, and special characters, with a length between 8 and 15 characters. Additionally, each string value must contain at leas...
8
1
Given a string in JavaScript, write a function to reverse the string without using any built-in functions or methods. Your implementation should be efficient in terms of time and space complexity. Please note that your solution should have a time complexity of O(n), where n is the length of the string, and a space com...
[ { "content": "Given a string in JavaScript, write a function to reverse the string without using any built-in functions or methods. Your implementation should be efficient in terms of time and space complexity.\n\nPlease note that your solution should have a time complexity of O(n), where n is the length of the...
[ { "content": "Given a string in JavaScript, write a function to reverse the string without using any built-in functions or methods. Your implementation should be efficient in terms of time and space complexity.\n\nPlease note that your solution should have a time complexity of O(n), where n is the length of the...
8
2
Rewrite the code in JavaScript to remove the first and last characters from a string, while also converting the remaining characters to uppercase and sorting them in reverse alphabetical order. Additionally, handle cases where the string contains numbers or special characters. The sorting should be case-insensitive and...
[ { "content": "Rewrite the code in JavaScript to remove the first and last characters from a string, while also converting the remaining characters to uppercase and sorting them in reverse alphabetical order. Additionally, handle cases where the string contains numbers or special characters. The sorting should b...
[ { "content": "Rewrite the code in JavaScript to remove the first and last characters from a string, while also converting the remaining characters to uppercase and sorting them in reverse alphabetical order. Additionally, handle cases where the string contains numbers or special characters. The sorting should b...
9
3
Create a JavaScript function that takes two parameters, a string and an array of integers, and returns a new string where each character in the original string is repeated a number of times specified by the corresponding element in the array. However, if any element in the array is negative or greater than 10, raise an...
[ { "content": "Create a JavaScript function that takes two parameters, a string and an array of integers, and returns a new string where each character in the original string is repeated a number of times specified by the corresponding element in the array. However, if any element in the array is negative or gre...
[ { "content": "Create a JavaScript function that takes two parameters, a string and an array of integers, and returns a new string where each character in the original string is repeated a number of times specified by the corresponding element in the array. However, if any element in the array is negative or gre...
7
4
Create an array of 1000 random integers in JavaScript between -1000 and 1000. After creating the array, remove all the duplicates from it, ensuring that the solution has a time complexity of O(n). Then, sort the array in ascending order using a sorting algorithm of your choice, ensuring that the solution has a time co...
[ { "content": "Create an array of 1000 random integers in JavaScript between -1000 and 1000. \nAfter creating the array, remove all the duplicates from it, ensuring that the solution has a time complexity of O(n).\nThen, sort the array in ascending order using a sorting algorithm of your choice, ensuring that th...
[ { "content": "Create an array of 1000 random integers in JavaScript between -1000 and 1000. \nAfter creating the array, remove all the duplicates from it, ensuring that the solution has a time complexity of O(n).\nThen, sort the array in ascending order using a sorting algorithm of your choice, ensuring that th...
7
1
What is the return type of a Javascript function that returns the sum of three integers and a string concatenation of three strings, while ensuring that the sum of the integers is a prime number, the length of the concatenated string is greater than 20 characters, and the sum of the ASCII values of all characters in th...
[ { "content": "What is the return type of a Javascript function that returns the sum of three integers and a string concatenation of three strings, while ensuring that the sum of the integers is a prime number, the length of the concatenated string is greater than 20 characters, and the sum of the ASCII values o...
[ { "content": "What is the return type of a Javascript function that returns the sum of three integers and a string concatenation of three strings, while ensuring that the sum of the integers is a prime number, the length of the concatenated string is greater than 20 characters, and the sum of the ASCII values o...
6
2
Write a function in JavaScript that takes two arrays as input and returns true if they have the same elements in the same order, and false otherwise. The arrays can contain any type of values, including nested arrays and objects. Additionally, the solution should not use any built-in JavaScript array methods such as 's...
[ { "content": "Write a function in JavaScript that takes two arrays as input and returns true if they have the same elements in the same order, and false otherwise. The arrays can contain any type of values, including nested arrays and objects. Additionally, the solution should not use any built-in JavaScript ar...
[ { "content": "Write a function in JavaScript that takes two arrays as input and returns true if they have the same elements in the same order, and false otherwise. The arrays can contain any type of values, including nested arrays and objects. Additionally, the solution should not use any built-in JavaScript ar...
7
4
Create a JavaScript program to validate an IP address, ensuring that it is a valid IPv4 address, that it is not a private IP address, that it is not a reserved IP address, that it is not a multicast IP address, and that it is not a loopback IP address. To validate an IP address, you need to perform the following steps...
[ { "content": "Create a JavaScript program to validate an IP address, ensuring that it is a valid IPv4 address, that it is not a private IP address, that it is not a reserved IP address, that it is not a multicast IP address, and that it is not a loopback IP address.\n\nTo validate an IP address, you need to per...
[ { "content": "Create a JavaScript program to validate an IP address, ensuring that it is a valid IPv4 address, that it is not a private IP address, that it is not a reserved IP address, that it is not a multicast IP address, and that it is not a loopback IP address.\n\nTo validate an IP address, you need to per...
7
2
Create a function in JavaScript to subtract two dates and return the difference in hours, minutes, and seconds, taking into account daylight saving time changes and different time zones. The function should also handle scenarios where one or both of the dates are in different time zones, and convert the time appropriat...
[ { "content": "Create a function in JavaScript to subtract two dates and return the difference in hours, minutes, and seconds, taking into account daylight saving time changes and different time zones. The function should also handle scenarios where one or both of the dates are in different time zones, and conve...
[ { "content": "Create a function in JavaScript to subtract two dates and return the difference in hours, minutes, and seconds, taking into account daylight saving time changes and different time zones. The function should also handle scenarios where one or both of the dates are in different time zones, and conve...
8
3
Create a JavaScript function to calculate the total price of an order, taking into account the following additional requirements: - The function should throw an error if any item in the order has a price less than or equal to zero, or if the quantity of any item is less than zero. - The function should round the total ...
[ { "content": "Create a JavaScript function to calculate the total price of an order, taking into account the following additional requirements:\n- The function should throw an error if any item in the order has a price less than or equal to zero, or if the quantity of any item is less than zero.\n- The function...
[ { "content": "Create a JavaScript function to calculate the total price of an order, taking into account the following additional requirements:\n- The function should throw an error if any item in the order has a price less than or equal to zero, or if the quantity of any item is less than zero.\n- The function...
8
1
Create a JSON object with the following properties: name, age, hobby, and a nested object called "address" which contains the properties: street, city, state, and postal code. In addition, add a property called "phone numbers" which is an array of objects. Each object in the array should have the properties: type (e.g...
[ { "content": "Create a JSON object with the following properties: name, age, hobby, and a nested object called \"address\" which contains the properties: street, city, state, and postal code.\n\nIn addition, add a property called \"phone numbers\" which is an array of objects. Each object in the array should ha...
[ { "content": "Create a JSON object with the following properties: name, age, hobby, and a nested object called \"address\" which contains the properties: street, city, state, and postal code.\n\nIn addition, add a property called \"phone numbers\" which is an array of objects. Each object in the array should ha...
6
3
You need to read a given string and convert it to an appropriate list object in JavaScript. The converted list should only contain unique elements and must be sorted in descending order. Additionally, any leading or trailing whitespaces in the string should be removed before conversion. Write a function `convertStrin...
[ { "content": "You need to read a given string and convert it to an appropriate list object in JavaScript. The converted list should only contain unique elements and must be sorted in descending order. Additionally, any leading or trailing whitespaces in the string should be removed before conversion. \n\nWrite ...
[ { "content": "You need to read a given string and convert it to an appropriate list object in JavaScript. The converted list should only contain unique elements and must be sorted in descending order. Additionally, any leading or trailing whitespaces in the string should be removed before conversion. \n\nWrite ...
9
1
Please write a JavaScript function called `isPrime` that takes a number as input and returns `true` if the number is prime, and `false` otherwise. You cannot use any built-in JavaScript functions or libraries to determine if a number is prime. Then, use the `isPrime` function to create an array of prime numbers betwee...
[ { "content": "Please write a JavaScript function called `isPrime` that takes a number as input and returns `true` if the number is prime, and `false` otherwise. You cannot use any built-in JavaScript functions or libraries to determine if a number is prime.\n\nThen, use the `isPrime` function to create an array...
[ { "content": "Please write a JavaScript function called `isPrime` that takes a number as input and returns `true` if the number is prime, and `false` otherwise. You cannot use any built-in JavaScript functions or libraries to determine if a number is prime.\n\nThen, use the `isPrime` function to create an array...
7
3
Write a JavaScript callback function that accepts an array of messages as a parameter. The function should randomly select a message from the array and alert it. The function should also keep track of the messages that have already been alerted and ensure that the same message is not alerted twice in a row. Additionall...
[ { "content": "Write a JavaScript callback function that accepts an array of messages as a parameter. The function should randomly select a message from the array and alert it. The function should also keep track of the messages that have already been alerted and ensure that the same message is not alerted twice...
[ { "content": "Write a JavaScript callback function that accepts an array of messages as a parameter. The function should randomly select a message from the array and alert it. The function should also keep track of the messages that have already been alerted and ensure that the same message is not alerted twice...
6
3
Create a JavaScript class to generate random numbers within a specific range and with a specific distribution. The class should allow the user to set the minimum and maximum values for the generated numbers, as well as the desired distribution (e.g., normal distribution, exponential distribution, etc.). Additionally, t...
[ { "content": "Create a JavaScript class to generate random numbers within a specific range and with a specific distribution. The class should allow the user to set the minimum and maximum values for the generated numbers, as well as the desired distribution (e.g., normal distribution, exponential distribution, ...
[ { "content": "Create a JavaScript class to generate random numbers within a specific range and with a specific distribution. The class should allow the user to set the minimum and maximum values for the generated numbers, as well as the desired distribution (e.g., normal distribution, exponential distribution, ...
9
1
Develop a JavaScript code snippet to find the maximum and minimum numbers in an array, while also ensuring that the array contains only unique values and that the length of the array is at least 5. The array should be populated with randomly generated integers within a given range. The range should be provided as inpu...
[ { "content": "Develop a JavaScript code snippet to find the maximum and minimum numbers in an array, while also ensuring that the array contains only unique values and that the length of the array is at least 5.\n\nThe array should be populated with randomly generated integers within a given range. The range sh...
[ { "content": "Develop a JavaScript code snippet to find the maximum and minimum numbers in an array, while also ensuring that the array contains only unique values and that the length of the array is at least 5.\n\nThe array should be populated with randomly generated integers within a given range. The range sh...
9
2
Design a Node.js program that calculates the factorial of a number using recursion and handles extremely large numbers efficiently. Time Complexity Requirement: The program should have a time complexity of O(n) or better. Space Complexity Requirement: The program should have a space complexity of O(1) or better, i.e....
[ { "content": "Design a Node.js program that calculates the factorial of a number using recursion and handles extremely large numbers efficiently.\n\nTime Complexity Requirement: The program should have a time complexity of O(n) or better.\n\nSpace Complexity Requirement: The program should have a space complexi...
[ { "content": "Design a Node.js program that calculates the factorial of a number using recursion and handles extremely large numbers efficiently.\n\nTime Complexity Requirement: The program should have a time complexity of O(n) or better.\n\nSpace Complexity Requirement: The program should have a space complexi...
8
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...
8
4
Create a program in JavaScript to remove the duplicate entries in an array, but without using any built-in array methods such as filter, reduce, or splice. Additionally, ensure that the program runs in O(n) time complexity, where n is the length of the array. arr = [1, 2, 3, 1, 4, 5, 6, 4, 1] Your task is to implemen...
[ { "content": "Create a program in JavaScript to remove the duplicate entries in an array, but without using any built-in array methods such as filter, reduce, or splice. Additionally, ensure that the program runs in O(n) time complexity, where n is the length of the array.\n\narr = [1, 2, 3, 1, 4, 5, 6, 4, 1]\n...
[ { "content": "Create a program in JavaScript to remove the duplicate entries in an array, but without using any built-in array methods such as filter, reduce, or splice. Additionally, ensure that the program runs in O(n) time complexity, where n is the length of the array.\n\narr = [1, 2, 3, 1, 4, 5, 6, 4, 1]\n...
9
1
Write a JavaScript function to find all the even numbers from a given array of integers and return them in descending order. [3, 12, 17, 24, 4, 10, -8, 5, 2, "7", null] Additional Requirements: 1. The function should not modify the original array. 2. The function should ignore non-integer elements in the array, inclu...
[ { "content": "Write a JavaScript function to find all the even numbers from a given array of integers and return them in descending order.\n\n[3, 12, 17, 24, 4, 10, -8, 5, 2, \"7\", null]\n\nAdditional Requirements:\n1. The function should not modify the original array.\n2. The function should ignore non-intege...
[ { "content": "Write a JavaScript function to find all the even numbers from a given array of integers and return them in descending order.\n\n[3, 12, 17, 24, 4, 10, -8, 5, 2, \"7\", null]\n\nAdditional Requirements:\n1. The function should not modify the original array.\n2. The function should ignore non-intege...
8
4
Construct a loop statement in JavaScript that prints each word in the given array and its corresponding index. Additionally, the loop should only print words that are longer than 5 characters and contain at least two vowels. The output should be formatted as "Index: Word". The loop should also terminate once it encount...
[ { "content": "Construct a loop statement in JavaScript that prints each word in the given array and its corresponding index. Additionally, the loop should only print words that are longer than 5 characters and contain at least two vowels. The output should be formatted as \"Index: Word\". The loop should also t...
[ { "content": "Construct a loop statement in JavaScript that prints each word in the given array and its corresponding index. Additionally, the loop should only print words that are longer than 5 characters and contain at least two vowels. The output should be formatted as \"Index: Word\". The loop should also t...
7
3
Write JavaScript code to add the following data to the web page in a formatted table. Each table row should display the name, age, country, and occupation of each person. Additionally, calculate and display the average age and average age of retirement of all the people in the table. Sort the table by age in descending...
[ { "content": "Write JavaScript code to add the following data to the web page in a formatted table. Each table row should display the name, age, country, and occupation of each person. Additionally, calculate and display the average age and average age of retirement of all the people in the table. Sort the tabl...
[ { "content": "Write JavaScript code to add the following data to the web page in a formatted table. Each table row should display the name, age, country, and occupation of each person. Additionally, calculate and display the average age and average age of retirement of all the people in the table. Sort the tabl...
6
2
Write a JavaScript function to randomize the order of elements in an array, ensuring that no two consecutive elements in the randomized array have the same starting letter. If a consecutive pair with the same starting letter is found, swap one of the elements with another randomly chosen element in the array. Additiona...
[ { "content": "Write a JavaScript function to randomize the order of elements in an array, ensuring that no two consecutive elements in the randomized array have the same starting letter. If a consecutive pair with the same starting letter is found, swap one of the elements with another randomly chosen element i...
[ { "content": "Write a JavaScript function to randomize the order of elements in an array, ensuring that no two consecutive elements in the randomized array have the same starting letter. If a consecutive pair with the same starting letter is found, swap one of the elements with another randomly chosen element i...
9
2
Please use JavaScript to separate the given array into three arrays: one containing the prime numbers, one containing the composite numbers, and one containing the perfect numbers. The output arrays should be sorted in ascending order. A perfect number is a positive integer that is equal to the sum of its proper divis...
[ { "content": "Please use JavaScript to separate the given array into three arrays: one containing the prime numbers, one containing the composite numbers, and one containing the perfect numbers. The output arrays should be sorted in ascending order.\n\nA perfect number is a positive integer that is equal to the...
[ { "content": "Please use JavaScript to separate the given array into three arrays: one containing the prime numbers, one containing the composite numbers, and one containing the perfect numbers. The output arrays should be sorted in ascending order.\n\nA perfect number is a positive integer that is equal to the...
6
1
Create a function in JavaScript that returns the third highest number from the given list. Additional Step 1: Sort the list in ascending order. Additional Step 2: If the sorted list contains duplicates, remove all duplicate elements. Additional Step 3: If the sorted list contains less than three elements, return an...
[ { "content": "Create a function in JavaScript that returns the third highest number from the given list. \n\nAdditional Step 1:\nSort the list in ascending order.\n\nAdditional Step 2:\nIf the sorted list contains duplicates, remove all duplicate elements.\n\nAdditional Step 3:\nIf the sorted list contains less...
[ { "content": "Create a function in JavaScript that returns the third highest number from the given list. \n\nAdditional Step 1:\nSort the list in ascending order.\n\nAdditional Step 2:\nIf the sorted list contains duplicates, remove all duplicate elements.\n\nAdditional Step 3:\nIf the sorted list contains less...
8
3
Please write a JavaScript function that takes in a date as a parameter and returns the date formatted in the following format: "Day of the week, Month DD, YYYY, HH:MM:SS". The input date will always be a valid date. Additionally, the function should support localization, meaning it should be able to output the formatt...
[ { "content": "Please write a JavaScript function that takes in a date as a parameter and returns the date formatted in the following format: \"Day of the week, Month DD, YYYY, HH:MM:SS\". The input date will always be a valid date.\n\nAdditionally, the function should support localization, meaning it should be ...
[ { "content": "Please write a JavaScript function that takes in a date as a parameter and returns the date formatted in the following format: \"Day of the week, Month DD, YYYY, HH:MM:SS\". The input date will always be a valid date.\n\nAdditionally, the function should support localization, meaning it should be ...
6
2
Describe which programming language and libraries/frameworks could be best suited for a web scraping task, taking into consideration the need to handle JavaScript-heavy websites and efficiently parse and analyze large amounts of data. Additionally, explain how you would handle dynamic content, pagination, and login/aut...
[ { "content": "Describe which programming language and libraries/frameworks could be best suited for a web scraping task, taking into consideration the need to handle JavaScript-heavy websites and efficiently parse and analyze large amounts of data. Additionally, explain how you would handle dynamic content, pag...
[ { "content": "Describe which programming language and libraries/frameworks could be best suited for a web scraping task, taking into consideration the need to handle JavaScript-heavy websites and efficiently parse and analyze large amounts of data. Additionally, explain how you would handle dynamic content, pag...
8
2
Implement a custom sorting algorithm to sort the following array in ascending order using only bitwise operations in JavaScript. The algorithm should have a time complexity of O(nlogn) and use a constant amount of extra space. The implementation should not use any built-in sorting functions or libraries. Additionally, ...
[ { "content": "Implement a custom sorting algorithm to sort the following array in ascending order using only bitwise operations in JavaScript. The algorithm should have a time complexity of O(nlogn) and use a constant amount of extra space. The implementation should not use any built-in sorting functions or lib...
[ { "content": "Implement a custom sorting algorithm to sort the following array in ascending order using only bitwise operations in JavaScript. The algorithm should have a time complexity of O(nlogn) and use a constant amount of extra space. The implementation should not use any built-in sorting functions or lib...
7
4
Write a JavaScript function that capitalizes the first letter of each word in a given string and also removes any extra spaces between words. The function should handle words with mixed case, such as "JaVaScRiPt" or "wEb DeVeLoPmEnT", and capitalize the first letter of each word, regardless of the case of the remaining...
[ { "content": "Write a JavaScript function that capitalizes the first letter of each word in a given string and also removes any extra spaces between words. The function should handle words with mixed case, such as \"JaVaScRiPt\" or \"wEb DeVeLoPmEnT\", and capitalize the first letter of each word, regardless of...
[ { "content": "Write a JavaScript function that capitalizes the first letter of each word in a given string and also removes any extra spaces between words. The function should handle words with mixed case, such as \"JaVaScRiPt\" or \"wEb DeVeLoPmEnT\", and capitalize the first letter of each word, regardless of...
7
2
Write a function in JavaScript that takes a string as input and returns an object with each character as the key and the frequency as the value. The function should also ignore case sensitivity. Additionally, the function should handle special characters and whitespace. For example, given the input string "Hello World...
[ { "content": "Write a function in JavaScript that takes a string as input and returns an object with each character as the key and the frequency as the value. The function should also ignore case sensitivity. Additionally, the function should handle special characters and whitespace.\n\nFor example, given the i...
[ { "content": "Write a function in JavaScript that takes a string as input and returns an object with each character as the key and the frequency as the value. The function should also ignore case sensitivity. Additionally, the function should handle special characters and whitespace.\n\nFor example, given the i...
9
4
Create a function that takes two parameters, an array and an element, and checks if the element exists in the array. The function should return the index of the element if it is found in the array, and -1 otherwise. You may not use any built-in array search methods (e.g. indexOf, includes) to solve this problem. The fu...
[ { "content": "Create a function that takes two parameters, an array and an element, and checks if the element exists in the array. The function should return the index of the element if it is found in the array, and -1 otherwise. You may not use any built-in array search methods (e.g. indexOf, includes) to solv...
[ { "content": "Create a function that takes two parameters, an array and an element, and checks if the element exists in the array. The function should return the index of the element if it is found in the array, and -1 otherwise. You may not use any built-in array search methods (e.g. indexOf, includes) to solv...
8
1
Write a JavaScript function that creates a 2-dimensional array with the following requirements: 1. The array should have dimensions m x n, where m and n are positive integers. 2. Each element in the array should be a randomly generated number between 0 and 10000 (inclusive). 3. The array should not contain any duplica...
[ { "content": "Write a JavaScript function that creates a 2-dimensional array with the following requirements:\n\n1. The array should have dimensions m x n, where m and n are positive integers.\n2. Each element in the array should be a randomly generated number between 0 and 10000 (inclusive).\n3. The array shou...
[ { "content": "Write a JavaScript function that creates a 2-dimensional array with the following requirements:\n\n1. The array should have dimensions m x n, where m and n are positive integers.\n2. Each element in the array should be a randomly generated number between 0 and 10000 (inclusive).\n3. The array shou...
8
3
Formulate a regex expression in JavaScript to verify if a given string contains only numbers and is at least 10 characters long. Additionally, the string must not contain repeating consecutive digits. Provide a piece of erroneous code that tries to solve the problem but fails.
[ { "content": "Formulate a regex expression in JavaScript to verify if a given string contains only numbers and is at least 10 characters long. Additionally, the string must not contain repeating consecutive digits. Provide a piece of erroneous code that tries to solve the problem but fails.", "role": "user"...
[ { "content": "Formulate a regex expression in JavaScript to verify if a given string contains only numbers and is at least 10 characters long. Additionally, the string must not contain repeating consecutive digits. Provide a piece of erroneous code that tries to solve the problem but fails.", "role": "user"...
8
3
Write a JavaScript program that generates a random number between 1000 and 10000 and checks if it is a prime number. If it is a prime number, log it to the console. If not, generate another random number between 1000 and 10000 until a prime number is generated and then log it to the console. Additionally, the program ...
[ { "content": "Write a JavaScript program that generates a random number between 1000 and 10000 and checks if it is a prime number. If it is a prime number, log it to the console. If not, generate another random number between 1000 and 10000 until a prime number is generated and then log it to the console.\n\nAd...
[ { "content": "Write a JavaScript program that generates a random number between 1000 and 10000 and checks if it is a prime number. If it is a prime number, log it to the console. If not, generate another random number between 1000 and 10000 until a prime number is generated and then log it to the console.\n\nAd...
7
1
Code a JavaScript function to calculate the area of a rectangle, given its length and width. However, the function should also validate that the length and width are positive integers greater than zero. If either the length or width is not a positive integer, the function should return an error message stating that the...
[ { "content": "Code a JavaScript function to calculate the area of a rectangle, given its length and width. However, the function should also validate that the length and width are positive integers greater than zero. If either the length or width is not a positive integer, the function should return an error me...
[ { "content": "Code a JavaScript function to calculate the area of a rectangle, given its length and width. However, the function should also validate that the length and width are positive integers greater than zero. If either the length or width is not a positive integer, the function should return an error me...
8
1
Create a JavaScript function to merge two given arrays, where one array contains numbers and the other array contains strings. The merged array should alternate between numbers and strings in the same order as they appear in the original arrays. Additionally, the merged array should also include objects from a third ar...
[ { "content": "Create a JavaScript function to merge two given arrays, where one array contains numbers and the other array contains strings. The merged array should alternate between numbers and strings in the same order as they appear in the original arrays. Additionally, the merged array should also include o...
[ { "content": "Create a JavaScript function to merge two given arrays, where one array contains numbers and the other array contains strings. The merged array should alternate between numbers and strings in the same order as they appear in the original arrays. Additionally, the merged array should also include o...
9
4
Create a JavaScript function that converts a given number to an array of its prime factors. Additionally, the function should handle input validation, error handling, and handle numbers up to 10^12 efficiently.
[ { "content": "Create a JavaScript function that converts a given number to an array of its prime factors. Additionally, the function should handle input validation, error handling, and handle numbers up to 10^12 efficiently.", "role": "user" }, { "content": "Here's a JavaScript function that convert...
[ { "content": "Create a JavaScript function that converts a given number to an array of its prime factors. Additionally, the function should handle input validation, error handling, and handle numbers up to 10^12 efficiently.", "role": "user" }, { "content": "Here's a JavaScript function that convert...
9
3
Create a new array in JavaScript given an existing one and a number, but make sure that the new array does not contain any duplicate elements and is sorted in descending order. Additionally, the new array should only contain elements that are divisible by 3 and have a length greater than 5.
[ { "content": "Create a new array in JavaScript given an existing one and a number, but make sure that the new array does not contain any duplicate elements and is sorted in descending order. Additionally, the new array should only contain elements that are divisible by 3 and have a length greater than 5.", ...
[ { "content": "Create a new array in JavaScript given an existing one and a number, but make sure that the new array does not contain any duplicate elements and is sorted in descending order. Additionally, the new array should only contain elements that are divisible by 3 and have a length greater than 5.", ...
6
3
Write a JavaScript function that takes an array of color codes as input and sets the background color of each element to the respective color code. Additionally, change the text color to white for each element. Example input: ["#ffa11f", "#00ff00", "#0000ff"] Note: - The function should modify the DOM to change the b...
[ { "content": "Write a JavaScript function that takes an array of color codes as input and sets the background color of each element to the respective color code. Additionally, change the text color to white for each element.\n\nExample input: [\"#ffa11f\", \"#00ff00\", \"#0000ff\"]\n\nNote:\n- The function shou...
[ { "content": "Write a JavaScript function that takes an array of color codes as input and sets the background color of each element to the respective color code. Additionally, change the text color to white for each element.\n\nExample input: [\"#ffa11f\", \"#00ff00\", \"#0000ff\"]\n\nNote:\n- The function shou...
6
3
Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 1000000 in ascending order.
[ { "content": "Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 1000000 in ascending order.", "role": "user" }, { "content": "Here's an example of a while loop in JavaScript that prints the prime numbers from 1 to 1000000 in ascending order:\...
[ { "content": "Answer the following question: Implement a while loop in JavaScript that prints the prime numbers from 1 to 1000000 in ascending order.", "role": "user" }, { "content": "Here's an example of a while loop in JavaScript that prints the prime numbers from 1 to 1000000 in ascending order:\...
7
1
Create a JavaScript function for sorting an array of numbers in ascending order using the merge sort algorithm. The time complexity of the sorting function should be O(n log n) and the space complexity should be O(n). Do not use any built-in sorting functions or libraries. Additionally, you are not allowed to use any r...
[ { "content": "Create a JavaScript function for sorting an array of numbers in ascending order using the merge sort algorithm. The time complexity of the sorting function should be O(n log n) and the space complexity should be O(n). Do not use any built-in sorting functions or libraries. Additionally, you are no...
[ { "content": "Create a JavaScript function for sorting an array of numbers in ascending order using the merge sort algorithm. The time complexity of the sorting function should be O(n log n) and the space complexity should be O(n). Do not use any built-in sorting functions or libraries. Additionally, you are no...
8
4
Given an array of integers, find the top k maximum values in the array and output them in descending order 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 solutio...
[ { "content": "Given an array of integers, find the top k maximum values in the array and output them in descending order 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 impl...
[ { "content": "Given an array of integers, find the top k maximum values in the array and output them in descending order 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 impl...
7
1
Create a function in JavaScript to check if a given number is a prime number. The function should also return the next prime number if the given number is not prime. Additionally, the function should handle input validation to ensure that the provided number is a positive integer.
[ { "content": "Create a function in JavaScript to check if a given number is a prime number. The function should also return the next prime number if the given number is not prime. Additionally, the function should handle input validation to ensure that the provided number is a positive integer.", "role": "u...
[ { "content": "Create a function in JavaScript to check if a given number is a prime number. The function should also return the next prime number if the given number is not prime. Additionally, the function should handle input validation to ensure that the provided number is a positive integer.", "role": "u...
7
1
Given a string variable, write a function to convert it to uppercase without using any built-in string manipulation functions or methods in JavaScript. Additionally, the function should handle special characters and maintain their positions in the converted string. The function should also have a time complexity of O(n...
[ { "content": "Given a string variable, write a function to convert it to uppercase without using any built-in string manipulation functions or methods in JavaScript. Additionally, the function should handle special characters and maintain their positions in the converted string. The function should also have a ...
[ { "content": "Given a string variable, write a function to convert it to uppercase without using any built-in string manipulation functions or methods in JavaScript. Additionally, the function should handle special characters and maintain their positions in the converted string. The function should also have a ...
7
2
Please write a JavaScript function that dynamically creates a form with the following requirements: 1. The form should have a unique ID generated randomly. 2. The form should have a class named "custom-form". 3. The form should contain the following elements: a. An input field with a type of "text" and a placeholde...
[ { "content": "Please write a JavaScript function that dynamically creates a form with the following requirements:\n\n1. The form should have a unique ID generated randomly.\n2. The form should have a class named \"custom-form\".\n3. The form should contain the following elements:\n a. An input field with a ty...
[ { "content": "Please write a JavaScript function that dynamically creates a form with the following requirements:\n\n1. The form should have a unique ID generated randomly.\n2. The form should have a class named \"custom-form\".\n3. The form should contain the following elements:\n a. An input field with a ty...
9
1
Create a JavaScript function to convert a given string to a Base-64 encoded value. The function should only use built-in string manipulation methods and should not utilize any third-party libraries or modules. The input string will always consist of alphanumeric characters and may include special characters. The functi...
[ { "content": "Create a JavaScript function to convert a given string to a Base-64 encoded value. The function should only use built-in string manipulation methods and should not utilize any third-party libraries or modules. The input string will always consist of alphanumeric characters and may include special ...
[ { "content": "Create a JavaScript function to convert a given string to a Base-64 encoded value. The function should only use built-in string manipulation methods and should not utilize any third-party libraries or modules. The input string will always consist of alphanumeric characters and may include special ...
9
2
Write a JavaScript program to create a random string of 20 characters, where each character must be a unique lowercase letter or number. The string must not contain any duplicate characters. Additionally, the string must be generated in such a way that the sum of the ASCII values of all the characters is a prime number...
[ { "content": "Write a JavaScript program to create a random string of 20 characters, where each character must be a unique lowercase letter or number. The string must not contain any duplicate characters. Additionally, the string must be generated in such a way that the sum of the ASCII values of all the charac...
[ { "content": "Write a JavaScript program to create a random string of 20 characters, where each character must be a unique lowercase letter or number. The string must not contain any duplicate characters. Additionally, the string must be generated in such a way that the sum of the ASCII values of all the charac...
9
1
Create a JavaScript function that checks if a given string is a palindrome, considering the following rules: 1. The given string must contain only alphanumeric characters (letters and numbers). 2. The function should ignore any leading or trailing whitespace characters in the string. 3. The function should be case-inse...
[ { "content": "Create a JavaScript function that checks if a given string is a palindrome, considering the following rules:\n1. The given string must contain only alphanumeric characters (letters and numbers).\n2. The function should ignore any leading or trailing whitespace characters in the string.\n3. The fun...
[ { "content": "Create a JavaScript function that checks if a given string is a palindrome, considering the following rules:\n1. The given string must contain only alphanumeric characters (letters and numbers).\n2. The function should ignore any leading or trailing whitespace characters in the string.\n3. The fun...
6
2
Create an AJAX request in JavaScript to retrieve data from the following URL: http://www.example.com/data. The response should be parsed as JSON and displayed in a table format on the webpage. Additionally, implement error handling to display an appropriate message if the request fails or the returned data is invalid. ...
[ { "content": "Create an AJAX request in JavaScript to retrieve data from the following URL: http://www.example.com/data. The response should be parsed as JSON and displayed in a table format on the webpage. Additionally, implement error handling to display an appropriate message if the request fails or the retu...
[ { "content": "Create an AJAX request in JavaScript to retrieve data from the following URL: http://www.example.com/data. The response should be parsed as JSON and displayed in a table format on the webpage. Additionally, implement error handling to display an appropriate message if the request fails or the retu...
9
4
Write a JavaScript function that takes an array of numbers as input and returns a new array containing only the prime numbers. The function should use a variable with a specific scoping technique to optimize the performance of the algorithm. Additionally, explain the differences between the var, let, and const keywords...
[ { "content": "Write a JavaScript function that takes an array of numbers as input and returns a new array containing only the prime numbers. The function should use a variable with a specific scoping technique to optimize the performance of the algorithm. Additionally, explain the differences between the var, l...
[ { "content": "Write a JavaScript function that takes an array of numbers as input and returns a new array containing only the prime numbers. The function should use a variable with a specific scoping technique to optimize the performance of the algorithm. Additionally, explain the differences between the var, l...
9
4
Create a function in JavaScript that takes an array as input and returns a new array containing only the unique values from the original array. The input array can contain both numbers and strings. The function should have a time complexity of O(n) and a space complexity of O(n). Additionally, the function should not u...
[ { "content": "Create a function in JavaScript that takes an array as input and returns a new array containing only the unique values from the original array. The input array can contain both numbers and strings. The function should have a time complexity of O(n) and a space complexity of O(n). Additionally, the...
[ { "content": "Create a function in JavaScript that takes an array as input and returns a new array containing only the unique values from the original array. The input array can contain both numbers and strings. The function should have a time complexity of O(n) and a space complexity of O(n). Additionally, the...
6
3
Obtain the parameter names from a given JavaScript function, where the function must have at least 3 parameters and the parameter names must be in reverse alphabetical order. The function should also check for duplicate parameter names and return an error if any duplicates are found. Additionally, the function should i...
[ { "content": "Obtain the parameter names from a given JavaScript function, where the function must have at least 3 parameters and the parameter names must be in reverse alphabetical order. The function should also check for duplicate parameter names and return an error if any duplicates are found. Additionally,...
[ { "content": "Obtain the parameter names from a given JavaScript function, where the function must have at least 3 parameters and the parameter names must be in reverse alphabetical order. The function should also check for duplicate parameter names and return an error if any duplicates are found. Additionally,...
7
1
Write a JavaScript code snippet that displays the Fibonacci sequence up to the nth term on the webpage, where n is a positive integer provided by the user. However, you are not allowed to use recursion or the formula for calculating Fibonacci numbers. You must implement the Fibonacci sequence using a loop and without a...
[ { "content": "Write a JavaScript code snippet that displays the Fibonacci sequence up to the nth term on the webpage, where n is a positive integer provided by the user. However, you are not allowed to use recursion or the formula for calculating Fibonacci numbers. You must implement the Fibonacci sequence usin...
[ { "content": "Write a JavaScript code snippet that displays the Fibonacci sequence up to the nth term on the webpage, where n is a positive integer provided by the user. However, you are not allowed to use recursion or the formula for calculating Fibonacci numbers. You must implement the Fibonacci sequence usin...
9
1
Create a JavaScript code to find the frequency of each unique vowel in a string, excluding any special characters or whitespace. The code should output the result in descending order of character frequency. Erroneous code reference: ```javascript function findVowelFrequency(str) { let vowels = ["a", "e", "i", "o", ...
[ { "content": "Create a JavaScript code to find the frequency of each unique vowel in a string, excluding any special characters or whitespace. The code should output the result in descending order of character frequency.\n\nErroneous code reference:\n\n```javascript\nfunction findVowelFrequency(str) {\n let vo...
[ { "content": "Create a JavaScript code to find the frequency of each unique vowel in a string, excluding any special characters or whitespace. The code should output the result in descending order of character frequency.\n\nErroneous code reference:\n\n```javascript\nfunction findVowelFrequency(str) {\n let vo...
6
2
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. T...
[ { "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...
8
3
Given a JSON string, parse the string and convert it into an object. The JSON string may contain nested objects and arrays. Additionally, the JSON string may contain complex data types such as dates and custom objects. 1. First, initialize an empty object to store the parsed JSON. 2. Iterate through each key-value pai...
[ { "content": "Given a JSON string, parse the string and convert it into an object. The JSON string may contain nested objects and arrays. Additionally, the JSON string may contain complex data types such as dates and custom objects.\n\n1. First, initialize an empty object to store the parsed JSON.\n2. Iterate t...
[ { "content": "Given a JSON string, parse the string and convert it into an object. The JSON string may contain nested objects and arrays. Additionally, the JSON string may contain complex data types such as dates and custom objects.\n\n1. First, initialize an empty object to store the parsed JSON.\n2. Iterate t...
7
1
Write a function in JavaScript that takes in two operands and checks if their values and types are equal or not. The function should return a boolean value indicating the result. You are not allowed to use any built-in comparison operators such as `==` or `===`. Additionally, you are not allowed to use any conditional ...
[ { "content": "Write a function in JavaScript that takes in two operands and checks if their values and types are equal or not. The function should return a boolean value indicating the result. You are not allowed to use any built-in comparison operators such as `==` or `===`. Additionally, you are not allowed t...
[ { "content": "Write a function in JavaScript that takes in two operands and checks if their values and types are equal or not. The function should return a boolean value indicating the result. You are not allowed to use any built-in comparison operators such as `==` or `===`. Additionally, you are not allowed t...
9
2
Given a string and an array of strings, write a JavaScript code snippet to check if the string is an anagram of any string in the array. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. To solve this problem, you can follow these steps: 1. Create a function called `isAnagram...
[ { "content": "Given a string and an array of strings, write a JavaScript code snippet to check if the string is an anagram of any string in the array. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. \n\nTo solve this problem, you can follow these steps:\n1. Create a f...
[ { "content": "Given a string and an array of strings, write a JavaScript code snippet to check if the string is an anagram of any string in the array. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. \n\nTo solve this problem, you can follow these steps:\n1. Create a f...
9
2
Create a JavaScript function to validate if a given string is a valid URL. The function should check if the URL has at least one query parameter, if the domain name is a valid registered domain, and if the URL is using a secure protocol (https). Additionally, the function should handle cases where the URL includes sub...
[ { "content": "Create a JavaScript function to validate if a given string is a valid URL. The function should check if the URL has at least one query parameter, if the domain name is a valid registered domain, and if the URL is using a secure protocol (https).\n\nAdditionally, the function should handle cases wh...
[ { "content": "Create a JavaScript function to validate if a given string is a valid URL. The function should check if the URL has at least one query parameter, if the domain name is a valid registered domain, and if the URL is using a secure protocol (https).\n\nAdditionally, the function should handle cases wh...
6
2
Write a JavaScript function that takes an argument and determines whether it is prime or composite. However, you cannot use any built-in functions or libraries to determine the primality of the number. The function should have a time complexity of O(sqrt(n)), where n is the input number.
[ { "content": "Write a JavaScript function that takes an argument and determines whether it is prime or composite. However, you cannot use any built-in functions or libraries to determine the primality of the number. The function should have a time complexity of O(sqrt(n)), where n is the input number.", "ro...
[ { "content": "Write a JavaScript function that takes an argument and determines whether it is prime or composite. However, you cannot use any built-in functions or libraries to determine the primality of the number. The function should have a time complexity of O(sqrt(n)), where n is the input number.", "ro...
6
4
Create a valid JavaScript array of objects containing the four elements "name", "age", "gender", and "hobbies". Each object should have a randomly generated name, age between 18 and 60, gender (either "male" or "female"), and an array of randomly generated hobbies (minimum 1 and maximum 5 hobbies per person). Addition...
[ { "content": "Create a valid JavaScript array of objects containing the four elements \"name\", \"age\", \"gender\", and \"hobbies\". Each object should have a randomly generated name, age between 18 and 60, gender (either \"male\" or \"female\"), and an array of randomly generated hobbies (minimum 1 and maximu...
[ { "content": "Create a valid JavaScript array of objects containing the four elements \"name\", \"age\", \"gender\", and \"hobbies\". Each object should have a randomly generated name, age between 18 and 60, gender (either \"male\" or \"female\"), and an array of randomly generated hobbies (minimum 1 and maximu...
9
1
Using a unique function in JavaScript, remove the duplicate values in the given array. However, you are not allowed to use any built-in JavaScript functions or libraries to achieve this. You must implement the removal of duplicates yourself. Furthermore, you must use constant space complexity O(1) to achieve this. No...
[ { "content": "Using a unique function in JavaScript, remove the duplicate values in the given array. However, you are not allowed to use any built-in JavaScript functions or libraries to achieve this. You must implement the removal of duplicates yourself.\n\nFurthermore, you must use constant space complexity O...
[ { "content": "Using a unique function in JavaScript, remove the duplicate values in the given array. However, you are not allowed to use any built-in JavaScript functions or libraries to achieve this. You must implement the removal of duplicates yourself.\n\nFurthermore, you must use constant space complexity O...
8
4
Write a JavaScript function that takes two positive integers as parameters and returns their sum. The function should be named "addNumbers". The function should only accept positive integers greater than 1 as inputs. If the input is not a positive integer or it is less than or equal to 1, the function should throw an ...
[ { "content": "Write a JavaScript function that takes two positive integers as parameters and returns their sum. The function should be named \"addNumbers\".\n\nThe function should only accept positive integers greater than 1 as inputs. If the input is not a positive integer or it is less than or equal to 1, the...
[ { "content": "Write a JavaScript function that takes two positive integers as parameters and returns their sum. The function should be named \"addNumbers\".\n\nThe function should only accept positive integers greater than 1 as inputs. If the input is not a positive integer or it is less than or equal to 1, the...
8
2
Change the variable "str" below to upper case, but without using any built-in string manipulation methods. Additionally, handle the case where the string contains special characters and numbers. Ensure that the solution has a time complexity of O(n) and a space complexity of O(1). let str = 'h3ll0 w0rld!';
[ { "content": "Change the variable \"str\" below to upper case, but without using any built-in string manipulation methods. Additionally, handle the case where the string contains special characters and numbers. Ensure that the solution has a time complexity of O(n) and a space complexity of O(1).\n\nlet str = '...
[ { "content": "Change the variable \"str\" below to upper case, but without using any built-in string manipulation methods. Additionally, handle the case where the string contains special characters and numbers. Ensure that the solution has a time complexity of O(n) and a space complexity of O(1).\n\nlet str = '...
9
3