in Education by
I am attempting to solve a problem where if the elements in one array are squared, and all of the new elements are found in array 2, then return true, otherwise return false. My code looks right but it gets caught up whenever 1 number is very close to the square of another. Here is my code: function comp(arr1, arr2){ let a1 = arr1.sort((a, b) => a - b).map(x => x*x); let a2 = arr2.sort((a, b) => a - b); return a1.filter(x => !a2.includes(x)).length === 0 ? true : false } When I test it on repl, with arr1 = [91, 15, 54, 53, 78, 80, 91]; arr2 = [8281, 225, 2916, 2809, 6084, 6400, 8282]; I get true for some reason even though it should be false. However other tested arrays for arr1 and arr2 pass, it just randomly fails. Is my code incorrect? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
EDIT (see edit history for original that fails if there are duplicates in the first array): Your original code works (at least for the example inputs). You are just doing more than necessary to get the output. The input array included in your question... [91, 15, 54, 53, 78, 80, 91] ...results in the following array after squaring the values: [8281, 225, 2916, 2809, 6084, 6400, 8281] All of these squared values are included in your comparison array (which also contains one additional value that is not generated by squaring the input array): [8281, 225, 2916, 2809, 6084, 6400, 8282] A slightly simpler approach would be to square the values in the first array and then use every and includes to check if every squared value exists in the second array. For example: const squareCompare = (square, compare) => { return square.map((x) => x ** 2).every((x) => compare.includes(x)); }; const t = squareCompare([91, 15, 54, 53, 78, 80, 91], [8281, 225, 2916, 2809, 6084, 6400, 8282]); console.log(t); // true const f = squareCompare([2, 4, 2], [25, 4, 81]); console.log(f); // false Run code snippetExpand snippet

Related questions

0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I have a table like below 11 12 13 11 12 13 11 12 13 After I click on the table, I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a table like below 11 12 13 11 12 13 11 12 13 After I click on the table, I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How can I check a regular expression to be fulfilled only if there is a number or group of numbers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    How can I check a regular expression to be fulfilled only if there is a number or group of numbers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I am trying to implement an algorithm, that finds the shortest path in the following two dimensional array ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    I am working on a project where I need to use array values in the getElementById() in javascript. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I'm trying to assemble and display image slices which have been preloaded into a two dimensional array, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    There are loads of questions about the current date in JavaScript in general, but in my scenario, I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I want to get this format : 2019-03-24 15:05:20 Here is what I have tried: var today = ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want to get this format : 2019-03-24 15:05:20 Here is what I have tried: var today = ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want to get this format : 2019-03-24 15:05:20 Here is what I have tried: var today = ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I have function of "endgame" and here's how the function goes function endgame() { setScreen("scorescreen" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
...