Intermediate Algorithm Scripting: Convert HTML Entities - bug?

Intermediate Algorithm Scripting: Convert HTML Entities - bug?
0

#1

Hello Everyone!

I’ve finished the challange and it works in the Chrome’s console, but passes only 2 tests on FCC website. Below is my solution:

function convertHTML(str) {

// :)

const characters = {

"&": "&​amp;",

"<": "&​lt;",

">": "&​gt;",

"\"": """,

"'": "&​apos;"

}

const keys = Object.keys(characters);

const arr = str.split("");

const result = arr.map((val,i) => {

if (keys.includes(val)) {

return characters[val];

} else {

return val;

}

});

return result.join("");

}

convertHTML("Sixty > twelve");

Please, can you look at it and tell me what is going on?

Many thanks in advance!
Szymon


#2

Two things:

  1. Please, use MarkDown Code Formatting to enhace the visualitation.
    Markdown Code Formatting

  2. What test not pass? What do you get?


#3

@alojz your characters dictionary contains \u200b (Zero-width space) characters in values, try removing them.


#4

  const characters = {
    "&": "&​amp;",
    "<": "&​lt;",
    ">": "&​gt;",
    '"': "&quot;",
    "'": "&​apos;"
  }

  const keys = Object.keys(characters);
  const arr = str.split("");

  const result = arr.map((val,i) => {
    if (keys.includes(val)) {
      return characters[val];
  } else {
    return val;
  }
  });

  return result.join("");
}

convertHTML("Sixty > twelve");

When I run above code in the Chrome’s console, I get:

“Sixty &​gt; twelve”

I don’t know why the test: " convertHTML("Sixty > twelve") should return Sixty &​gt; twelve ." isn’t passed…

Only these two tests are passed:

convertHTML('Stuff in "quotation marks"') should return Stuff in &​quot;quotation marks&​quot; .
convertHTML("abc") should return abc .

But in the console I get expected results also for other tests.

@iiiked I’ve removed it, but nothing’s changed…

Thanks for answers.

Cheers,
Szymon


#5

As @iiiked are stated, you have extra hidden characters in your lookup object. I assume you copied and pasted the replacement values which appear to have these hidden characters in them. Just manually retype the replacement values and retest your code.


#6

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#7

:thinking: The Browser’s console is more permisive.

Try pass only one parameter to your function on the map() method. ¿why i? :man_shrugging: Replace by map(val => <!-- this continue...-->)

Your keys variable is unnecessary. You can check the ownership of an object with yourObject.hasOwnProperty(val)

More information about hasOwnProperty method:


#8

@RandellDawson Thanks! It works now! I didn’t even know that something like hidden characters can exist while copying values!

Cheers,
Szymon


#9

There are some extra characters which need to be removed. I am going to document it as an issue on Github.


#10

@alojz here are few more tips re your code

  1. If you have controlled dictionary where you know what type of values you’ve got, you can use even simpler version of your map method:
arr.map(val => characters[val] || val);
  1. Furthermore, you don’t really need to convert string to array and then array back to string, as string is a perfectly iterable type in javascript. There is no .map() method for string partly because there is much powerful .replace() method. For example, try this right after assigning your characters variable:
return str.replace(/./g, val =>  characters[val] || val);

Looks similar, right?
And actually, you can further specify what exactly you want to replace in the string:

const re = /[\&\<\>\"\']/g;
return str.replace(re, val => characters[val]);
// You no longer need to check if key exists in characters dictionary

Cheers!