Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
Are they the "same"? Codewars Kata

I'm trying to solve the above-mentioned codewars kata with the following instructions:

Given two arrays a and b write a function comp(a, b) (orcompSame(a, b)) that checks whether the two arrays have the "same" elements, with the same multiplicities (the multiplicity of a member is the number of times it appears). "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

Examples Valid arrays a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [1111, 121121, 144144, 1919, 161161, 1919, 144144, 1919] Invalid arrays If, for example, we change the first number to something else, comp is not returning true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361] comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361] comp(a,b) returns false because in b 36100 is not the square of any number of a.

Remarks a or b might be [] or {} (all languages except R, Shell). a or b might be nil or null or None or nothing (except in C++, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift). If a or b are nil (or null or None, depending on the language), the problem doesn't make sense so return false.

This is my code, which is passing the initial tests but failing some tests later:

const comp = (a, b) => {
    function sortNumber(n1, n2) {
        return n1 - n2;
    }
    if (a === [] || b === [] || a === {} || b === {} || a === null || b === null) {
        return false
    } else {
        a = a.sort(sortNumber)
        b = b.sort(sortNumber)
    }
    for (let i = 0; i < a.length; i++) {
        for (let i = 0; i < b.length; i++) {
            if (Math.pow(a[i], 2) === b[i]) {
                return true
            } else {
                return false
            }

        }
    }
}

The error message I'm getting is: It should work with random inputs too - expected false but got true (or viceversa). What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!