From c37cb92e63843d69f70e809da9403931b2b55813 Mon Sep 17 00:00:00 2001 From: Crystal Yungwirth Date: Fri, 24 May 2019 16:10:05 -0400 Subject: [PATCH] Alternative Advanced Code Solution (#35965) * Alternative Advanced Code Solution Uses .replace() instead of .split()-.join() * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com> * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com> * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com> * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com> * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com> * Update guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Addresses review by RandellDawson --- .../binary-agents/index.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md index 3f29d2957a..4884c2f347 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents/index.md @@ -147,6 +147,26 @@ Make sure that each time you transcode a character from binary to decimal, you r * Array.prototype.map +## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Alternative Advanced Code Solution: +```js + const binaryAgent = str => str.replace(/\d+./g, char => String.fromCharCode(`0b${char}`)); + + binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); +``` + +## Code Explanation + +* Find all groups of one or more digits followed by one other character +* Replace with a string created from the specified sequence of UTF-16 code units +* Use `0b` to lead the code unit to express that a binary integer literal is being converted. + +## Relevant Links + +* String.fromCharCode() +* String.prototype.replace() +* Convert a string of binary characters to the ASCII equivalents +* Grammar and types/Numeric Literals + ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: * ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.