Intermediate Algorithm Scripting: Convert HTML Entities - bug?

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

Two things:

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

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

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


  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.

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

Thanks for answers.

Cheers,
Szymon

: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:

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

Cheers,
Szymon

@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!