From 91df817cfe8a0de2c895dc1e3a69ce2101c79e79 Mon Sep 17 00:00:00 2001 From: mrugesh <1884376+raisedadead@users.noreply.github.com> Date: Tue, 30 Jul 2019 00:25:58 +0530 Subject: [PATCH] fix(guide) add stubs, update spellings and prepare for move (#36531) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(guide) add stubs and correct file path misspellings and pr… (#36528) * fix: corrected file path to match curriculum * fix: renamed to newer challenge name * fix: added solutions to articles from challenge files * fix: added missing .english to file name * fix: added missing title to guide article * fix: correct solution for guide article * fix: replaced stub with hint * fix: added space in Hint headers * fix: added solution to guide article * fix: added solution to guide article * test: replaced stub with hint and solution * fix: add Problem number: to title * fix: changed generatorexponential to correct name * fix: renamed knight's tour to knights-tour * fix: updated guide article --- ...sing-object-property-shorthand.english.md} | 0 ...th-the-javascript-fetch-method.english.md} | 0 ...our.english.md => knights-tour.english.md} | 0 .../check-if-binary-search-tree/index.md | 50 +++ .../problem-1-multiples-of-3-and-5/index.md | 2 +- .../problem-10-summation-of-primes/index.md | 2 +- .../index.md | 6 +- .../index.md | 6 +- .../problem-2-even-fibonacci-numbers/index.md | 2 +- .../index.md | 6 +- .../problem-3-largest-prime-factor/index.md | 2 +- .../index.md | 2 +- .../problem-7-10001st-prime/index.md | 2 +- .../index.md | 2 +- .../index.md | 2 +- .../rosetta-code/averages-mode/index.md | 35 ++ .../averages-pythagorean-means/index.md | 76 +++++ .../averages-root-mean-square/index.md | 18 ++ .../rosetta-code/averagesmode/index.md | 10 - .../averagespythagorean-means/index.md | 10 - .../averagesroot-mean-square/index.md | 10 - .../cumulative-standard-deviation/index.md | 26 ++ .../rosetta-code/cusip/index.md | 40 +++ .../rosetta-code/cut-a-rectangle/index.md | 66 ++++ .../rosetta-code/dot-product/index.md | 20 ++ .../index.md | 0 .../rosetta-code/k-d-tree/index.md | 303 ++++++++++++++++++ .../rosetta-code/kaprekar-numbers/index.md | 23 ++ .../rosetta-code/knights-tour/index.md | 96 ++++++ .../index.md | 29 ++ .../last-friday-of-each-month/index.md | 25 ++ .../rosetta-code/leap-year/index.md | 17 + .../least-common-multiple/index.md | 23 ++ .../rosetta-code/left-factorials/index.md | 32 ++ .../index.md | 19 ++ .../sort-disjoint-sublist/index.md | 31 ++ .../rosetta-code/sort-stability/index.md | 18 ++ .../sort-using-a-custom-comparator/index.md | 23 ++ .../sorting-algorithms-bead-sort/index.md | 49 +++ .../sorting-algorithms-bogosort/index.md | 35 ++ .../sorting-algorithms-cocktail-sort/index.md | 43 +++ .../sorting-algorithms-comb-sort/index.md | 56 ++++ .../sorting-algorithms-gnome-sort/index.md | 27 ++ .../sorting-algorithms-pancake-sort/index.md | 45 +++ .../index.md | 53 +++ .../sorting-algorithms-shell-sort/index.md | 25 ++ .../sorting-algorithms-stooge-sort/index.md | 40 +++ .../sorting-algorithms-strand-sort/index.md | 47 +++ .../rosetta-code/soundex/index.md | 38 +++ .../rosetta-code/spiral-matrix/index.md | 31 ++ .../index.md | 46 +++ .../rosetta-code/state-name-puzzle/index.md | 58 ++++ .../stern-brocot-sequence/index.md | 26 ++ .../straddling-checkerboard/index.md | 52 +++ .../rosetta-code/stream-merge/index.md | 37 +++ .../index.md | 21 ++ .../rosetta-code/subleq/index.md | 34 ++ .../rosetta-code/sudoku/index.md | 194 +++++++++++ .../sum-digits-of-an-integer/index.md | 19 ++ .../sum-multiples-of-3-and-5/index.md | 21 ++ .../rosetta-code/sum-of-a-series/index.md | 22 ++ .../rosetta-code/sum-of-squares/index.md | 23 ++ .../rosetta-code/sum-to-100/index.md | 100 ++++++ .../index.md | 50 +++ .../symmetric-difference/index.md | 34 ++ .../rosetta-code/vector-dot-product/index.md | 43 ++- .../use-the-twitch-json-api/index.md | 4 +- .../index.md | 26 +- .../index.md | 60 ++++ .../index.md | 72 ++++- .../index.md | 70 +++- .../index.md | 63 +++- .../render-with-an-if-else-condition/index.md | 45 +++ .../index.md | 2 +- .../index.md | 25 ++ .../index.md | 25 ++ .../es6/create-a-javascript-promise/index.md | 17 + .../es6/create-a-module-script/index.md | 21 ++ .../index.md | 28 ++ .../index.md | 32 ++ .../index.md | 19 ++ .../index.md | 25 +- .../index.md | 29 ++ .../use-export-to-share-a-code-block/index.md | 21 ++ .../index.md | 4 +- .../index.md | 4 +- .../index.md | 0 .../cascading-css-variables/index.md | 56 ---- .../basic-css/inherit-css-variables/index.md | 252 +++++++++++++++ 89 files changed, 3074 insertions(+), 129 deletions(-) rename curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/{write-concise-object-literal-declarations-using-object-property-shorthand.md => write-concise-object-literal-declarations-using-object-property-shorthand.english.md} (100%) rename curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/{get-json-with-the-javascript-fetch-method.md => get-json-with-the-javascript-fetch-method.english.md} (100%) rename curriculum/challenges/english/08-coding-interview-prep/rosetta-code/{knight's-tour.english.md => knights-tour.english.md} (100%) create mode 100644 guide/english/certifications/coding-interview-prep/data-structures/check-if-binary-search-tree/index.md rename guide/english/certifications/coding-interview-prep/project-euler/{problem-157-solving-the-diophantine-equation-1a1b-p10n => problem-152-writing-one-half-as-a-sum-of-inverse-squares}/index.md (62%) rename guide/english/certifications/coding-interview-prep/project-euler/{problem-152-writing-12-as-a-sum-of-inverse-squares => problem-157-solving-the-diophantine-equation}/index.md (65%) rename guide/english/certifications/coding-interview-prep/project-euler/{problem-283-integer-sided-triangles-for-which-the--areaperimeter-ratio-is-integral => problem-283-integer-sided-triangles-for-which-the-area--perimeter-ratio-is-integral}/index.md (63%) create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averages-mode/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averages-pythagorean-means/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averages-root-mean-square/index.md delete mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averagesmode/index.md delete mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averagespythagorean-means/index.md delete mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/averagesroot-mean-square/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/cumulative-standard-deviation/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/cusip/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/cut-a-rectangle/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/dot-product/index.md rename guide/english/certifications/coding-interview-prep/rosetta-code/{generatorexponential => generator-exponential}/index.md (100%) create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/k-d-tree/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/kaprekar-numbers/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/knights-tour/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/largest-int-from-concatenated-ints/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/last-friday-of-each-month/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/leap-year/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/least-common-multiple/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/left-factorials/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sort-an-array-of-composite-structures/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sort-disjoint-sublist/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sort-stability/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sort-using-a-custom-comparator/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bogosort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/soundex/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/spiral-matrix/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/split-a-character-string-based-on-change-of-character/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/state-name-puzzle/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/stern-brocot-sequence/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/straddling-checkerboard/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/stream-merge/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/strip-control-codes-and-extended-characters-from-a-string/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/subleq/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sudoku/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sum-digits-of-an-integer/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sum-multiples-of-3-and-5/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-a-series/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-squares/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sum-to-100/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/sutherland-hodgman-polygon-clipping/index.md create mode 100644 guide/english/certifications/coding-interview-prep/rosetta-code/symmetric-difference/index.md create mode 100644 guide/english/certifications/data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method/index.md create mode 100644 guide/english/certifications/front-end-libraries/react/render-with-an-if-else-condition/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-javascript-promise/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-module-script/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects/index.md create mode 100644 guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block/index.md rename guide/english/certifications/javascript-algorithms-and-data-structures/es6/{write-concise-object-literal-declarations-using-simple-fields => write-concise-object-literal-declarations-using-object-property-shorthand}/index.md (76%) rename guide/english/certifications/responsive-web-design/applied-accessibility/{make-links-navigatable-with-html-access-keys => make-links-navigable-with-html-access-keys}/index.md (100%) delete mode 100644 guide/english/certifications/responsive-web-design/basic-css/cascading-css-variables/index.md create mode 100644 guide/english/certifications/responsive-web-design/basic-css/inherit-css-variables/index.md diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.english.md similarity index 100% rename from curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.md rename to curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.english.md diff --git a/curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md b/curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.english.md similarity index 100% rename from curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.md rename to curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method.english.md diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/knight's-tour.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/knights-tour.english.md similarity index 100% rename from curriculum/challenges/english/08-coding-interview-prep/rosetta-code/knight's-tour.english.md rename to curriculum/challenges/english/08-coding-interview-prep/rosetta-code/knights-tour.english.md diff --git a/guide/english/certifications/coding-interview-prep/data-structures/check-if-binary-search-tree/index.md b/guide/english/certifications/coding-interview-prep/data-structures/check-if-binary-search-tree/index.md new file mode 100644 index 0000000000..db588e11db --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/data-structures/check-if-binary-search-tree/index.md @@ -0,0 +1,50 @@ +--- +title: Check if Tree is Binary Search Tree +--- +# Check if Tree is Binary Search Tree + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2)); +function Node(value) { + this.value = value; + this.left = null; + this.right = null; +} +function BinarySearchTree() { + this.root = null; +} +function isBinarySearchTree(tree) { + if (tree.root == null) { + return null; + } else { + let isBST = true; + function checkTree(node) { + if (node.left != null) { + const left = node.left; + if (left.value > node.value) { + isBST = false; + } else { + checkTree(left); + } + } + if (node.right != null) { + const right = node.right; + if (right.value < node.value) { + isBST = false; + } else { + checkTree(right); + } + } + } + checkTree(tree.root); + return isBST; + } +}; +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-1-multiples-of-3-and-5/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-1-multiples-of-3-and-5/index.md index 1b180d5b09..eca6a36f58 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-1-multiples-of-3-and-5/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-1-multiples-of-3-and-5/index.md @@ -1,5 +1,5 @@ --- -title: Multiples of 3 and 5 +title: 'Problem 1: Multiples of 3 and 5' --- # Problem 1: Multiples of 3 and 5 diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-10-summation-of-primes/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-10-summation-of-primes/index.md index bd148cabf8..7aa76dbb65 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-10-summation-of-primes/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-10-summation-of-primes/index.md @@ -1,5 +1,5 @@ --- -title: Summation of primes +title: 'Problem 10: Summation of primes' --- # Problem 10: Summation of primes diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation-1a1b-p10n/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-one-half-as-a-sum-of-inverse-squares/index.md similarity index 62% rename from guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation-1a1b-p10n/index.md rename to guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-one-half-as-a-sum-of-inverse-squares/index.md index 3c9745346e..3cd4059de7 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation-1a1b-p10n/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-one-half-as-a-sum-of-inverse-squares/index.md @@ -1,9 +1,9 @@ --- -title: Solving the diophantine equation 1/a+1/b= p/10n +title: 'Problem 152: Writing half as a sum of inverse squares' --- -## Problem 157: Solving the diophantine equation 1/a+1/b= p/10n +## Problem 152: Writing one half as a sum of inverse squares -This is a stub. Help our community expand it. +This is a stub. Help our community expand it. This quick style guide will help ensure your pull request gets accepted. diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-12-as-a-sum-of-inverse-squares/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation/index.md similarity index 65% rename from guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-12-as-a-sum-of-inverse-squares/index.md rename to guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation/index.md index c82aa660d0..258d3eb695 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-152-writing-12-as-a-sum-of-inverse-squares/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-157-solving-the-diophantine-equation/index.md @@ -1,9 +1,9 @@ --- -title: Writing 1/2 as a sum of inverse squares +title: 'Problem 157: Solving the diophantine equation' --- -## Problem 152: Writing 1/2 as a sum of inverse squares +## Problem 157: Solving the diophantine equation -This is a stub. Help our community expand it. +This is a stub. Help our community expand it. This quick style guide will help ensure your pull request gets accepted. diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers/index.md index 3ae469cd26..cd8123912f 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers/index.md @@ -1,5 +1,5 @@ --- -title: Even Fibonacci Numbers +title: 'Problem 2: Even Fibonacci Numbers' --- # Problem 2: Even Fibonacci Numbers diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the--areaperimeter-ratio-is-integral/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the-area--perimeter-ratio-is-integral/index.md similarity index 63% rename from guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the--areaperimeter-ratio-is-integral/index.md rename to guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the-area--perimeter-ratio-is-integral/index.md index 6b84a98c2b..8de9568a19 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the--areaperimeter-ratio-is-integral/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-283-integer-sided-triangles-for-which-the-area--perimeter-ratio-is-integral/index.md @@ -1,9 +1,9 @@ --- -title: Integer sided triangles for which the area/perimeter ratio is integral +title: 'Problem 283: Integer sided triangles for which the area * perimeter ratio is integral' --- -## Problem 283: Integer sided triangles for which the area/perimeter ratio is integral +## Problem 283: Integer sided triangles for which the area * perimeter ratio is integral -This is a stub. Help our community expand it. +This is a stub. Help our community expand it. This quick style guide will help ensure your pull request gets accepted. diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-3-largest-prime-factor/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-3-largest-prime-factor/index.md index a37aca9308..5644693fe6 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-3-largest-prime-factor/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-3-largest-prime-factor/index.md @@ -1,5 +1,5 @@ --- -title: Largest prime factor +title: 'Problem 3: Largest prime factor' --- # Problem 3: Largest prime factor diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/index.md index 8441c8a579..ed040288ee 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/index.md @@ -1,5 +1,5 @@ --- -title: Largest palindrome product +title: 'Problem 4: Largest palindrome product' --- # Problem 4: Largest palindrome product diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-7-10001st-prime/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-7-10001st-prime/index.md index 743589f62b..aef551236c 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-7-10001st-prime/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-7-10001st-prime/index.md @@ -1,5 +1,5 @@ --- -title: 10001st prime +title: 'Problem 7: 10001st prime' --- # Problem 7: 10001st prime diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-8-largest-product-in-a-series/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-8-largest-product-in-a-series/index.md index adc45fb968..2b312407bc 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-8-largest-product-in-a-series/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-8-largest-product-in-a-series/index.md @@ -1,5 +1,5 @@ --- -title: Largest product in a series +title: 'Problem 8: Largest product in a series' --- # Problem 8: Largest product in a series diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md index c2b5405c53..c7ebecdd0d 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md @@ -1,5 +1,5 @@ --- -title: Special Pythagorean triplet +title: 'Problem 9: Special Pythagorean triplet' --- # Problem 9: Special Pythagorean triplet diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averages-mode/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-mode/index.md new file mode 100644 index 0000000000..71f3586f33 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-mode/index.md @@ -0,0 +1,35 @@ +--- +title: Averages/Mode +--- +# Averages/Mode + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function mode(arr) { + const counter = {}; + let result = []; + let max = 0; + // for (const i in arr) { + arr.forEach(el => { + if (!(el in counter)) { + counter[el] = 0; + } + counter[el]++; + + if (counter[el] === max) { + result.push(el); + } + else if (counter[el] > max) { + max = counter[el]; + result = [el]; + } + }); + return result; +} +``` + +
diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averages-pythagorean-means/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-pythagorean-means/index.md new file mode 100644 index 0000000000..a85ca5cbdf --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-pythagorean-means/index.md @@ -0,0 +1,76 @@ +--- +title: Averages-Pythagorean means +--- +# Averages-Pythagorean means + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function pythagoreanMeans(rangeArr) { + // arithmeticMean :: [Number] -> Number + const arithmeticMean = xs => + foldl((sum, n) => sum + n, 0, xs) / length(xs); + + // geometricMean :: [Number] -> Number + const geometricMean = xs => + raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs)); + + // harmonicMean :: [Number] -> Number + const harmonicMean = xs => + length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs); + + // GENERIC FUNCTIONS ------------------------------------------------------ + + // A list of functions applied to a list of arguments + // <*> :: [(a -> b)] -> [a] -> [b] + const ap = (fs, xs) => // + Array.prototype.concat(...fs.map(f => // + Array.prototype.concat(...xs.map(x => [f(x)])))); + + // foldl :: (b -> a -> b) -> b -> [a] -> b + const foldl = (f, a, xs) => xs.reduce(f, a); + + // length :: [a] -> Int + const length = xs => xs.length; + + // mapFromList :: [(k, v)] -> Dictionary + const mapFromList = kvs => + foldl((a, [k, v]) => + (a[(typeof k === 'string' && k)] = v, a), {}, kvs); + + // raise :: Num -> Int -> Num + const raise = (n, e) => Math.pow(n, e); +/* + // show :: a -> String + // show :: a -> Int -> String + const show = (...x) => + JSON.stringify.apply( + null, x.length > 1 ? [x[0], null, x[1]] : x + ); +*/ + // zip :: [a] -> [b] -> [(a,b)] + const zip = (xs, ys) => + xs.slice(0, Math.min(xs.length, ys.length)) + .map((x, i) => [x, ys[i]]); + + // TEST ------------------------------------------------------------------- + // mean :: Dictionary + const mean = mapFromList(zip( + ['Arithmetic', 'Geometric', 'Harmonic'], + ap([arithmeticMean, geometricMean, harmonicMean], [ + rangeArr + ]) + )); + + return { + values: mean, + test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric && + mean.Geometric >= mean.Harmonic ? 'yes' : 'no'}` + }; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averages-root-mean-square/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-root-mean-square/index.md new file mode 100644 index 0000000000..0858c4e0ac --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/averages-root-mean-square/index.md @@ -0,0 +1,18 @@ +--- +title: Averages-Root mean square +--- +# Averages-Root mean square + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function rms(arr) { + const sumOfSquares = arr.reduce((s, x) => s + x * x, 0); + return Math.sqrt(sumOfSquares / arr.length); +} +``` + +
diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averagesmode/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averagesmode/index.md deleted file mode 100644 index 367c71abab..0000000000 --- a/guide/english/certifications/coding-interview-prep/rosetta-code/averagesmode/index.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: Averages/Mode ---- -# Averages/Mode - -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - - diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averagespythagorean-means/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averagespythagorean-means/index.md deleted file mode 100644 index 2fc800451a..0000000000 --- a/guide/english/certifications/coding-interview-prep/rosetta-code/averagespythagorean-means/index.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: Averages/Pythagorean means ---- -# Averages/Pythagorean means - -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - - diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/averagesroot-mean-square/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/averagesroot-mean-square/index.md deleted file mode 100644 index f97d1daa23..0000000000 --- a/guide/english/certifications/coding-interview-prep/rosetta-code/averagesroot-mean-square/index.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: Averages/Root mean square ---- -# Averages/Root mean square - -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - - diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/cumulative-standard-deviation/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/cumulative-standard-deviation/index.md new file mode 100644 index 0000000000..b331f6d6b2 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/cumulative-standard-deviation/index.md @@ -0,0 +1,26 @@ +--- +title: Cumulative standard deviation +--- +# Cumulative standard deviation + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function standardDeviation(arr) { + var sum = 0, + sum_sq = 0, + n = arr.length; + arr.forEach(function(e) { + sum += e; + sum_sq += e * e; + }) + + var std_dev=Math.sqrt((sum_sq / n) - Math.pow(sum / n, 2)) + return Math.round(std_dev*1000)/1000; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/cusip/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/cusip/index.md new file mode 100644 index 0000000000..7f79275823 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/cusip/index.md @@ -0,0 +1,40 @@ +--- +title: CUSIP +--- +# CUSIP + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function isCusip(s) { + if (s.length != 9) return false; + var sum = 0; + var ASCII = x => x.charCodeAt(0); + for (var i = 0; i < 7; i++) { + var c = s.charCodeAt(i); + + var v; + if (c >= ASCII('0') && c <= ASCII('9')) { + v = c - 48; + } else if (c >= ASCII('A') && c <= ASCII('Z')) { + v = c - 64; // lower case letters apparently invalid + } else if (c == ASCII('*')) { + v = 36; + } else if (c == ASCII('@')) { + v = 37; + } else if (c == ASCII('#')) { + v = 38; + } else { + return false; + } + if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing + sum += Math.floor(v / 10) + v % 10; + } + return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/cut-a-rectangle/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/cut-a-rectangle/index.md new file mode 100644 index 0000000000..fe168f2310 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/cut-a-rectangle/index.md @@ -0,0 +1,66 @@ +--- +title: Cut a rectangle +--- +# Cut a rectangle + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function cutRectangle(w, h) { + if (w % 2 == 1 && h % 2 == 1) + return; + + var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]]; + + var grid = new Array(h); for (var i = 0; i < grid.length; i++) grid[i]=new Array(w); + var stack = []; + + var half = Math.floor((w * h) / 2); + var bits = Math.pow(2, half) - 1; + var result=0; + for (; bits > 0; bits -= 2) { + + for (var i = 0; i < half; i++) { + var r = Math.floor(i / w); + var c = i % w; + grid[r][c] = (bits & (1 << i)) != 0 ? 1 : 0; + grid[h - r - 1][w - c - 1] = 1 - grid[r][c]; + } + + stack.push(0); + grid[0][0] = 2; + var count = 1; + while (stack.length!=0) { + + var pos = stack.pop(); + var r = Math.floor(pos / w); + var c = pos % w; + + for (var dir of dirs) { + var nextR = r + dir[0]; + var nextC = c + dir[1]; + + if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) { + + if (grid[nextR][nextC] == 1) { + stack.push(nextR * w + nextC); + grid[nextR][nextC] = 2; + count++; + } + } + } + } + + if (count == half) { + result++; + } + } + + return result; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/dot-product/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/dot-product/index.md new file mode 100644 index 0000000000..147a55e5eb --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/dot-product/index.md @@ -0,0 +1,20 @@ +--- +title: Dot product +--- +# Dot product + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function dotProduct(ary1, ary2) { + var dotprod = 0; + for (var i = 0; i < ary1.length; i++) + dotprod += ary1[i] * ary2[i]; + return dotprod; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/generatorexponential/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/generator-exponential/index.md similarity index 100% rename from guide/english/certifications/coding-interview-prep/rosetta-code/generatorexponential/index.md rename to guide/english/certifications/coding-interview-prep/rosetta-code/generator-exponential/index.md diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/k-d-tree/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/k-d-tree/index.md new file mode 100644 index 0000000000..23336c4f15 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/k-d-tree/index.md @@ -0,0 +1,303 @@ +--- +title: K-d tree +--- +# K-d tree + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function kdNN(fpoints, fpoint) { + function Node(obj, dimension, parent) { + this.obj = obj; + this.left = null; + this.right = null; + this.parent = parent; + this.dimension = dimension; + } + + function kdTree(points, metric, dimensions) { + + var self = this; + + function buildTree(points, depth, parent) { + var dim = depth % dimensions.length, + median, + node; + + if (points.length === 0) { + return null; + } + if (points.length === 1) { + return new Node(points[0], dim, parent); + } + + points.sort(function (a, b) { + return a[dimensions[dim]] - b[dimensions[dim]]; + }); + + median = Math.floor(points.length / 2); + node = new Node(points[median], dim, parent); + node.left = buildTree(points.slice(0, median), depth + 1, node); + node.right = buildTree(points.slice(median + 1), depth + 1, node); + + return node; + } + + this.root = buildTree(points, 0, null); + + this.insert = function (point) { + function innerSearch(node, parent) { + + if (node === null) { + return parent; + } + + var dimension = dimensions[node.dimension]; + if (point[dimension] < node.obj[dimension]) { + return innerSearch(node.left, node); + } else { + return innerSearch(node.right, node); + } + } + + var insertPosition = innerSearch(this.root, null), + newNode, + dimension; + + if (insertPosition === null) { + this.root = new Node(point, 0, null); + return; + } + + newNode = new Node(point, (insertPosition.dimension + 1) % dimensions.length, insertPosition); + dimension = dimensions[insertPosition.dimension]; + + if (point[dimension] < insertPosition.obj[dimension]) { + insertPosition.left = newNode; + } else { + insertPosition.right = newNode; + } + }; + + this.nearest = function (point, maxNodes, maxDistance) { + var i, + result, + bestNodes; + + bestNodes = new BinaryHeap( + function (e) { return -e[1]; } + ); + + function nearestSearch(node) { + var bestChild, + dimension = dimensions[node.dimension], + ownDistance = metric(point, node.obj), + linearPoint = {}, + linearDistance, + otherChild, + i; + + function saveNode(node, distance) { + bestNodes.push([node, distance]); + if (bestNodes.size() > maxNodes) { + bestNodes.pop(); + } + } + + for (i = 0; i < dimensions.length; i += 1) { + if (i === node.dimension) { + linearPoint[dimensions[i]] = point[dimensions[i]]; + } else { + linearPoint[dimensions[i]] = node.obj[dimensions[i]]; + } + } + + linearDistance = metric(linearPoint, node.obj); + + if (node.right === null && node.left === null) { + if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) { + saveNode(node, ownDistance); + } + return; + } + + if (node.right === null) { + bestChild = node.left; + } else if (node.left === null) { + bestChild = node.right; + } else { + if (point[dimension] < node.obj[dimension]) { + bestChild = node.left; + } else { + bestChild = node.right; + } + } + + nearestSearch(bestChild); + + if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) { + saveNode(node, ownDistance); + } + + if (bestNodes.size() < maxNodes || Math.abs(linearDistance) < bestNodes.peek()[1]) { + if (bestChild === node.left) { + otherChild = node.right; + } else { + otherChild = node.left; + } + if (otherChild !== null) { + nearestSearch(otherChild); + } + } + } + + if (maxDistance) { + for (i = 0; i < maxNodes; i += 1) { + bestNodes.push([null, maxDistance]); + } + } + + if (self.root) + nearestSearch(self.root); + + result = []; + + for (i = 0; i < Math.min(maxNodes, bestNodes.content.length); i += 1) { + if (bestNodes.content[i][0]) { + result.push([bestNodes.content[i][0].obj, bestNodes.content[i][1]]); + } + } + return result; + }; + } + + function BinaryHeap(scoreFunction) { + this.content = []; + this.scoreFunction = scoreFunction; + } + + BinaryHeap.prototype = { + push: function (element) { + // Add the new element to the end of the array. + this.content.push(element); + // Allow it to bubble up. + this.bubbleUp(this.content.length - 1); + }, + + pop: function () { + // Store the first element so we can return it later. + var result = this.content[0]; + // Get the element at the end of the array. + var end = this.content.pop(); + // If there are any elements left, put the end element at the + // start, and let it sink down. + if (this.content.length > 0) { + this.content[0] = end; + this.sinkDown(0); + } + return result; + }, + + peek: function () { + return this.content[0]; + }, + + size: function () { + return this.content.length; + }, + + bubbleUp: function (n) { + // Fetch the element that has to be moved. + var element = this.content[n]; + // When at 0, an element can not go up any further. + while (n > 0) { + // Compute the parent element's index, and fetch it. + var parentN = Math.floor((n + 1) / 2) - 1, + parent = this.content[parentN]; + // Swap the elements if the parent is greater. + if (this.scoreFunction(element) < this.scoreFunction(parent)) { + this.content[parentN] = element; + this.content[n] = parent; + // Update 'n' to continue at the new position. + n = parentN; + } + // Found a parent that is less, no need to move it further. + else { + break; + } + } + }, + + sinkDown: function (n) { + // Look up the target element and its score. + var length = this.content.length, + element = this.content[n], + elemScore = this.scoreFunction(element); + + while (true) { + // Compute the indices of the child elements. + var child2N = (n + 1) * 2, child1N = child2N - 1; + // This is used to store the new position of the element, + // if any. + var swap = null; + // If the first child exists (is inside the array)... + if (child1N < length) { + // Look it up and compute its score. + var child1 = this.content[child1N], + child1Score = this.scoreFunction(child1); + // If the score is less than our element's, we need to swap. + if (child1Score < elemScore) + swap = child1N; + } + // Do the same checks for the other child. + if (child2N < length) { + var child2 = this.content[child2N], + child2Score = this.scoreFunction(child2); + if (child2Score < (swap == null ? elemScore : child1Score)) { + swap = child2N; + } + } + + // If the element needs to be moved, swap it, and continue. + if (swap != null) { + this.content[n] = this.content[swap]; + this.content[swap] = element; + n = swap; + } + // Otherwise, we are done. + else { + break; + } + } + } + }; + + var dims = [] + + for (var i = 0; i < fpoint.length; i++) dims.push(i) + + var tree = new kdTree(fpoints, function (e1, e2) { + var d = 0; + var e3 = e1; + if (!Array.isArray(e1)) { + e3 = [] + for (var key in e1) + e3.push(e1[key]) + + e1 = e3 + } + e1.forEach(function (e, i) { + var sqd = (e1[i] - e2[i]); + d += sqd * sqd; + }) + return d; + }, dims) + + return tree.nearest(fpoint, 1, 1000)[0][0]; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/kaprekar-numbers/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/kaprekar-numbers/index.md new file mode 100644 index 0000000000..6cb3f46343 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/kaprekar-numbers/index.md @@ -0,0 +1,23 @@ +--- +title: Kaprekar numbers +--- +# Kaprekar numbers + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function isKaprekar(n, bs) { + if (n < 1) return false; + if (n == 1) return true; + for (var a = n * n, b = 0, s = 1; a; s *= bs) { + b += a % bs * s; + a = Math.floor(a / bs); + if (b && a + b == n) return true; + } return false; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/knights-tour/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/knights-tour/index.md new file mode 100644 index 0000000000..5eaf1f66b0 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/knights-tour/index.md @@ -0,0 +1,96 @@ +--- +title: Knight's tour +--- +# Knight's tour + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function knightTour (w, h) { + var b, cnt=0; + + var dx = [ -2, -2, -1, 1, 2, 2, 1, -1 ]; + var dy = [ -1, 1, 2, 2, 1, -1, -2, -2 ]; + + function init_board() + { + var i, j, k, x, y; + // * b is board; a is board with 2 rows padded at each side + + for(i=0;i= 0 && x < w && y >= 0 && y < h) b[i][j]++; + } + } + } + } + + function walk_board(x, y) + { + var i, nx, ny, least; + var steps = 0; + // printf(E"H"E"J"E"%d;%dH"E"32m[]"E"m", y + 1, 1 + 2 * x); + + while (1) { + // * occupy cell + b[y][x] = 255; + + // * reduce all neighbors' neighbor count + for (i = 0; i < 8; i++) + if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w) + b[ y + dy[i] ][ x + dx[i] ]--; + + // find neighbor with lowest neighbor count + least = 255; + for (i = 0; i < 8; i++) { + if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w) + if (b[ y + dy[i] ][ x + dx[i] ] < least) { + nx = x + dx[i]; + ny = y + dy[i]; + least = b[ny][nx]; + } + } + + if (least > 7) { + return steps == w * h - 1; + } + + steps++; + x = nx, y = ny; + } + } + + function solve (x, y) { + b=new Array(h); + for(var i=0;i \ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/largest-int-from-concatenated-ints/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/largest-int-from-concatenated-ints/index.md new file mode 100644 index 0000000000..ff1745a339 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/largest-int-from-concatenated-ints/index.md @@ -0,0 +1,29 @@ +--- +title: Largest int from concatenated ints +--- +# Largest int from concatenated ints + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function maxCombine(xs) { + return parseInt( + xs.sort( + function(x, y) { + var a = x.toString(), + b = y.toString(), + ab = parseInt(a + b), + ba = parseInt(b + a); + + return ab > ba ? -1 : (ab < ba ? 1 : 0); + } + ) + .join(''), 10 + ); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/last-friday-of-each-month/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/last-friday-of-each-month/index.md new file mode 100644 index 0000000000..469208b7f2 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/last-friday-of-each-month/index.md @@ -0,0 +1,25 @@ +--- +title: Last Friday of each month +--- +# Last Friday of each month + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function lastFriday (year, month) { + var i, last_day; + i = 0; + while (true) { + last_day = new Date(year, month, i); + if (last_day.getDay() === 5) { + return last_day.getDate(); + } + i -= 1; + } +}; +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/leap-year/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/leap-year/index.md new file mode 100644 index 0000000000..44fc96cb1e --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/leap-year/index.md @@ -0,0 +1,17 @@ +--- +title: Leap year +--- +# Leap year + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function isLeapYear (year) { + return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/least-common-multiple/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/least-common-multiple/index.md new file mode 100644 index 0000000000..092d3cd2ac --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/least-common-multiple/index.md @@ -0,0 +1,23 @@ +--- +title: Least common multiple +--- +# Least common multiple + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function LCM (A) { + var n = A.length, a = Math.abs(A[0]); + for (var i = 1; i < n; i++) + { var b = Math.abs(A[i]), c = a; + while (a && b){ a > b ? a %= b : b %= a; } + a = Math.abs(c*A[i])/(a+b); + } + return a; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/left-factorials/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/left-factorials/index.md new file mode 100644 index 0000000000..b804ba5cf6 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/left-factorials/index.md @@ -0,0 +1,32 @@ +--- +title: Left factorials +--- +# Left factorials + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function leftFactorial(n) { + if (n == 0) + return 0 + if (n == 1) + return 1; + + // Note: for n>=20, the result may not be correct. + // This is because JavaScript uses 53 bit integers and + // for n>=20 result becomes too large. + + let res = 2, fact = 2; + for (var i = 2; i < n; i++) { + res += fact; + fact *= (i + 1); + } + + return res; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sort-an-array-of-composite-structures/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-an-array-of-composite-structures/index.md new file mode 100644 index 0000000000..bdf28b5415 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-an-array-of-composite-structures/index.md @@ -0,0 +1,19 @@ +--- +title: Sort an array of composite structures +--- +# Sort an array of composite structures + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sortByKey (arr) { + return arr.sort(function(a, b) { + return a.key - b.key + }); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sort-disjoint-sublist/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-disjoint-sublist/index.md new file mode 100644 index 0000000000..e9e3c2690a --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-disjoint-sublist/index.md @@ -0,0 +1,31 @@ +--- +title: Sort disjoint sublist +--- +# Sort disjoint sublist + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sortDisjoint(values, indices) { + let sublist = []; + + indices.sort(function (a, b) { return a - b; }); + + for (let i = 0; i < indices.length; i++) { + sublist.push(values[indices[i]]); + } + + sublist.sort((a, b) => { return a - b; }); + + for (let i = 0; i < indices.length; i++) { + values[indices[i]] = sublist[i]; + } + + return values; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sort-stability/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-stability/index.md new file mode 100644 index 0000000000..96bd1224bc --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-stability/index.md @@ -0,0 +1,18 @@ +--- +title: Sort stability +--- +# Sort stability + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function stableSort(arr) { + arr.sort(function(a, b) { return (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0)) }); + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sort-using-a-custom-comparator/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-using-a-custom-comparator/index.md new file mode 100644 index 0000000000..9d9dcc6000 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sort-using-a-custom-comparator/index.md @@ -0,0 +1,23 @@ +--- +title: Sort using a custom comparator +--- +# Sort using a custom comparator + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function lengthSorter(arr) { + arr.sort(function(a, b) { + var result = b.length - a.length; + if (result == 0) + result = a.localeCompare(b); + return result; + }) + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort/index.md new file mode 100644 index 0000000000..42bb27ed38 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort/index.md @@ -0,0 +1,49 @@ +--- +title: Sorting algorithms/Bead sort +--- +# Sorting algorithms/Bead sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function beadSort(arr) { + var max = 0; + for (var i = 0; i < arr.length; i++) + if (arr[i] > max) + max = arr[i];; + var grid = new Array(arr.length); + for (var i = 0; i < grid.length; i++) { + grid[i] = new Array(max); + } + var levelcount = new Array(max); + levelcount.fill(0) + for (var i = 0; i < max; i++) { + levelcount[i] = 0; + for (var j = 0; j < arr.length; j++) + grid[j][i] = '_'; + }; + for (var i = 0; i < arr.length; i++) { + var num = arr[i]; + for (var j = 0; num > 0; j++) { + grid[levelcount[j]++][j] = '*'; + num--; + }; + }; + var sorted = new Array(arr.length) + sorted.fill(0) + for (var i = 0; i < arr.length; i++) { + var putt = 0; + for (var j = 0; j < max && (function(c) { + return c.charCodeAt == null ? c : c.charCodeAt(0); + })(grid[arr.length - 1 - i][j]) == '*'.charCodeAt(0); j++) + putt++; + sorted[i] = putt; + }; + return sorted; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bogosort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bogosort/index.md new file mode 100644 index 0000000000..99fdd25f0c --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-bogosort/index.md @@ -0,0 +1,35 @@ +--- +title: Sorting algorithms/Bogosort +--- +# Sorting algorithms/Bogosort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function bogosort(v) { + function shuffle(v) { + for (var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); + return v; + }; + + function isSorted(v) { + for (var i = 1; i < v.length; i++) { + if (v[i - 1] > v[i]) { + return false; + } + } + return true; + } + var sorted = false; + while (sorted == false) { + v = shuffle(v); + sorted = isSorted(v); + } + return v; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort/index.md new file mode 100644 index 0000000000..3ec1271f4e --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort/index.md @@ -0,0 +1,43 @@ +--- +title: Sorting algorithms/Cocktail sort +--- +# Sorting algorithms/Cocktail sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function cocktailSort(arr) { + let isSorted = true; + while (isSorted) { + for (let i = 0; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + let temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + isSorted = true; + } + } + + if (!isSorted) + break; + + isSorted = false; + + for (let j = arr.length - 1; j > 0; j--) { + if (arr[j - 1] > arr[j]) { + let temp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = temp; + isSorted = true; + } + } + } + + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort/index.md new file mode 100644 index 0000000000..e3af93066c --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort/index.md @@ -0,0 +1,56 @@ +--- +title: Sorting algorithms/Comb sort +--- +# Sorting algorithms/Comb sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function combSort(arr) { + function is_array_sorted(arr) { + var sorted = true; + for (var i = 0; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + sorted = false; + break; + } + } + return sorted; + } + var iteration_count = 0; + var gap = arr.length - 2; + var decrease_factor = 1.25; + + // Until array is not sorted, repeat iterations + while (!is_array_sorted(arr)) { + // If not first gap + if (iteration_count > 0) + // Calculate gap + gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor); + + // Set front and back elements and increment to a gap + var front = 0; + var back = gap; + while (back <= arr.length - 1) { + // If elements are not ordered swap them + if (arr[front] > arr[back]) { + var temp = arr[front]; + arr[front] = arr[back]; + arr[back] = temp; + } + + // Increment and re-run swapping + front += 1; + back += 1; + } + iteration_count += 1; + } + + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort/index.md new file mode 100644 index 0000000000..ba4bcd1a6b --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort/index.md @@ -0,0 +1,27 @@ +--- +title: Sorting algorithms/Gnome sort +--- +# Sorting algorithms/Gnome sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function gnomeSort(a) { + function moveBack(i) { + for (; i > 0 && a[i - 1] > a[i]; i--) { + var t = a[i]; + a[i] = a[i - 1]; + a[i - 1] = t; + } + } + for (var i = 1; i < a.length; i++) { + if (a[i - 1] > a[i]) moveBack(i); + } + return a; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort/index.md new file mode 100644 index 0000000000..26d59f5d52 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort/index.md @@ -0,0 +1,45 @@ +--- +title: Sorting algorithms/Pancake sort +--- +# Sorting algorithms/Pancake sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function pancakeSort(arr) { + for (var i = arr.length - 1; i >= 1; i--) { + // find the index of the largest element not yet sorted + var max_idx = 0; + var max = arr[0]; + for (var j = 1; j <= i; j++) { + if (arr[j] > max) { + max = arr[j]; + max_idx = j; + } + } + + if (max_idx == i) + continue; // element already in place + + var new_slice; + + // flip arr max element to index 0 + if (max_idx > 0) { + new_slice = arr.slice(0, max_idx + 1).reverse(); + for (var j = 0; j <= max_idx; j++) + arr[j] = new_slice[j]; + } + + // then flip the max element to its place + new_slice = arr.slice(0, i + 1).reverse(); + for (var j = 0; j <= i; j++) + arr[j] = new_slice[j]; + } + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort/index.md new file mode 100644 index 0000000000..354af98536 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort/index.md @@ -0,0 +1,53 @@ +--- +title: Sorting algorithms/Permutation sort +--- +# Sorting algorithms/Permutation sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function permutationSort(arr) { + function pSort(a) { + var list = []; + permute(a, a.length, list); + for (var i = 0; i < list.length; i++) { + var x = list[i]; + if (isSorted(x)) + return x; + } + return a; + }; + + function permute(a, n, list) { + if (n === 1) { + var b = a.slice(); + list.push(b); + return; + } + for (var i = 0; i < n; i++) { + swap(a, i, n - 1); + permute(a, n - 1, list); + swap(a, i, n - 1); + }; + }; + + function isSorted(a) { + for (var i = 1; i < a.length; i++) + if (a[i - 1] > a[i]) + return false;; + return true; + }; + + function swap(arr, i, j) { + var temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + }; + return pSort(arr); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort/index.md new file mode 100644 index 0000000000..3feaa1d310 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort/index.md @@ -0,0 +1,25 @@ +--- +title: Sorting algorithms/Shell sort +--- +# Sorting algorithms/Shell sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function shellSort(a) { + for (var h = a.length; h > 0; h = parseInt(h / 2)) { + for (var i = h; i < a.length; i++) { + var k = a[i]; + for (var j = i; j >= h && k < a[j - h]; j -= h) + a[j] = a[j - h]; + a[j] = k; + } + } + return a; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort/index.md new file mode 100644 index 0000000000..492f6ed262 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort/index.md @@ -0,0 +1,40 @@ +--- +title: Sorting algorithms/Stooge sort +--- +# Sorting algorithms/Stooge sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function stoogeSort(arr) { + function stoogeSortRecurse(array, i, j) { + if (j === undefined) { + j = array.length - 1; + } + + if (i === undefined) { + i = 0; + } + + if (array[j] < array[i]) { + var aux = array[i]; + array[i] = array[j]; + array[j] = aux; + } + + if (j - i > 1) { + var t = Math.floor((j - i + 1) / 3); + stoogeSortRecurse(array, i, j - t); + stoogeSortRecurse(array, i + t, j); + stoogeSortRecurse(array, i, j - t); + } + } + stoogeSortRecurse(arr); + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort/index.md new file mode 100644 index 0000000000..e7686f033d --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort/index.md @@ -0,0 +1,47 @@ +--- +title: Sorting algorithms/Strand sort +--- +# Sorting algorithms/Strand sort + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function strandSort(list) { + + function merge(left, right) { + var result = []; + while (left.length != 0 && right.length != 0) { + if (left[0] <= right[0]) + result.push(left.shift()); + else + result.push(right.shift()); + } + result.push.apply(result, left); + result.push.apply(result, right); + return result; + } + + if (list.length <= 1) return list; + var result = []; + while (list.length > 0) { + var sorted = []; + sorted.push(list.shift()); + var len = list.length; + for (var i = 1; i < len; i++) { + var elem = list[i]; + if (sorted[i - 1] <= elem) { + sorted.push(elem); + sorted.splice(i, 1); + } + } + + result = merge(result, sorted); + } + return result; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/soundex/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/soundex/index.md new file mode 100644 index 0000000000..fb7b615ad2 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/soundex/index.md @@ -0,0 +1,38 @@ +--- +title: Soundex +--- +# Soundex + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function soundex(s) { + var a = s.toLowerCase().split('') + var f = a.shift(), + r = '', + codes = { + a: '', e: '', i: '', o: '', u: '', + b: 1, f: 1, p: 1, v: 1, + c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2, + d: 3, t: 3, + l: 4, + m: 5, n: 5, + r: 6 + }; + r = f + a + .map(function(v, i, a) { + return codes[v] + }) + .filter(function(v, i, a) { + return ((i === 0) ? v !== codes[f] : v !== a[i - 1]); + }) + .join(''); + + return (r + '000').slice(0, 4).toUpperCase(); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/spiral-matrix/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/spiral-matrix/index.md new file mode 100644 index 0000000000..b5c13766d2 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/spiral-matrix/index.md @@ -0,0 +1,31 @@ +--- +title: Spiral matrix +--- +# Spiral matrix + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function spiralArray(n) { + var arr = Array(n), + x = 0, y = n, + total = n * n--, + dx = 1, dy = 0, + i = 0, j = 0; + while (y) arr[--y] = []; + while (i < total) { + arr[y][x] = i++; + x += dx; y += dy; + if (++j == n) { + if (dy < 0) { x++; y++; n -= 2 } + j = dx; dx = -dy; dy = j; j = 0; + } + } + return arr; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/split-a-character-string-based-on-change-of-character/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/split-a-character-string-based-on-change-of-character/index.md new file mode 100644 index 0000000000..482d9c9842 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/split-a-character-string-based-on-change-of-character/index.md @@ -0,0 +1,46 @@ +--- +title: Split a character string based on change of character +--- +# Split a character string based on change of character + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function split(str) { + const concat = xs => + xs.length > 0 ? (() => { + const unit = typeof xs[0] === 'string' ? '' : []; + return unit.concat.apply(unit, xs); + })() : []; + + const group = xs => groupBy((a, b) => a === b, xs); + + const groupBy = (f, xs) => { + const dct = xs.slice(1) + .reduce((a, x) => { + const + h = a.active.length > 0 ? a.active[0] : undefined, + blnGroup = h !== undefined && f(h, x); + return { + active: blnGroup ? a.active.concat([x]) : [x], + sofar: blnGroup ? a.sofar : a.sofar.concat([a.active]) + }; + }, { + active: xs.length > 0 ? [xs[0]] : [], + sofar: [] + }); + return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []); + }; + + const map = (f, xs) => xs.map(f); + + const stringChars = s => s.split(''); + + return map(concat, group(stringChars(str))) +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/state-name-puzzle/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/state-name-puzzle/index.md new file mode 100644 index 0000000000..443b1adbd8 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/state-name-puzzle/index.md @@ -0,0 +1,58 @@ +--- +title: State name puzzle +--- +# State name puzzle + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function solve(input) { + var orig = {}; + input.forEach(function(e) { + orig[e.replace(/\s/g, "").toLowerCase()] = e; + }) + + input = Object.keys(orig) + var map = {}; + for (var i = 0; i < input.length - 1; i++) { + var pair0 = input[i]; + for (var j = i + 1; j < input.length; j++) { + + var pair = [pair0, input[j]]; + var s = pair0 + pair[1]; + var key = s.split("").sort(); + + var val = map[key] ? map[key] : []; + val.push(pair); + map[key] = val; + } + } + + var result = []; + Object.keys(map).forEach((key) => { + + for (var i = 0; i < map[key].length - 1; i++) { + var a = map[key][i]; + for (var j = i + 1; j < map[key].length; j++) { + var b = map[key][j]; + + if ((new Set([a[0], b[0], a[1], b[1]])).size < 4) + continue; + var from = [orig[a[0]], orig[a[1]]].sort() + var to = [orig[b[0]], orig[b[1]]].sort() + result.push({ + from, + to + }) + } + } + }); + + return result; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/stern-brocot-sequence/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/stern-brocot-sequence/index.md new file mode 100644 index 0000000000..48426df310 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/stern-brocot-sequence/index.md @@ -0,0 +1,26 @@ +--- +title: Stern-Brocot sequence +--- +# Stern-Brocot sequence + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sternBrocot(num) { + function f(n) { + return n < 2 ? n : (n & 1) ? f(Math.floor(n / 2)) + f(Math.floor(n / 2 + 1)) : f(Math.floor(n / 2)); + } + + function gcd(a, b) { + return a ? a < b ? gcd(b % a, a) : gcd(a % b, b) : b; + } + var n; + for (n = 1; f(n) != num; n++); + return n; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/straddling-checkerboard/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/straddling-checkerboard/index.md new file mode 100644 index 0000000000..954bc9137e --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/straddling-checkerboard/index.md @@ -0,0 +1,52 @@ +--- +title: Straddling checkerboard +--- +# Straddling checkerboard + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function straddle(message, alphabet) { + var prefixes = new Array("", alphabet[0].indexOf(" "), alphabet[0].lastIndexOf(" ")) + + var out = "" + message = message.toUpperCase() + message = message.replace(/([0-9])/g, "/$1") // dumb way to escape numbers + for (var i = 0; i < message.length; i++) { + var chr = message[i] + if (chr == " ") continue + for (var j = 0; j < 3; j++) { + var k = alphabet[j].indexOf(chr) + if (k < 0) continue + out += prefixes[j].toString() + k + } + if (chr == "/") out += message[++i] + } + return out +} +function unstraddle(message, alphabet) { + var prefixes = new Array("", alphabet[0].indexOf(" "), alphabet[0].lastIndexOf(" ")) + var out = "" + var n, o + for (var i = 0; i < message.length; i++) { + n = message[i] * 1 + switch (n) { + case prefixes[1]: + o = alphabet[1][message[++i]]; + break + case prefixes[2]: + o = alphabet[2][message[++i]]; + break + default: + o = alphabet[0][n] + } + o == "/" ? out += message[++i] : out += o + } + return out +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/stream-merge/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/stream-merge/index.md new file mode 100644 index 0000000000..8411a3ebca --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/stream-merge/index.md @@ -0,0 +1,37 @@ +--- +title: Stream Merge +--- +# Stream Merge + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function mergeLists(lists) { + function merge (l1, l2) { + var result = [], i=0, j=0; + while (l1.length && l2.length) { + if(l1[i]<=l2[j]){ + result.push(l1.shift()); + }else{ + result.push(l2.shift()); + } + } + + result.push.apply(result, l1); + result.push.apply(result, l2); + return result; + } + + var result=lists[0]; + for (var i = 1; i < lists.length; i++) { + result=merge(result, lists[i]); + } + + return result; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/strip-control-codes-and-extended-characters-from-a-string/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/strip-control-codes-and-extended-characters-from-a-string/index.md new file mode 100644 index 0000000000..1f3dda6ef1 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/strip-control-codes-and-extended-characters-from-a-string/index.md @@ -0,0 +1,21 @@ +--- +title: Strip control codes and extended characters from a string +--- +# Strip control codes and extended characters from a string + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function strip(s) { + return s.split('').filter(function(x) { + var n = x.charCodeAt(0); + + return 31 < n && 127 > n; + }).join(''); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/subleq/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/subleq/index.md new file mode 100644 index 0000000000..e1410c7d3d --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/subleq/index.md @@ -0,0 +1,34 @@ +--- +title: Subleq +--- +# Subleq + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function Subleq(mem) { + var out = ""; + var instructionPointer = 0; + do { + var a = mem[instructionPointer]; + var b = mem[instructionPointer + 1]; + if (a === -1) {} else if (b === -1) { + out += String.fromCharCode(mem[a]); + } else { + mem[b] -= mem[a]; + if (mem[b] < 1) { + instructionPointer = mem[instructionPointer + 2]; + continue; + } + } + instructionPointer += 3; + } while ((instructionPointer >= 0)); + + return out; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sudoku/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sudoku/index.md new file mode 100644 index 0000000000..910063f89d --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sudoku/index.md @@ -0,0 +1,194 @@ +--- +title: Sudoku +--- +# Sudoku + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function solveSudoku(puzzle) { + var solution; + + class DoX { + constructor(V, H) { + this.V = V; + this.L = this; + this.R = this; + this.U = this; + this.D = this; + this.S = 1; + this.H = H || this; + H && (H.S += 1); + } + } + + const addRight = (e, n) => { + n.R = e.R; + n.L = e; + e.R.L = n; + return e.R = n; + }; + + const addBelow = (e, n) => { + n.D = e.D; + n.U = e; + e.D.U = n; + return e.D = n; + }; + + const search = function(h, s) { + if (h.R == h) { + printSol(s); + } else { + let c = chooseColumn(h); + cover(c); + for (let r = c.D; r != c; r = r.D) { + s.push(r); + for (let j = r.R; r != j; j = j.R) { + cover(j.H); + } + search(h, s); + r = s.pop(); + for (let j = r.R; j != r; j = j.R) { + uncover(j.H); + } + } + uncover(c); + } + }; + + const chooseColumn = h => { + let s = Number.POSITIVE_INFINITY; + let c = h; + for (let j = h.R; j != h; j = j.R) { + if (j.S < s) { + c = j; + s = j.S; + } + } + return c; + }; + + const cover = c => { + c.L.R = c.R; + c.R.L = c.L; + for (let i = c.D; i != c; i = i.D) { + for (let j = i.R; j != i; j = j.R) { + j.U.D = j.D; + j.D.U = j.U; + j.H.S = j.H.S - 1; + } + } + }; + + const uncover = c => { + for (let i = c.U; i != c; i = i.U) { + for (let j = i.L; i != j; j = j.L) { + j.H.S = j.H.S + 1; + j.U.D = j; + j.D.U = j; + } + } + c.L.R = c; + c.R.L = c; + }; + + const printSol = a => { + solution = a.reduce((p, c) => { + let [i, v] = c.V.split(':'); + p[i * 1] = v; + return p; + }, new Array(a.length).fill('.')); + }; + + const gridMeta = s => { + const g = s.split(''); + const cellCount = g.length; + const tokenCount = Math.sqrt(cellCount); + const N = Math.sqrt(tokenCount); + const g2D = g.map(e => isNaN(e * 1) ? + new Array(tokenCount).fill(1).map((_, i) => i + 1) : [e * 1]); + return [cellCount, N, tokenCount, g2D]; + }; + + const indexesN = n => i => { + let c = Math.floor(i / (n * n)); + i %= n * n; + return [c, i, Math.floor(c / n) * n + Math.floor(i / n)]; + }; + + const reduceGrid = puzString => { + + const [ + numCells, // The total number of cells in a grid (81 for a 9x9 grid) + N, // the 'n' value of the grid. (3 for a 9x9 grid) + U, // The total number of unique tokens to be placed. + g2D // A 2D array representation of the grid, with each element + // being an array of candidates for a cell. Known cells are + // single element arrays. + ] = gridMeta(puzString); + + const getIndex = indexesN(N); + + const headRow = new Array(4 * numCells) + .fill('') + .map((_, i) => new DoX(`H${i}`)); + + let H = new DoX('ROOT'); + headRow.reduce((p, c) => addRight(p, c), H); + + for (let i = 0; i < numCells; i++) { + const [ri, ci, bi] = getIndex(i); + g2D[i].forEach(num => { + let id = `${i}:${num}`; + let candIdx = num - 1; + + // The 4 columns that we will populate. + const A = headRow[i]; + const B = headRow[numCells + candIdx + (ri * U)]; + const C = headRow[(numCells * 2) + candIdx + (ci * U)]; + const D = headRow[(numCells * 3) + candIdx + (bi * U)]; + + // The Row-Column Constraint + let rcc = addBelow(A.U, new DoX(id, A)); + + // The Row-Number Constraint + let rnc = addBelow(B.U, addRight(rcc, new DoX(id, B))); + + // The Column-Number Constraint + let cnc = addBelow(C.U, addRight(rnc, new DoX(id, C))); + + // The Block-Number Constraint + addBelow(D.U, addRight(cnc, new DoX(id, D))); + }); + } + search(H, []); + }; + + var stringPuzzle = ""; + + for (var i = 0; i < puzzle.length; i++) { + puzzle[i].forEach(function(e) { + if (e == -1) + stringPuzzle += "."; + else + stringPuzzle += e; + }) + } + + reduceGrid(stringPuzzle) + + var result = []; + + for (var i = 0; i < 9; i++) { + result.push(solution.slice(i * 9, (i + 1) * 9).map(e => parseInt(e))) + } + + return result; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sum-digits-of-an-integer/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-digits-of-an-integer/index.md new file mode 100644 index 0000000000..7c25b57e57 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-digits-of-an-integer/index.md @@ -0,0 +1,19 @@ +--- +title: Sum digits of an integer +--- +# Sum digits of an integer + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sumDigits(n) { + n += '' + for (var s=0, i=0, e=n.length; i \ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sum-multiples-of-3-and-5/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-multiples-of-3-and-5/index.md new file mode 100644 index 0000000000..88ec0c9aea --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-multiples-of-3-and-5/index.md @@ -0,0 +1,21 @@ +--- +title: Sum multiples of 3 and 5 +--- +# Sum multiples of 3 and 5 + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sumMults(n) { + var sum = 0; + for (var i = 1; i < n; i++) { + if (i % 3 == 0 || i % 5 == 0) sum += i; + } + return sum; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-a-series/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-a-series/index.md new file mode 100644 index 0000000000..130a3ee511 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-a-series/index.md @@ -0,0 +1,22 @@ +--- +title: Sum of a series +--- +# Sum of a series + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sum(a, b) { + function fn(x) { + return 1 / (x * x) + } + var s = 0; + for (; a <= b; a++) s += fn(a); + return s; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-squares/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-squares/index.md new file mode 100644 index 0000000000..b9cb8a2597 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-of-squares/index.md @@ -0,0 +1,23 @@ +--- +title: Sum of squares +--- +# Sum of squares + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sumsq(array) { + var sum = 0; + var i, iLen; + + for (i = 0, iLen = array.length; i < iLen; i++) { + sum += array[i] * array[i]; + } + return sum; +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sum-to-100/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-to-100/index.md new file mode 100644 index 0000000000..978db4451c --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sum-to-100/index.md @@ -0,0 +1,100 @@ +--- +title: Sum to 100 +--- +# Sum to 100 + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function sumTo100(n) { + var permutationsWithRepetition = function(n, as) { + return as.length > 0 ? + foldl1(curry(cartesianProduct)(as), replicate(n, as)) : []; + }; + + var cartesianProduct = function(xs, ys) { + return [].concat.apply([], xs.map(function(x) { + return [].concat.apply([], ys.map(function(y) { + return [ + [x].concat(y) + ]; + })); + })); + }; + + var curry = function(f) { + return function(a) { + return function(b) { + return f(a, b); + }; + }; + }; + + var flip = function(f) { + return function(a, b) { + return f.apply(null, [b, a]); + }; + }; + + var foldl1 = function(f, xs) { + return xs.length > 0 ? xs.slice(1) + .reduce(f, xs[0]) : []; + }; + + var replicate = function(n, a) { + var v = [a], + o = []; + if (n < 1) return o; + while (n > 1) { + if (n & 1) o = o.concat(v); + n >>= 1; + v = v.concat(v); + } + return o.concat(v); + }; + + var asSum = function(xs) { + var dct = xs.reduceRight(function(a, sign, i) { + var d = i + 1; // zero-based index to [1-9] positions + if (sign !== 0) { + // Sum increased, digits cleared + return { + digits: [], + n: a.n + sign * parseInt([d].concat(a.digits) + .join(''), 10) + }; + } else return { // Digits extended, sum unchanged + digits: [d].concat(a.digits), + n: a.n + }; + }, { + digits: [], + n: 0 + }); + return dct.n + ( + dct.digits.length > 0 ? parseInt(dct.digits.join(''), 10) : 0 + ); + }; + + var asString = function(xs) { + var ns = xs.reduce(function(a, sign, i) { + var d = (i + 1) + .toString(); + return sign === 0 ? a + d : a + (sign > 0 ? '+' : '-') + d; + }, ''); + + return ns[0] === '+' ? tail(ns) : ns; + }; + + var universe = permutationsWithRepetition(9, [0, 1, -1]) + .filter(function(x) { + return x[0] !== 1 && asSum(x) === n; + }).map(asString); + return universe.sort() +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/sutherland-hodgman-polygon-clipping/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/sutherland-hodgman-polygon-clipping/index.md new file mode 100644 index 0000000000..85b214a6a1 --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/sutherland-hodgman-polygon-clipping/index.md @@ -0,0 +1,50 @@ +--- +title: Sutherland-Hodgman polygon clipping +--- +# Sutherland-Hodgman polygon clipping + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function clip(subjectPolygon, clipPolygon) { + var cp1, cp2, s, e, i, j; + var inside = function(p) { + return (cp2[0] - cp1[0]) * (p[1] - cp1[1]) > (cp2[1] - cp1[1]) * (p[0] - cp1[0]); + }; + var intersection = function() { + var dc = [cp1[0] - cp2[0], cp1[1] - cp2[1]], + dp = [s[0] - e[0], s[1] - e[1]], + n1 = cp1[0] * cp2[1] - cp1[1] * cp2[0], + n2 = s[0] * e[1] - s[1] * e[0], + n3 = 1.0 / (dc[0] * dp[1] - dc[1] * dp[0]); + return [(n1 * dp[0] - n2 * dc[0]) * n3, (n1 * dp[1] - n2 * dc[1]) * n3]; + }; + var outputList = subjectPolygon; + cp1 = clipPolygon[clipPolygon.length - 1]; + for (j in clipPolygon) { + var cp2 = clipPolygon[j]; + var inputList = outputList; + outputList = []; + s = inputList[inputList.length - 1]; //last on the input list + for (i in inputList) { + var e = inputList[i]; + if (inside(e)) { + if (!inside(s)) { + outputList.push(intersection()); + } + outputList.push(e); + } else if (inside(s)) { + outputList.push(intersection()); + } + s = e; + } + cp1 = cp2; + } + return outputList.map(e => e.map(f => Math.round(f * 1000) / 1000)); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/symmetric-difference/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/symmetric-difference/index.md new file mode 100644 index 0000000000..ade7eeb08f --- /dev/null +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/symmetric-difference/index.md @@ -0,0 +1,34 @@ +--- +title: Symmetric difference +--- +# Symmetric difference + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function symmetricDifference(A, B) { + function relative_complement(A, B) { + return A.filter(function(elem) { + return B.indexOf(elem) == -1 + }); + } + + function unique(ary) { + var u = ary.concat().sort(); + for (var i = 1; i < u.length;) { + if (u[i - 1] === u[i]) + u.splice(i, 1); + else + i++; + } + return u; + } + + return unique(relative_complement(A, B).concat(relative_complement(B, A))).sort(); +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/coding-interview-prep/rosetta-code/vector-dot-product/index.md b/guide/english/certifications/coding-interview-prep/rosetta-code/vector-dot-product/index.md index 3240a40f0d..1e76f78715 100644 --- a/guide/english/certifications/coding-interview-prep/rosetta-code/vector-dot-product/index.md +++ b/guide/english/certifications/coding-interview-prep/rosetta-code/vector-dot-product/index.md @@ -3,8 +3,45 @@ title: Vector dot product --- # Vector dot product -This is a stub. Help our community expand it. +--- +## Solutions -This quick style guide will help ensure your pull request gets accepted. +
Solution 1 (Click to Show/Hide) - +```javascript +function dotProduct(...vectors) { + if (!vectors || !vectors.length) { + return null; + } + if (!vectors[0] || !vectors[0].length) { + return null; + } + const vectorLen = vectors[0].length; + const numVectors = vectors.length; + + // If all vectors not same length, return null + for (let i = 0; i < numVectors; i++) { + if (vectors[i].length !== vectorLen) { + return null; // return undefined + } + } + + let prod = 0; + let sum = 0; + let j = vectorLen; + let i = numVectors; + // Sum terms + while (j--) { + i = numVectors; + prod = 1; + + while (i--) { + prod *= vectors[i][j]; + } + sum += prod; + } + return sum; +} +``` + +
diff --git a/guide/english/certifications/coding-interview-prep/take-home-projects/use-the-twitch-json-api/index.md b/guide/english/certifications/coding-interview-prep/take-home-projects/use-the-twitch-json-api/index.md index 5683ef2014..0c94fe3da0 100644 --- a/guide/english/certifications/coding-interview-prep/take-home-projects/use-the-twitch-json-api/index.md +++ b/guide/english/certifications/coding-interview-prep/take-home-projects/use-the-twitch-json-api/index.md @@ -6,9 +6,9 @@ title: Use the Twitch JSON API --- ## Problem Explanation -Update September 29, 2016: Twitch has changed their API and now requires an API key in order to run queries. If you are using CodePen or GitHub pages to build these, we do not recommend adding an API key to your project for security reasons. +Update December 28, 2018: Twitch has updated their API a few times since we wrote this. [The latest version of their API is here](https://dev.twitch.tv/docs/api/reference/#get-streams). -Instead of using Twitch's API, we recommend hard-coding this JSON into your app as a variable. It is a series of responses for different accounts from Twitch. +Due to a change in conditions on API usage, Twitch.tv requires an API key, but we've built a workaround. Use https://wind-bow.glitch.me/helix instead of twitch's API base URL (i.e. https://api.twitch.tv/helix ) and you'll still be able to get account information, without needing to sign up for an API key. * * * diff --git a/guide/english/certifications/data-visualization/json-apis-and-ajax/get-geolocation-data-to-find-a-users-gps-coordinates/index.md b/guide/english/certifications/data-visualization/json-apis-and-ajax/get-geolocation-data-to-find-a-users-gps-coordinates/index.md index e6ad275e96..e343a6dd95 100644 --- a/guide/english/certifications/data-visualization/json-apis-and-ajax/get-geolocation-data-to-find-a-users-gps-coordinates/index.md +++ b/guide/english/certifications/data-visualization/json-apis-and-ajax/get-geolocation-data-to-find-a-users-gps-coordinates/index.md @@ -3,8 +3,28 @@ title: Get Geolocation Data to Find A User's GPS Coordinates --- # Get Geolocation Data to Find A User's GPS Coordinates -This is a stub. Help our community expand it. +-- +## Hints -This quick style guide will help ensure your pull request gets accepted. +### Hint 1 +Every browser has a built in navigator that can give us this information. - +The navigator will get our user's current longitude and latitude. + +You will see a prompt to allow or block this site from knowing your current location. The challenge can be completed either way, as long as the code is correct. + +By selecting allow you will see the text on the output phone change to your latitude and longitude + +Here's some code that does this: + +```javascript +if (navigator.geolocation) { + + navigator.geolocation.getCurrentPosition(function(position) { + + $("#data").html("latitude: " + position.coords.latitude + "
longitude: " + position.coords.longitude); + + }); + +} +``` diff --git a/guide/english/certifications/data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method/index.md b/guide/english/certifications/data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method/index.md new file mode 100644 index 0000000000..78292a3bea --- /dev/null +++ b/guide/english/certifications/data-visualization/json-apis-and-ajax/get-json-with-the-javascript-fetch-method/index.md @@ -0,0 +1,60 @@ +--- +title: Get JSON with the JavaScript fetch method +--- +# Get JSON with the JavaScript fetch method + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```html + + +

Cat Photo Finder

+

+ The message will go here +

+

+ +

+``` + +
\ No newline at end of file diff --git a/guide/english/certifications/data-visualization/json-apis-and-ajax/pre-filter-json-to-get-the-data-you-need/index.md b/guide/english/certifications/data-visualization/json-apis-and-ajax/pre-filter-json-to-get-the-data-you-need/index.md index 065783b408..fd3ea13690 100644 --- a/guide/english/certifications/data-visualization/json-apis-and-ajax/pre-filter-json-to-get-the-data-you-need/index.md +++ b/guide/english/certifications/data-visualization/json-apis-and-ajax/pre-filter-json-to-get-the-data-you-need/index.md @@ -3,8 +3,74 @@ title: Pre-filter JSON to Get the Data You Need --- # Pre-filter JSON to Get the Data You Need -This is a stub. Help our community expand it. +--- +## Solutions -This quick style guide will help ensure your pull request gets accepted. +
Solution 1 (Click to Show/Hide) - +```html + + +

Cat Photo Finder

+

+ The message will go here +

+

+ +

+``` + +
\ No newline at end of file diff --git a/guide/english/certifications/front-end-libraries/bootstrap/warn-your-users-of-a-dangerous-action-with-btn-danger/index.md b/guide/english/certifications/front-end-libraries/bootstrap/warn-your-users-of-a-dangerous-action-with-btn-danger/index.md index 251ef77454..4b7498dfe4 100644 --- a/guide/english/certifications/front-end-libraries/bootstrap/warn-your-users-of-a-dangerous-action-with-btn-danger/index.md +++ b/guide/english/certifications/front-end-libraries/bootstrap/warn-your-users-of-a-dangerous-action-with-btn-danger/index.md @@ -3,8 +3,72 @@ title: Warn Your Users of a Dangerous Action with btn-danger --- # Warn Your Users of a Dangerous Action with btn-danger -This is a stub. Help our community expand it. +## Problem Explanation +The `btn-danger` class is the button color you'll use to notify users that the button performs a destructive action, such as deleting a cat photo. Note that these buttons still need the `btn` and `btn-block` classes. -This quick style guide will help ensure your pull request gets accepted. +
Solution 1 (Click to Show/Hide) - +```html + + + +
+

CatPhotoApp

+ +

Click here for cat photos.

+ + A cute orange cat lying on its back. + + Three kittens running towards the camera. + + + +

Things cats love:

+
    +
  • cat nip
  • +
  • laser pointers
  • +
  • lasagna
  • +
+

Top 3 things cats hate:

+
    +
  1. flea treatment
  2. +
  3. thunder
  4. +
  5. other cats
  6. +
+
+ + + + + + + +
+
+``` + +
diff --git a/guide/english/certifications/front-end-libraries/jquery/target-even-elements-using-jquery/index.md b/guide/english/certifications/front-end-libraries/jquery/target-even-elements-using-jquery/index.md index b20f4a6813..2df35670eb 100644 --- a/guide/english/certifications/front-end-libraries/jquery/target-even-elements-using-jquery/index.md +++ b/guide/english/certifications/front-end-libraries/jquery/target-even-elements-using-jquery/index.md @@ -3,8 +3,65 @@ title: Target Even Elements Using jQuery --- # Target Even Elements Using jQuery -This is a stub. Help our community expand it. +## Hints -This quick style guide will help ensure your pull request gets accepted. +### Hint # 1 +Here's how you would target all the `odd-numbered` elements with class target and give them classes: - +```javascript +$('.target:odd').addClass('animated shake'); +``` + +This will shake all the even ones: + +```javascript +$('.target:even').addClass("shake"); +``` +The right way to do it would be $ `("button: even")` ... + +## Solutions + +
Solution 1 (Click to Show/Hide) + +```html + + + + +
+

jQuery Playground

+
+
+

#left-well

+
+ + + +
+
+
+

#right-well

+
+ + + +
+
+
+
+``` + +
diff --git a/guide/english/certifications/front-end-libraries/react/render-with-an-if-else-condition/index.md b/guide/english/certifications/front-end-libraries/react/render-with-an-if-else-condition/index.md new file mode 100644 index 0000000000..fff3f3129f --- /dev/null +++ b/guide/english/certifications/front-end-libraries/react/render-with-an-if-else-condition/index.md @@ -0,0 +1,45 @@ +--- +title: Render with an If-Else Condition +--- +# Render with an If-Else Condition + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```jsx +class MyComponent extends React.Component { + constructor(props) { + super(props); + this.state = { + display: true + } + this.toggleDisplay = this.toggleDisplay.bind(this); + } + toggleDisplay() { + this.setState({ + display: !this.state.display + }); + } + render() { + // change code below this line + if (this.state.display) { + return ( +
+ +

Displayed!

+
+ ); + } else { + return ( +
+ +
+ ); + } + } +}; +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript/index.md index 19d46a0e48..c42f916aeb 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript/index.md @@ -17,7 +17,7 @@ JavaScript uses the `/` symbol for division.
Solution 1 (Click to Show/Hide) ```javascript -var quotient = 0.6 / 0.3; //quotient gets the value 2 +var quotient = 4.4 / 2.0; // Fix this line ```
diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers/index.md new file mode 100644 index 0000000000..f6335e5776 --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers/index.md @@ -0,0 +1,25 @@ +--- +title: Use Recursion to Create a Range of Numbers +--- + +# Use Recursion to Create a Range of Numbers + + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +function rangeOfNumbers(startNum, endNum) { + if (endNum - startNum === 0) { + return [startNum]; + } else { + var numbers = rangeOfNumbers(startNum, endNum - 1); + numbers.push(endNum); + return numbers; + } +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject/index.md new file mode 100644 index 0000000000..edd5180fbb --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject/index.md @@ -0,0 +1,25 @@ +--- +title: Complete a Promise with resolve and reject +--- + +# Complete a Promise with resolve and reject + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer represents a response from a server + let responseFromServer; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); +``` + +
diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-javascript-promise/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-javascript-promise/index.md new file mode 100644 index 0000000000..5c4284f3ef --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-javascript-promise/index.md @@ -0,0 +1,17 @@ +--- +title: Create a JavaScript Promise +--- +# Create a JavaScript Promise + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +const makeServerRequest = new Promise((resolve, reject) => { + +}); +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-module-script/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-module-script/index.md new file mode 100644 index 0000000000..2fb102463e --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/create-a-module-script/index.md @@ -0,0 +1,21 @@ +--- +title: Create a Module Script +--- +# Create a Module Script + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```html + + + + + + + +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then/index.md new file mode 100644 index 0000000000..d62a780e65 --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then/index.md @@ -0,0 +1,28 @@ +--- +title: Handle a Fulfilled Promise with then +--- +# Handle a Fulfilled Promise with then + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch/index.md new file mode 100644 index 0000000000..26f7a584db --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch/index.md @@ -0,0 +1,32 @@ +--- +title: Handle a Rejected Promise with catch +--- +# Handle a Rejected Promise with catch + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); + +makeServerRequest.catch(error => { + console.log(error); +}); +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import/index.md new file mode 100644 index 0000000000..190d43c410 --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import/index.md @@ -0,0 +1,19 @@ +--- +title: Reuse Javascript Code Using import +--- +# Reuse Javascript Code Using import + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +import { uppercaseString, lowercaseString } from './string_functions.js'; +// add code above this line + +uppercaseString("hello"); +lowercaseString("WORLD!"); +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/index.md index a35a9a121a..6ef41bb6ea 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/index.md @@ -4,9 +4,26 @@ title: Use Destructuring Assignment to Assign Variables from Objects # Use Destructuring Assignment to Assign Variables from Objects -This is a stub. Help our community expand it. +--- +## Solutions -This quick style guide will help ensure your pull request gets accepted. +
Solution 1 (Click to Show/Hide) -#### More Information: - +```javascript +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// change code below this line + +const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES; + +// change code above this line + +console.log(highToday); // should be 77 +console.log(highTomorrow); // should be 80 +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects/index.md new file mode 100644 index 0000000000..e6eb3f265b --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects/index.md @@ -0,0 +1,29 @@ +--- +title: Use Destructuring Assignment to Extract Values from Objects +--- +# Use Destructuring Assignment to Extract Values from Objects + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// change code below this line + +const { today, tomorrow } = HIGH_TEMPERATURES; + +// change code above this line + +console.log(yesterday) // should be not defined +console.log(today); // should be 77 +console.log(tomorrow); // should be 80 +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block/index.md new file mode 100644 index 0000000000..310ca8c044 --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block/index.md @@ -0,0 +1,21 @@ +--- +title: Use export to Share a Code Block +--- +# Use export to Share a Code Block + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + +```javascript +export const uppercaseString = (string) => { + return string.toUpperCase(); +} + +export const lowercaseString = (string) => { + return string.toLowerCase() +} +``` + +
\ No newline at end of file diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-simple-fields/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand/index.md similarity index 76% rename from guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-simple-fields/index.md rename to guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand/index.md index 4df40fa5ff..e6850b1fa3 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-simple-fields/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand/index.md @@ -1,7 +1,7 @@ --- -title: Write Concise Object Literal Declarations Using Simple Fields +title: Write Concise Object Literal Declarations Using Object Property Shorthand --- -# Write Concise Object Literal Declarations Using Simple Fields +# Write Concise Object Literal Declarations Using Object Property Shorthand --- ## Problem Explanation diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method/index.md index 15037e1f73..1d8abdda16 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method/index.md @@ -17,10 +17,10 @@ Use the `join` method (among others) inside the `sentensify` function to make a --- ## Hints -### Hint1 +### Hint 1 You may need to convert the string to an array first. -### Hint2 +### Hint 2 You may need to use regular expression to split the string. diff --git a/guide/english/certifications/responsive-web-design/applied-accessibility/make-links-navigatable-with-html-access-keys/index.md b/guide/english/certifications/responsive-web-design/applied-accessibility/make-links-navigable-with-html-access-keys/index.md similarity index 100% rename from guide/english/certifications/responsive-web-design/applied-accessibility/make-links-navigatable-with-html-access-keys/index.md rename to guide/english/certifications/responsive-web-design/applied-accessibility/make-links-navigable-with-html-access-keys/index.md diff --git a/guide/english/certifications/responsive-web-design/basic-css/cascading-css-variables/index.md b/guide/english/certifications/responsive-web-design/basic-css/cascading-css-variables/index.md deleted file mode 100644 index 3e743885f0..0000000000 --- a/guide/english/certifications/responsive-web-design/basic-css/cascading-css-variables/index.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: Cascading CSS Variables ---- -# Cascading CSS Variables - ---- -## Problem Explanation -Cascading CSS variables (officially called custom properties) are entities which behave similarly to traditional variables, in that variables allow for data to be stored and updated to reflect new values later2. - -CSS variables are defined to contain specific values and be reused throughout a document. They are set using custom property notation (e.g., `--main-color: black`) and are accessed using the `var()` function (e.g., `color: var(--main-color)`)1. Declare the CSS variable in the `:root` or `body` selectors for global access. - -When maintaining complex CSS documents, it is not only beneficial to use CSS Variables but also smart. When making future updates instead of searching potential hundreds of lines of code, one only needs to update the necessary CSS variable1. - - -**Example Syntax:** - -```css -:root { - --main-bkgnd-color: #00B8CB; -} - -body { - background-color: var(--main-bkgnd-color); - font-family: 'Raleway', Helvetica, sans-serif; -} -``` -Declaring the variable: -```css ---custom-name: value -``` - -Using the variable: -```css -var(--custom-name) -``` - - ---- -## Solutions - -
Solution 1 (Click to Show/Hide) - - -In the ```:root``` selector we need to declare the ```--penguin-belly``` variable and give it the value of ```pink```: - -```css - :root { - --penguin-belly: pink; - } -``` - -#### Relevant Links - -1. [Visit MDN's Cascading CSS Variables page for more information.](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables) -2. [Perna, Maria Antonietta. "A Practical Guide to CSS Variables (Custom Properties)" *sitepoint*. August 01, 2018. Accessed: October 5, 2018](https://www.sitepoint.com/practical-guide-css-variables-custom-properties/) -
diff --git a/guide/english/certifications/responsive-web-design/basic-css/inherit-css-variables/index.md b/guide/english/certifications/responsive-web-design/basic-css/inherit-css-variables/index.md new file mode 100644 index 0000000000..7702927c6f --- /dev/null +++ b/guide/english/certifications/responsive-web-design/basic-css/inherit-css-variables/index.md @@ -0,0 +1,252 @@ +--- +title: Cascading CSS Variables +--- +# Cascading CSS Variables + +--- +## Problem Explanation +Cascading CSS variables (officially called custom properties) are entities which behave similarly to traditional variables, in that variables allow for data to be stored and updated to reflect new values later2. + +CSS variables are defined to contain specific values and be reused throughout a document. They are set using custom property notation (e.g., `--main-color: black`) and are accessed using the `var()` function (e.g., `color: var(--main-color)`)1. Declare the CSS variable in the `:root` or `body` selectors for global access. + +When maintaining complex CSS documents, it is not only beneficial to use CSS Variables but also smart. When making future updates instead of searching potential hundreds of lines of code, one only needs to update the necessary CSS variable1. + + +**Example Syntax:** + +```css +:root { + --main-bkgnd-color: #00B8CB; +} + +body { + background-color: var(--main-bkgnd-color); + font-family: 'Raleway', Helvetica, sans-serif; +} +``` +Declaring the variable: +```css +--custom-name: value +``` + +Using the variable: +```css +var(--custom-name) +``` + + +--- +## Solutions + +
Solution 1 (Click to Show/Hide) + + +In the ```:root``` selector we need to declare the ```--penguin-belly``` variable and give it the value of ```pink```: + +```html + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+``` + +#### Relevant Links + +1. [Visit MDN's Cascading CSS Variables page for more information.](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables) +2. [Perna, Maria Antonietta. "A Practical Guide to CSS Variables (Custom Properties)" *sitepoint*. August 01, 2018. Accessed: October 5, 2018](https://www.sitepoint.com/practical-guide-css-variables-custom-properties/) +