0

Hello all I have an ascii image.

<pre class="bear">   _     _   
  (c).-.(c)  
   / ._. \   
 __\( P )/__ 
(_.-/'L'\-._)
   || A ||   
 _.' `Y' '._ 
(.-./`-'\.-.)
 `-'     `-' </pre>

I am wrapping each character with a span using the following function

const bear = document.querySelector('.bear')
bear.innerHTML = sparanWrap(bear.textContent)

const sparanWrap = (word) => {
return [...word].map((letter) => `<span>${letter}</span>`).join('')
}

The ascii image contains the characters 'P','L','A','Y' which I would lke to wrap with a different span:

<span class="play">P</span>
<span class="play">L</span>
<span class="play">A</span>
<span class="play">Y</span> 

I'm guessing I should be using filter here but not quite sure how to do that?

Thanks in advance

3
  • Just check if letter is one of your "special" characters and return a different string. Commented Mar 15, 2020 at 17:24
  • with an `if`` statement fro example? Commented Mar 15, 2020 at 17:27
  • got it thanks for the hint Commented Mar 15, 2020 at 17:39

2 Answers 2

1

First I would like to point out that because you assigned your function to a const, it will not get hoisted and therefore you cannot call it before declaring it. If you need it to be declared afterwards for whatever reason, use the function keyword instead.

Working example

In the ASCII art in the example, only uppercase letters will have a different class, so you could use a simple Regular Expression such as /[A-Z]/ to test each letter:

const bear = document.querySelector('.bear')
bear.innerHTML = sparanWrap(bear.textContent)

function sparanWrap(word) {

  // define helper function to identify uppercase letters
  let applyClass = letter => /[A-Z]/.test(letter) ? ' class="play"' : ''

  //apply function inside template string
  return [...word].map((letter) => `<span${applyClass(letter)}>${letter}</span>`).join('')

}

Edit: I realized raw angle brackets could be a bad idea - although this is a pre tag, if it were anything else it might have caused the browser to interpret it as an opening HTML tag.

This, however, is hardly safe for indiscriminate use in ASCII art. If, by any means, the letters which you want to replace in the art also appear as part of the art (for example, if the c in the ears were uppercase), I would suggest you wrap the letters in some specific marking (like the ones used for HTML comments: <!-- and -->), then you could include them in your Regex, like so:

/<!--[A-Z]-->/
Sign up to request clarification or add additional context in comments.

4 Comments

A very elegant solution and It has helped me understand the capabilities of Regular Expressions, thank you
The Regexp.prototype.test() method takes a string as an argument and returns true if the string passed matches the pattern from the regex. You can read the full docs here.
And you're right, regular expressions can be really powerful, but each language may vary on the way they implement regex. JS, for example, used to lack lookbehinds, but it seems it's slowly being implemented. There are a few exercises on HackerRank that can get you through with the basics.
Thanks for pointing out HackerRank. Looks like a useful learning tool
0

Thank you Andreas for the hint, a ternary operator worked

const sparanWrap = (word) => {
        return [...word]
            .map((letter) => {
                return letter === 'P' ||
                    letter === 'L' ||
                    letter === 'A' ||
                    letter === 'Y'
                    ? `<span class="word">${letter}</span>`
                    : `<span>${letter}</span>`
            })
            .join('')
    }

1 Comment

["P", "L", "A", "Y"].includes(letter) ? ... : ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.