Doing CodeWars Algorithms Now - Still Uneasy, Need Help

Hi,

So, I’ve been on a steady diet of JS youtube videos, FCC algorithms/learning, books, etc, but some things I forgot (already! grrr) and now I’m doing CodeWars algorithms, which seem to be a major step up in difficulty, even on the 8th and 7th Kyu katas. I don’t know if I screwed something up, but I can’t figure out for the life of me when to use certain higher order functions in certain contexts. Here’s the problem:

REDACTED

Meanwhile... somewhere in a Pentagon basement
Your job is to compare two confidential documents that have come into your possession.

The first document has parts redacted, and the other one doesn't.

But the original (unredacted) document might be a fake!

You need to compare the two documents and decide if it is possible they are the same or not.

Kata Task
Return true if the two documents are possibly the same. Return false otherwise.

Notes
Each document is made of any visible characters, spaces, punctuation, and newlines \n
Any character might be redacted (except \n)
The redaction character is X
The redacted document is always the first one

Now, for this algorithm, I’ve tried 3 different solutions, and I’ve been working on it for 2.5 hours (is this normal?). I’m worried that I’m not where I should be skills wise. Here are the aborted attempts I’ve made at this Kata.

The REGEX attempt

function redacted(doc1, doc2) {
    if (doc1.match(/[X]/) === "X") {
        doc2.indexOf(doc1.match(/[X]/)).replace("", "X");
        let doc1Arr = doc1.split("");
        let doc2Arr = doc2.split("");
        if (doc1Arr === doc2Arr){
            return true;
        } else {
            return false;
        }
    }
}

Array Split Iterative #1

function redacted(doc1, doc2) {
    let doc1Arr = doc1.split("");
    let doc2Arr = doc2.split("");
    
    if (doc1Arr === doc2Arr) {
        return true;
    } else if (doc1Arr.length != doc2Arr.length) {
        return false;
    } else {
        for (let i = 0; i < doc1Arr.length; i++) {
            if (doc1Arr[i] !== doc2Arr[i]){
                return true;
            }
        }
    }
}

Array Split Iterative #2

function redacted(doc1, doc2) {
    let redactedArr = doc1.split("");
    let normalArr = doc2.split("");
    let redactedXIndex = [];
    
    for (let i = 0; i < redactedArr.length; i++) {
        if (redactedArr[i] === "X") {
            redactedXIndex.push([i]);
            for (let n = 0; n < normalArr.length; n++) {
                redactedXIndex.forEach(function (n) {
                    let index = //Lost train of thought this is becoming too complex. Going to whiteboard.
                });
            }
            //Each index number of redactedXXX, change to X on normalArr, then compare.
        }
        console.log(redactedXIndex);
    }
}

What am I missing? Anyone have any tips on methods or higher order functions I should be looking at?

What exactly do you think this is doing for you?

Replacing a match of the char X in doc 1 with an X in doc 2?

Yeah, I am unsure of the right syntax for that. I have the issue whiteboarded and can explain a solution in plain English. Here’s what I want to do to solve this, logically:

If there are redacted letters in doc1, match any whitespace or characters in doc2 and replace them with “X”, then see if the two strings match.

I suggest breaking this down into smaller steps which show the logic in the simplest form. There is some form of iteration needed which I don’t see in your written algorithm.

Hey, thanks for the tips. I appreciate your help!

When I look at this problem I see the following algorithm:

  1. If doc1’s length does not match doc2’s length return false otherwise continue to next step
  2. Iterate through each char of doc2 and if the character at the any index of doc1 and doc2 do not match AND the character of the index in doc1 is not an “X” AND the character at the index of doc1 is not a “\n”, then return false.
  3. If all characters have been compared and step 2 did not result in returning false, then return true

What’s weird about this is that the original solution I had seems similar to one of the solutions there. Is there a point in JS when you wouldn’t encapsulate a return after a conditional statement?

I would need to see the other solution to comment on their differences.