diff --git a/curriculum/challenges/_meta/rosetta-code/meta.json b/curriculum/challenges/_meta/rosetta-code/meta.json index 31f5d6864c..280a02e322 100644 --- a/curriculum/challenges/_meta/rosetta-code/meta.json +++ b/curriculum/challenges/_meta/rosetta-code/meta.json @@ -344,6 +344,10 @@ "5a23c84252665b21eecc7edb", "Largest int from concatenated ints" ], + [ + "5a23c84252665b21eecc7edd", + "Last letter-first letter" + ], [ "5a23c84252665b21eecc7edc", "Last Friday of each month" @@ -360,6 +364,22 @@ "5a23c84252665b21eecc7ee0", "Left factorials" ], + [ + "5a23c84252665b21eecc7ee1", + "Letter frequency" + ], + [ + "5a23c84252665b21eecc7ee2", + "Levenshtein distance" + ], + [ + "5a23c84252665b21eecc7ee3", + "Linear congruential generator" + ], + [ + "5a23c84252665b21eecc7eec", + "Long multiplication" + ], [ "59da22823d04c95919d46269", "Sailors, coconuts and a monkey problem" diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/last-letter-first-letter.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/last-letter-first-letter.english.md new file mode 100644 index 0000000000..f2165912e1 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/last-letter-first-letter.english.md @@ -0,0 +1,129 @@ +--- +id: 5a23c84252665b21eecc7edd +title: Last letter-first letter +challengeType: 5 +--- + +## Description +
+A certain children's game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game. +For example, with "animals" as the category, +
+Child 1: dog
+Child 2: goldfish
+Child 1: hippopotamus
+Child 2: snake
+...
+
+
+ +## Instructions +
+Write a function that takes an input array of words. The function should return an array of words where the first letter of each word is the same as the last letter of the previous word. The first word in the return array can be anything. Use only the words in the input array, and once a word is used it cannot be repeated. The words in the return array should be selected and sequenced so that that the length of the array is maximized. +
+ +## Tests +
+ +``` yml +tests: + - text: findLongestChain should be a function. + testString: assert(typeof findLongestChain == 'function', 'findLongestChain should be a function.'); + - text: findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]) should return an array. + testString: assert(Array.isArray(findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])), 'findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]) should return an array.'); + - text: findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]) should return ["involves", "starting", "game", "each"]. + testString: assert.deepEqual(findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]), ['involves', 'starting', 'game', 'each'], 'findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]) should return ["involves", "starting", "game", "each"].'); + - text: findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"]) should return ["braviary", "yamask", "kangaskhan"] + testString: assert.deepEqual(findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"]), ['braviary', 'yamask', 'kangaskhan'], 'findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"]) should return ["braviary", "yamask", "kangaskhan"].'); + - text: findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"]) should return ["poliwrath", "harp", "poochyena", "archana"]. + testString: assert.deepEqual(findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"]), ['poliwrath', 'harp', 'poochyena', 'archana'], 'findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"]) should return ["poliwrath", "harp", "poochyena", "archana"].'); + - text: findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"]) should return ["scolipede", "elephant", "tigers", "sealeo"]. + testString: assert.deepEqual(findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"]), ['scolipede', 'elephant', 'tigers', 'sealeo'], 'findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"]) should return ["scolipede", "elephant", "tigers", "sealeo"].'); + - text: findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"]) should return ["machamp", "petilil", "lumineon", "nosepass"]. + testString: assert.deepEqual(findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"]), ['machamp', 'petilil', 'lumineon', 'nosepass'], 'findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"]) should return ["machamp", "petilil", "lumineon", "nosepass"].'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function findLongestChain (items) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function findLongestChain (items) { + function Ref(index, first_char, last_char) { + this.index = index; + this.first_char = first_char; + this.last_char = last_char; + } + + var items_len = items.length + var refs_len = items_len; + var refs = [] + + // enough space for all items + var longest_path_refs_len = 0; + var longest_path_refs = new Array(items_len); + + function search(curr_len) { + if (curr_len > longest_path_refs_len) { + longest_path_refs_len = curr_len; + + for (var i = 0; i < curr_len; i++) { + longest_path_refs[i] = refs[i]; + } + } + + // recursive search + var last_char = refs[curr_len - 1].last_char; + for (var i = curr_len; i < refs_len; i++) + if (refs[i].first_char == last_char) { + var aux = refs[curr_len]; + refs[curr_len] = refs[i]; + refs[i] = aux; + search(curr_len + 1); + refs[i] = refs[curr_len]; + refs[curr_len] = aux; + } + } + + for (var i = 0; i < items_len; i++) { + var itemsi_len = items[i].length; + refs.push(new Ref(i, items[i][0], items[i][itemsi_len - 1])); + } + + // try each item as possible start + for (var i = 0; i < items_len; i++) { + var aux = refs[0]; + refs[0] = refs[i]; + refs[i] = aux; + search(1); + refs[i] = refs[0]; + refs[0] = aux; + } + + var longest_path_len = longest_path_refs_len; + var longest_path = new Array(longest_path_len); + + for (var i = 0; i < longest_path_len; i++) + longest_path[i] = items[longest_path_refs[i].index]; + + return longest_path; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/letter-frequency.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/letter-frequency.english.md new file mode 100644 index 0000000000..c4b372f2ab --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/letter-frequency.english.md @@ -0,0 +1,82 @@ +--- +id: 5a23c84252665b21eecc7ee1 +title: Letter frequency +challengeType: 5 +--- + +## Description +
+You are given a string and your task is to calculate frequency of each character. +All characters should be counted. This includes lower and upper case letters, digits, whitespace, special characters, or any other distinct characters. +
+ +## Instructions +
+Write a function to count the occurrences of each character in a given string. +The function should return a 2D array with each of the elements in the following form: ['char', freq]. The character should be a string with a length of 1, and frequency is a number denoting the count. +For example, given the string "ab", your function should return [['a', 1], ['b', 1]]. +
+ +## Tests +
+ +``` yml +tests: + - text: letterFrequency should be a function. + testString: assert(typeof letterFrequency == 'function', 'letterFrequency should be a function.'); + - text: letterFrequency("Not all that Mrs. Bennet, however") should return an array. + testString: assert(Array.isArray(letterFrequency("Not all that Mrs. Bennet, however")), 'letterFrequency("Not all that Mrs. Bennet, however") should return an array.'); + - text: letterFrequency("Not all that Mrs. Bennet, however") should return [[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]]. + testString: assert.deepEqual(letterFrequency("Not all that Mrs. Bennet, however"), [[' ', 5], [',', 1], ['.', 1], ['B', 1], ['M', 1], ['N', 1], ['a', 2], ['e', 4], ['h', 2], ['l', 2], ['n', 2], ['o', 2], ['r', 2], ['s', 1], ['t', 4], ['v', 1], ['w', 1]], 'letterFrequency("Not all that Mrs. Bennet, however") should return [[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]].'); + - text: letterFrequency("daughters, could ask on the ") should return [[' ',5],[',',1],['a',2],['c',1],['d',2],['e',2],['g',1],['h',2],['k',1],['l',1],['n',1],['o',2],['r',1],['s',2],['t',2],['u',2]]. + testString: assert.deepEqual(letterFrequency("daughters, could ask on the "), [[' ', 5], [',', 1], ['a', 2], ['c', 1], ['d', 2], ['e', 2], ['g', 1], ['h', 2], ['k', 1], ['l', 1], ['n', 1], ['o', 2], ['r', 1], ['s', 2], ['t', 2], ['u', 2]], 'letterFrequency("daughters, could ask on the ") should return [[" ", 5], [", ", 1], ["a", 2], ["c", 1], ["d", 2], ["e", 2], ["g", 1], ["h", 2], ["k", 1], ["l", 1], ["n", 1], ["o", 2], ["r", 1], ["s", 2], ["t", 2], ["u", 2]].'); + - text: letterFrequency("husband any satisfactory description") should return [[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]]. + testString: assert.deepEqual(letterFrequency("husband any satisfactory description"), [[' ', 3], ['a', 4], ['b', 1], ['c', 2], ['d', 2], ['e', 1], ['f', 1], ['h', 1], ['i', 3], ['n', 3], ['o', 2], ['p', 1], ['r', 2], ['s', 4], ['t', 3], ['u', 1], ['y', 2]], 'letterFrequency("husband any satisfactory description") should return [[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]].'); + - text: letterFrequency("in various ways--with barefaced") should return [[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]]. + testString: assert.deepEqual(letterFrequency("in various ways--with barefaced"), [[' ', 3], ['-', 2], ['a', 4], ['b', 1], ['c', 1], ['d', 1], ['e', 2], ['f', 1], ['h', 1], ['i', 3], ['n', 1], ['o', 1], ['r', 2], ['s', 2], ['t', 1], ['u', 1], ['v', 1], ['w', 2], ['y', 1]], 'letterFrequency("in various ways--with barefaced") should return [[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]].'); + - text: letterFrequency("distant surmises; but he eluded") should return [[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]]. + testString: assert.deepEqual(letterFrequency("distant surmises; but he eluded"), [[' ', 4], [';', 1], ['a', 1], ['b', 1], ['d', 3], ['e', 4], ['h', 1], ['i', 2], ['l', 1], ['m', 1], ['n', 1], ['r', 1], ['s', 4], ['t', 3], ['u', 3]], 'letterFrequency("distant surmises; but he eluded") should return [[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]].'); + - text: letterFrequency("last obliged to accept the second-hand,") should return [[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]]. + testString: assert.deepEqual(letterFrequency("last obliged to accept the second-hand,"), [[' ', 5], [',', 1], ['-', 1], ['a', 3], ['b', 1], ['c', 3], ['d', 3], ['e', 4], ['g', 1], ['h', 2], ['i', 1], ['l', 2], ['n', 2], ['o', 3], ['p', 1], ['s', 2], ['t', 4]], 'letterFrequency("last obliged to accept the second-hand,") should return [[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]].'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function letterFrequency (txt) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function letterFrequency (txt) { + var cs = txt.split(''), + i = cs.length, + dct = {}, + c = '', + keys; + + while (i--) { + c = cs[i]; + dct[c] = (dct[c] || 0) + 1; + } + + keys = Object.keys(dct); + keys.sort(); + return keys.map(function (c) { return [c, dct[c]]; }); +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/levenshtein-distance.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/levenshtein-distance.md new file mode 100644 index 0000000000..79d9903352 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/levenshtein-distance.md @@ -0,0 +1,81 @@ +--- +id: 5a23c84252665b21eecc7ee2 +title: Levenshtein distance +challengeType: 5 +--- + +## Description +
+In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character. +Example: +The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits: +kitten sitten (substitution of 'k' with 's') +sitten sittin (substitution of 'e' with 'i') +sittin sitting (insert 'g' at the end). +''The Levenshtein distance between "rosettacode", "raisethysword" is 8. +The distance between two strings is same as that when both strings are reversed. +
+ +## Instructions +
+Write a function that returns the Levenshtein distance between two strings given as parameters . +
+ +## Tests +
+ +``` yml +tests: + - text: levenshtein should be a function. + testString: assert(typeof levenshtein == 'function', 'levenshtein should be a function.'); + - text: levenshtein("mist", "dist") should return a number. + testString: assert(typeof levenshtein("mist", "dist") == 'number', 'levenshtein("mist", "dist") should return a number.'); + - text: levenshtein("mist", "dist") should return 1. + testString: assert.equal(levenshtein("mist", "dist"), 1, 'levenshtein("mist", "dist") should return 1.'); + - text: levenshtein("tier", "tor") should return 2. + testString: assert.equal(levenshtein("tier", "tor"), 2, 'levenshtein("tier", "tor") should return 2.'); + - text: levenshtein("kitten", "sitting") should return 3. + testString: assert.equal(levenshtein("kitten", "sitting"), 3, 'levenshtein("kitten", "sitting") should return 3.'); + - text: levenshtein("stop", "tops") should return 2. + testString: assert.equal(levenshtein("stop", "tops"), 2, 'levenshtein("stop", "tops") should return 2.'); + - text: levenshtein("rosettacode", "raisethysword") should return 8. + testString: assert.equal(levenshtein("rosettacode", "raisethysword"), 8, 'levenshtein("rosettacode", "raisethysword") should return 8.'); + - text: levenshtein("mississippi", "swiss miss") should return 8. + testString: assert.equal(levenshtein("mississippi", "swiss miss"), 8, 'levenshtein("mississippi", "swiss miss") should return 8.'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function levenshtein (a, b) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function levenshtein (a, b) { + var t = [], u, i, j, m = a.length, n = b.length; + if (!m) { return n; } + if (!n) { return m; } + for (j = 0; j <= n; j++) { t[j] = j; } + for (i = 1; i <= m; i++) { + for (u = [i], j = 1; j <= n; j++) { + u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : Math.min(t[j - 1], t[j], u[j - 1]) + 1; + } t = u; + } return u[n]; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/linear-congruential-generator.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/linear-congruential-generator.md new file mode 100644 index 0000000000..2b92a0daeb --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/linear-congruential-generator.md @@ -0,0 +1,76 @@ +--- +id: 5a23c84252665b21eecc7ee3 +title: Linear congruential generator +challengeType: 5 +--- + +## Description +
+The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: +$$r_{n + 1} = a \times r_n + c \pmod m$$ +Where: + +If one chooses the values of $a$, $c$ and $m$ with care, then the generator produces a uniform distribution of integers from $0$ to $m - 1$. +LCG numbers have poor quality. $r_n$ and $r_{n + 1}$ are not independent, as true random numbers would be. Anyone who knows $r_n$ can predict $r_{n + 1}$, therefore LCG is not cryptographically secure. The LCG is still good enough for simple tasks like Miller-Rabin primality test, or FreeCell deals. Among the benefits of the LCG, one can easily reproduce a sequence of numbers, from the same $r_0$. One can also reproduce such sequence with a different programming language, because the formula is so simple. +
+ +## Instructions +
+Write a function that takes $r_0,a,c,m,n$ as parameters and returns $r_n$. +
+ +## Tests +
+ +``` yml +tests: + - text: linearCongGenerator should be a function. + testString: assert(typeof linearCongGenerator == 'function', 'linearCongGenerator should be a function.'); + - text: linearCongGenerator(324, 1145, 177, 2148, 3) should return a number. + testString: assert(typeof linearCongGenerator(324, 1145, 177, 2148, 3) == 'number', 'linearCongGenerator(324, 1145, 177, 2148, 3) should return a number.'); + - text: linearCongGenerator(324, 1145, 177, 2148, 3) should return 855. + testString: assert.equal(linearCongGenerator(324, 1145, 177, 2148, 3), 855, 'linearCongGenerator(324, 1145, 177, 2148, 3) should return 855.'); + - text: linearCongGenerator(234, 11245, 145, 83648, 4) should return 1110. + testString: assert.equal(linearCongGenerator(234, 11245, 145, 83648, 4), 1110, 'linearCongGenerator(234, 11245, 145, 83648, 4) should return 1110.'); + - text: linearCongGenerator(85, 11, 1234, 214748, 5) should return 62217. + testString: assert.equal(linearCongGenerator(85, 11, 1234, 214748, 5), 62217, 'linearCongGenerator(85, 11, 1234, 214748, 5) should return 62217.'); + - text: linearCongGenerator(0, 1103515245, 12345, 2147483648, 1) should return 12345. + testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 1), 12345, 'linearCongGenerator(0, 1103515245, 12345, 2147483648, 1) should return 12345.'); + - text: linearCongGenerator(0, 1103515245, 12345, 2147483648, 2) should return 1406932606. + testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 2), 1406932606, 'linearCongGenerator(0, 1103515245, 12345, 2147483648, 2) should return 1406932606.'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function linearCongGenerator (r0, a, c, m, n) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function linearCongGenerator(r0, a, c, m, n) { + for (let i = 0; i < n; i++) { + r0 = (a * r0 + c) % m; + } + return r0; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/long-multiplication.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/long-multiplication.md new file mode 100644 index 0000000000..745b0a87c8 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/long-multiplication.md @@ -0,0 +1,81 @@ +--- +id: 5a23c84252665b21eecc7eec +title: Long multiplication +challengeType: 5 +--- + +## Description +
+Explicitly implement long multiplication. +This is one possible approach to arbitrary-precision integer algebra. +Note: In JavaScript, arithmetic operations are inaccurate with large numbers, so you will have to implement precise multiplication yourself. +
+ +## Instructions +
+Write a function that takes two strings of large numbers as parameters. Your function should return the product of these two large numbers as a string. +
+ +## Tests +
+ +``` yml +tests: + - text: mult should be a function. + testString: assert(typeof mult == 'function', 'mult should be a function.'); + - text: mult("18446744073709551616", "18446744073709551616") should return a string. + testString: assert(typeof mult("18446744073709551616", "18446744073709551616") == 'string', 'mult("18446744073709551616", "18446744073709551616") should return a string.'); + - text: mult("18446744073709551616", "18446744073709551616") should return "340282366920938463463374607431768211456". + testString: assert.equal(mult("18446744073709551616", "18446744073709551616"), "340282366920938463463374607431768211456", 'mult("18446744073709551616", "18446744073709551616") should return "340282366920938463463374607431768211456".'); + - text: mult("31844674073709551616", "1844674407309551616") should return "58743055272886011737990786529368211456". + testString: assert.equal(mult("31844674073709551616", "1844674407309551616"), "58743055272886011737990786529368211456", 'mult("31844674073709551616", "1844674407309551616") should return "58743055272886011737990786529368211456".'); + - text: mult("1846744073709551616", "44844644073709551616") should return "82816580680737279241781007431768211456". + testString: assert.equal(mult("1846744073709551616", "44844644073709551616"), "82816580680737279241781007431768211456", 'mult("1846744073709551616", "44844644073709551616") should return "82816580680737279241781007431768211456".'); + - text: mult("1844674407370951616", "1844674407709551616") should return "3402823669833978308014392742590611456". + testString: assert.equal(mult("1844674407370951616", "1844674407709551616"), "3402823669833978308014392742590611456", 'mult("1844674407370951616", "1844674407709551616") should return "3402823669833978308014392742590611456".'); + - text: mult("2844674407370951616", "1844674407370955616") should return "5247498076580334548376218009219475456". + testString: assert.equal(mult("2844674407370951616", "1844674407370955616"), "5247498076580334548376218009219475456", 'mult("2844674407370951616", "1844674407370955616") should return "5247498076580334548376218009219475456".'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function mult (strNum1, strNum2) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ +```js +function mult(strNum1, strNum2) { + var a1 = strNum1.split("").reverse(); + var a2 = strNum2.toString().split("").reverse(); + var aResult = new Array; + + for ( var iterNum1 = 0; iterNum1 < a1.length; iterNum1++ ) { + for ( var iterNum2 = 0; iterNum2 < a2.length; iterNum2++ ) { + var idxIter = iterNum1 + iterNum2; // Get the current array position. + aResult[idxIter] = a1[iterNum1] * a2[iterNum2] + ( idxIter >= aResult.length ? 0 : aResult[idxIter] ); + + if ( aResult[idxIter] > 9 ) { // Carrying + aResult[idxIter + 1] = Math.floor( aResult[idxIter] / 10 ) + ( idxIter + 1 >= aResult.length ? 0 : aResult[idxIter + 1] ); + aResult[idxIter] %= 10; + } + } + } + return aResult.reverse().join(""); +} +``` + +