I am working to solve this hard challenge in www.edabit.com.
My code is messing to complete the challenge and I tried too much on my code but until know I do not know how to pass this hard challenge. This challenge is called Hidden Anagram.
My Code So Far:
function hiddenAnagram(text, phrase) {
text = text.replace(/[^\w]/g, "").toLowerCase();
phrase = phrase.replace(/[^\w]/g, "").toLowerCase();
let sortText = text.split("").sort();
let sortPhrase = phrase.split("").sort()
return "noutfond";
}
console.log(hiddenAnagram("D e b90it->?$ (c)a r...d,,#~", "bad credit"));
Link to challenge: https://edabit.com/challenge/7hnsWYJGc6yPqEMjc
Okay, maybe i can be a bit more helpful. I’m going to spoiler tag each of these individually, as they’ll get more spoilery.
How is “An old west action hero actor” an anagram of “Clint Eastwood”?
What do you need to do to find any possible starting point for an anagram of the target?
What do we know about the length of the anagram, and how can that narrow our search
How do we check all of the possible substrings of the original text for an anagram of the target?
Follow-on: What do we know about when we can stop looking for points in the string where the anagram could start?
Check every valid starting point to see if the substring, once sorted, matches the target. If it does, return the original substring. If it doesn’t, continue. If you get to the end of your possibilities, return noutfond.
@m_hutley
I have read your post and I try again with my code but I have two problems:
The output in the console are not sorted as the challenge said. for example:
when you pass to the console :
the output returns: badcredit. But the challenge said should return: debitcard. But as you see, both are the same length and the same letters but in different order.
I used filter method and not for..loop because it did not work with me and I tried too much with it. so I do not know where to put return "noutfond".
You’re returning the phrase instead of the substring.
either way can work. return "noutfond" should be the last line of the function; the function should return the matched string if it finds one at any point - and return the default if it tries everything, and has no match.
#1: Please read post #4. #2: You’re not looking to see if an anagram can be made from the letters of the string. That’s not what the puzzle is asking you to do. Go back to the instructions, look at the example for hiddenAnagram("Bright is the moon", "Bongo mirth"), and their explanation for why that one should return noutfond.
Before trying to code the answer, you need to understand what the question is asking you to do.
yes Thank you
I was using Arrays instead of string.
I will try again solving this challenge.
I’m sorry not understanding correctly the challenge. English is not my first language!
What the challenge is asking you to do is to take the letters of a string, in order, and determine if any subset of consecutive characters in the string (case insensitively) constitute an anagram of the target phrase. If so, return the substring of those characters as they were in the original string. Otherwise, return “noutfond”.
I did, in fact.
I stuck closer to the OP’s work (and based on the suggestions in the challenge, probably what they were expecting), because… well it was going to go in here.
function hiddenAnagram(text, phrase) {
text = text.toLowerCase().replace(/[^a-z]/g, "");
phrase = phrase.toLowerCase().replace(/[^a-z]/g, "").split("").sort().join("");
for(i = 0; i <= text.length - phrase.length; i++) {
const ss = text.substr(i,phrase.length);
if(ss.split("").sort().join("") == phrase) { return ss; }
}
return "noutfond";
}