├── .editorconfig ├── .gitignore ├── README.md ├── elixir ├── CodeFights │ └── arcade │ │ └── intro │ │ ├── add.md │ │ ├── adjacent_elements_product.ex │ │ ├── adjacent_elements_product.md │ │ ├── centuryFromYear.ex │ │ ├── centuryFromYear.md │ │ ├── checkIfPalindrome.md │ │ └── check_if_palindrome.ex └── Problems │ └── sum_list.ex ├── golang ├── CodeFights │ └── arcade │ │ ├── addBorder.go │ │ ├── addBorder_test.go │ │ ├── alternateSums.go │ │ ├── alternateSums_test.go │ │ ├── reverseInParentheses.go │ │ └── reverseInParentheses_test.go └── go.mod ├── javascript ├── CodeFights │ ├── Add Two Digits.md │ ├── Candies.md │ ├── Factorial.md │ ├── Find its minimal element.md │ ├── Find the Sum of all its digits.md │ ├── Largest Number.md │ ├── Uber new feature for drivers.md │ ├── arcade │ │ ├── edgeOfTheOcean │ │ │ └── matrixElementsSum.md │ │ ├── intro │ │ │ ├── add.md │ │ │ ├── adjacentElementsProduct.md │ │ │ ├── centuryFromYear.md │ │ │ └── checkPalindrome.md │ │ └── smoothSailing │ │ │ ├── allLongestStrings.md │ │ │ ├── commonCharacterCount.md │ │ │ ├── isLucky.md │ │ │ └── sortByHeight.md │ └── companyChallenge │ │ ├── codeSignal │ │ └── companyBotStrategy.md │ │ └── uber │ │ ├── fancyRide.md │ │ └── fareEstimator.md ├── CodeWars │ ├── Bit Counting.md │ ├── Create Four Letter Birding Codes from Bird Names.md │ ├── Does my number look big in this?.md │ ├── Find the missing letter.md │ ├── Friend or Foe?.md │ ├── Multiples of 3 and 5.md │ ├── Sum of positive.md │ ├── Sum of two lowest positive integers.md │ └── Which are in?.md ├── FreeCodeCamps │ ├── Basic Algorithm Scripting │ │ ├── Caesars Cipher.md │ │ ├── Check for Palindromes.md │ │ ├── Chunky Monkey.md │ │ ├── Confirm the Ending.md │ │ ├── Factorialize a Number.md │ │ ├── Falsy Bouncer.md │ │ ├── Find the Longest Word in a String.md │ │ ├── Mutations.md │ │ ├── Repeat a string repeat a string.md │ │ ├── Return Largest Numbers in Arrays.md │ │ ├── Reverse a String.md │ │ ├── Seek and Destroy.md │ │ ├── Slasher Flick.md │ │ ├── Title Case a Sentence.md │ │ ├── Truncate a string.md │ │ └── Where do I belong.md │ ├── Basic JavaScript │ │ ├── Access Array Data with Indexes.md │ │ ├── Access MultiDimensional Arrays With Indexes.md │ │ ├── Accessing Nested Arrays.md │ │ ├── Accessing Nested Objects.md │ │ ├── Accessing Objects Properties with Bracket Notation.md │ │ ├── Accessing Objects Properties with Variables.md │ │ ├── Accessing Objects Properties with the Dot Operator.md │ │ ├── Add New Properties to a JavaScript Object.md │ │ ├── Add Two Numbers with JavaScript.md │ │ ├── Adding a default option in Switch statements.md │ │ ├── Appending Variables to Strings.md │ │ ├── Assignment with a Returned Value.md │ │ ├── Build JavaScript Objects.md │ │ ├── Chaining If Else Statements.md │ │ ├── Comment Your JavaScript Code.md │ │ ├── Comparison with the Equality Operator.md │ │ ├── Comparison with the Greater Than Operator.md │ │ ├── Comparison with the Greater Than Or Equal To Operator.md │ │ ├── Comparison with the Inequality Operator.md │ │ ├── Comparison with the Less Than Operator.md │ │ ├── Comparison with the Less Than Or Equal To Operator.md │ │ ├── Comparison with the Strict Equality Operator.md │ │ ├── Comparison with the Strict Inequality Operator.md │ │ ├── Comparisons with the Logical And Operator.md │ │ ├── Comparisons with the Logical Or Operator.md │ │ ├── Compound Assignment With Augmented Addition.md │ │ ├── Compound Assignment With Augmented Division.md │ │ ├── Compound Assignment With Augmented Multiplication.md │ │ ├── Compound Assignment With Augmented Subtraction.md │ │ ├── Concatenating Strings with Plus Operator.md │ │ ├── Concatenating Strings with the Plus Equals Operator.md │ │ ├── Constructing Strings with Variables.md │ │ ├── Convert Celsius to Fahrenheit.md │ │ ├── Count Backwards With a For Loop.md │ │ ├── Counting Cards.md │ │ ├── Create Decimal Numbers with JavaScript.md │ │ ├── Declare JavaScript Variables.md │ │ ├── Declare String Variables.md │ │ ├── Decrement a Number with JavaScript.md │ │ ├── Delete Properties from a JavaScript Object.md │ │ ├── Divide One Number by Another with JavaScript.md │ │ ├── Divide one Decimal by Another with JavaScript.md │ │ ├── Escape Sequences in Strings.md │ │ ├── Escaping Literal Quotes in Strings.md │ │ ├── Find Numbers with Regular Expressions.md │ │ ├── Find Whitespace with Regular Expressions.md │ │ ├── Find the Length of a String.md │ │ ├── Finding a Remainder in JavaScript.md │ │ ├── Generate Random Fractions with JavaScript.md │ │ ├── Generate Random Whole Numbers with JavaScript.md │ │ ├── Generate Random Whole Numbers within a Range.md │ │ ├── Global Scope and Functions.md │ │ ├── Global vs Local Scope in Functions.md │ │ ├── Golf Code.md │ │ ├── Increment a Number with JavaScript.md │ │ ├── Introducing Else If Statements.md │ │ ├── Introducing Else Statements.md │ │ ├── Invert Regular Expression Matches with JavaScript.md │ │ ├── Iterate Odd Numbers With a For Loop.md │ │ ├── Iterate Through an Array with a For Loop.md │ │ ├── Iterate with JavaScript For Loops.md │ │ ├── Iterate with JavaScript While Loops.md │ │ ├── Local Scope and Functions.md │ │ ├── Logical Order in If Else Statements.md │ │ ├── Manipulate Arrays With pop.md │ │ ├── Manipulate Arrays With push.md │ │ ├── Manipulate Arrays With shift.md │ │ ├── Manipulate Arrays With unshift.md │ │ ├── Manipulating Complex Objects.md │ │ ├── Modify Array Data With Indexes.md │ │ ├── Multiple Identical Options in Switch Statements.md │ │ ├── Multiply Two Decimals with JavaScript.md │ │ ├── Multiply Two Numbers with JavaScript .md │ │ ├── Nest one Array within Another Array.md │ │ ├── Nesting For Loops.md │ │ ├── Passing Values to Functions with Arguments.md │ │ ├── Profile Lookup.md │ │ ├── Quoting Strings with Single Quotes.md │ │ ├── Record Collection.md │ │ ├── Replacing If Else Chains with Switch.md │ │ ├── Return Early Pattern for Functions.md │ │ ├── Return a Value from a Function with Return.md │ │ ├── Returning Boolean Values from Functions.md │ │ ├── Selecting from many options with Switch Statements.md │ │ ├── Shopping List.md │ │ ├── Sift through Text with Regular Expressions.md │ │ ├── Stand in Line.md │ │ ├── Store Multiple Values in one Variable using JavaScript Arrays.md │ │ ├── Storing Values With The Assignment Operator.md │ │ ├── Subtract One Number from Another with JavaScript .md │ │ ├── Testing Objects for Properties.md │ │ ├── Understand String Immutability.md │ │ ├── Understanding Boolean Values.md │ │ ├── Understanding Case Sensitivity in Variables.md │ │ ├── Understanding Uninitialized Variables.md │ │ ├── Updating Object Properties.md │ │ ├── Use Bracket Notation to Find the First Character in a String.md │ │ ├── Use Bracket Notation to Find the Last Character in a String.md │ │ ├── Use Bracket Notation to Find the Nth Character in a String.md │ │ ├── Use Bracket Notation to Find the NthtoLast Character in a String.md │ │ ├── Use Conditional Logic with If Statements.md │ │ ├── Using Objects for Lookups.md │ │ ├── Word Blanks.md │ │ ├── Write Reusable JavaScript with Functions.md │ │ └── initializingVariablesWithTheAssignmentOperator.md │ ├── Functional Programming │ │ ├── Apply Functional Programming to Convert Strings to URL Slugs.md │ │ ├── Combine an Array into a String Using the join Method.md │ │ ├── Return a Sorted Array Without Changing the Original Array.md │ │ ├── Sort an Array Alphabetically using the sort Method.md │ │ ├── Split a String into an Array Using the split Method.md │ │ └── Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem.md │ ├── Intermediate Algorithm Scripting │ │ ├── Boo Who.md │ │ ├── Convert HTML Entities.md │ │ ├── Diff Two Arrays.md │ │ ├── Missing letters.md │ │ ├── Roman Numeral Converter.md │ │ ├── Search and Replace.md │ │ ├── Sorted Union.md │ │ ├── Sum All Numbers in a Range.md │ │ └── Wherefore art thou.md │ └── Object Oriented and Functional Programming │ │ ├── Concatenate Arrays with concat.md │ │ ├── Condense arrays with reduce.md │ │ ├── Construct JavaScript Objects with Functions.md │ │ ├── Declare JavaScript Objects as Variables.md │ │ ├── Filter Arrays with filter.md │ │ ├── Iterate over Arrays with map.md │ │ ├── Join Strings with join.md │ │ ├── Make Instances of Objects with a Constructor Function.md │ │ ├── Make Object Properties Private.md │ │ ├── Make Unique Objects by Passing Parameters to our Constructor.md │ │ ├── Reverse Arrays with reverse.md │ │ ├── Sort Arrays with sort.md │ │ └── Split Strings with split.md └── Problems │ └── FizzBuzz.md ├── sql └── CodeFights │ ├── Always Leave Table in ORDER │ └── volleyballResults.md │ └── Welcome to the Table │ ├── automaticNotifications.md │ ├── countriesSelection.md │ ├── monthlyScholarships.md │ ├── projectList.md │ └── projectsTeam.md └── swift └── shuffledArray.swift /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = true 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeChallenge 2 | 3 | ## Why this repos ? 4 | 5 | I create this repos for save all my progress whit some code challenge. 6 | I can keep my answer and wrote my thinking process. So in the future I can 7 | comeback here and see my past answers. 8 | 9 | ## Which code challenge you do ? 10 | 11 | - [CodeFights](https://codefights.com) 12 | - [FreeCodeCamps](https://www.freecodecamp.com) 13 | - [CodeWars](https://www.codewars.com/) 14 | 15 | ## Free Code Camps ? 16 | 17 | I already start the Fcc bootcamp but I have stop for take the one on [Thinkful](thinkful.com). 18 | After little reflection I have thinking about restart all the part about JavaScript 19 | for be sure all the basic is really **Assimilate**. 20 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/add.md: -------------------------------------------------------------------------------- 1 | Write a function that returns the sum of two numbers. 2 | 3 | ```elixir 4 | def add(param1, param2) do 5 | param1 + param2 6 | end 7 | ``` 8 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/adjacent_elements_product.ex: -------------------------------------------------------------------------------- 1 | defmodule AdjacentElementsProduct do 2 | def adjacentElementsProduct(inputArray) do 3 | calc(inputArray, []) 4 | end 5 | 6 | def calc([head | tail], results) do 7 | case Enum.count(tail) > 0 do 8 | true -> get_product([head | tail], results) 9 | false -> Enum.max(results) 10 | end 11 | end 12 | 13 | defp get_product([head | tail], results) do 14 | value = head * List.first(tail) 15 | new_results = [value | results] 16 | calc(tail, new_results) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/adjacent_elements_product.md: -------------------------------------------------------------------------------- 1 | Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. 2 | 3 | ```elixir 4 | def adjacentElementsProduct(inputArray) do 5 | calc(inputArray, []) 6 | end 7 | 8 | def calc([head | tail], results) do 9 | case Enum.count(tail) > 0 do 10 | true -> get_product([head | tail], results) 11 | false -> Enum.max(results) 12 | end 13 | end 14 | 15 | defp get_product([head | tail], results) do 16 | value = head * List.first(tail) 17 | new_results = [value | results] 18 | calc(tail, new_results) 19 | end 20 | ``` 21 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/centuryFromYear.ex: -------------------------------------------------------------------------------- 1 | defmodule CenturyFromYear do 2 | def centuryFromYear(year) do 3 | calculCentury(year, 0) 4 | end 5 | 6 | def calculCentury(year, centuryCount) when year <= 0, do: centuryCount 7 | 8 | def calculCentury(year, centuryCount) do 9 | calculCentury(year - 100, centuryCount + 1) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/centuryFromYear.md: -------------------------------------------------------------------------------- 1 | Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc. 2 | 3 | ```elixir 4 | def centuryFromYear(year) do 5 | calculCentury(year, 0) 6 | end 7 | 8 | def calculCentury(year, centuryCount) when year <= 0, do: centuryCount 9 | 10 | def calculCentury(year, centuryCount) do 11 | calculCentury(year - 100, centuryCount + 1) 12 | end 13 | ``` 14 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/checkIfPalindrome.md: -------------------------------------------------------------------------------- 1 | Given the string, check if it is a palindrome. 2 | 3 | ```elixir 4 | def checkPalindrome(inputString) do 5 | inputString == String.reverse(inputString) 6 | end 7 | ``` 8 | -------------------------------------------------------------------------------- /elixir/CodeFights/arcade/intro/check_if_palindrome.ex: -------------------------------------------------------------------------------- 1 | defmodule CheckIfPalindrome do 2 | def checkPalindrome(inputString) do 3 | reverse_string(inputString) == inputString 4 | end 5 | 6 | def reverse_string(str) do 7 | str 8 | |> String.split(~r{}) 9 | |> Enum.reverse 10 | |> Enum.join 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /elixir/Problems/sum_list.ex: -------------------------------------------------------------------------------- 1 | defmodule SumList do 2 | # add initial value do the sum function 3 | def sum(list, acc \\ 0) 4 | # if no list anymore return the accumulator 5 | def sum([], acc), do: acc 6 | # cut the head and tail for let recurse over the with the tail as the list 7 | # and change the accumulator to be the addition of the past value and the head 8 | def sum([head | tail], acc), do: sum(tail, acc + head) 9 | end 10 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/addBorder.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | import "strings" 4 | 5 | // Given a rectangular matrix of characters, add a border of asterisks(*) to it. 6 | 7 | func addBorder(p []string) []string { 8 | var result []string 9 | 10 | ast := "*" 11 | 12 | // get the length of the first item and add 2 who mean asterisks at front and end 13 | l := len(p[0]) + 2 14 | 15 | // we need to have the first element to be the list of asterisks 16 | result = append(result, strings.Repeat(ast, l)) 17 | 18 | for _, h := range p { 19 | result = append(result, ast+h+ast) 20 | } 21 | 22 | // we need to have the last element to be the list of asterisks 23 | result = append(result, strings.Repeat(ast, l)) 24 | 25 | return result 26 | } 27 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/addBorder_test.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func Test_addBorder(t *testing.T) { 9 | type args struct { 10 | picture []string 11 | } 12 | tests := []struct { 13 | name string 14 | args args 15 | want []string 16 | }{ 17 | {name: "['abc','ded']", args: args{picture: []string{"abc", "ded"}}, want: []string{"*****", "*abc*", "*ded*", "*****"}}, 18 | {name: "['wzy**']", args: args{picture: []string{"wzy**"}}, want: []string{"*******", "*wzy***", "*******"}}, 19 | } 20 | for _, tt := range tests { 21 | t.Run(tt.name, func(t *testing.T) { 22 | if got := addBorder(tt.args.picture); !reflect.DeepEqual(got, tt.want) { 23 | t.Errorf("addBorder() = %v, want %v", got, tt.want) 24 | } 25 | }) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/alternateSums.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | // Several people are standing in a row and need to be divided into two teams. 4 | // The first person goes into team 1, the second goes into team 2, 5 | // the third goes into team 1 again, the fourth into team 2, and so on. 6 | // 7 | // You are given an array of positive integers - the weights of the people. 8 | // Return an array of two integers, where the first element is the total weight of 9 | // team 1, and the second element is the total weight of team 2 after the division is complete. 10 | 11 | func alternatingSums(a []int) []int { 12 | var team1, team2 int 13 | 14 | for i, p := range a { 15 | if i%2 == 0 { 16 | team1 += p 17 | } else { 18 | team2 += p 19 | } 20 | } 21 | 22 | return []int{team1, team2} 23 | } 24 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/alternateSums_test.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func Test_alternatingSums(t *testing.T) { 9 | type args struct { 10 | a []int 11 | } 12 | tests := []struct { 13 | name string 14 | args args 15 | want []int 16 | }{ 17 | {name: "[180, 105]", args: args{a: []int{50, 60, 60, 45, 70}}, want: []int{180, 105}}, 18 | {name: "[100, 50]", args: args{a: []int{100, 50}}, want: []int{100, 50}}, 19 | {name: "[80, 0]", args: args{a: []int{80}}, want: []int{80, 0}}, 20 | {name: "[100, 50, 50, 100]", args: args{a: []int{100, 50, 50, 100}}, want: []int{150, 150}}, 21 | } 22 | for _, tt := range tests { 23 | t.Run(tt.name, func(t *testing.T) { 24 | if got := alternatingSums(tt.args.a); !reflect.DeepEqual(got, tt.want) { 25 | t.Errorf("alternatingSums() = %v, want %v", got, tt.want) 26 | } 27 | }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/reverseInParentheses.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | func reverseInParentheses(s string) string { 4 | stack, tmp := make([]rune, 0, len(s)), make([]rune, 0, len(s)) 5 | for _, c := range s { 6 | if c == ')' { 7 | i := len(stack) - 1 8 | tmp = tmp[:0] 9 | for ; stack[i] != '('; i-- { 10 | tmp = append(tmp, stack[i]) 11 | } 12 | stack = append(stack[:i], tmp...) 13 | } else { 14 | stack = append(stack, c) 15 | } 16 | } 17 | return string(stack) 18 | } 19 | -------------------------------------------------------------------------------- /golang/CodeFights/arcade/reverseInParentheses_test.go: -------------------------------------------------------------------------------- 1 | package arcade 2 | 3 | import "testing" 4 | 5 | func Test_reverseInParentheses(t *testing.T) { 6 | type args struct { 7 | inputString string 8 | } 9 | tests := []struct { 10 | name string 11 | args args 12 | want string 13 | }{ 14 | {name: "(bar)", args: args{inputString: "(bar)"}, want: "rab"}, 15 | {name: "foo(bar)baz", args: args{inputString: "foo(bar)baz"}, want: "foorabbaz"}, 16 | {name: "foo(bar)baz(blim)", args: args{inputString: "foo(bar)baz(blim)"}, want: "foorabbazmilb"}, 17 | {name: "foo(bar(baz))blim", args: args{inputString: "foo(bar(baz))blim"}, want: "foobazrabblim"}, 18 | {name: "()", args: args{inputString: "()"}, want: ""}, 19 | {name: "(abc)d(efg)", args: args{inputString: "(abc)d(efg)"}, want: "cbadgfe"}, 20 | } 21 | for _, tt := range tests { 22 | t.Run(tt.name, func(t *testing.T) { 23 | if got := reverseInParentheses(tt.args.inputString); got != tt.want { 24 | t.Errorf("reverseInParentheses() = %v, want %v", got, tt.want) 25 | } 26 | }) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /golang/go.mod: -------------------------------------------------------------------------------- 1 | module codechalenge 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /javascript/CodeFights/Add Two Digits.md: -------------------------------------------------------------------------------- 1 | You are given a two-digit integer n. Return the sum of its digits. 2 | 3 | Example 4 | 5 | For n = 29, the output should be 6 | addTwoDigits(n) = 11. 7 | 8 | Input/Output 9 | 10 | [time limit] 4000ms (js) 11 | [input] integer n 12 | 13 | A positive two-digit integer. 14 | 15 | Constraints: 16 | 10 ≤ n ≤ 99. 17 | 18 | [output] integer 19 | 20 | The sum of the first and second digits of the input number. 21 | 22 | ## My Answer 23 | 24 | ```js 25 | 'use strict' 26 | 27 | function addTwoDigits(n) { 28 | const arr = (n).toString(10).split("").map(Number); 29 | 30 | return arr[0] + arr[1]; 31 | } 32 | 33 | addTwoDigits(25); 34 | ``` 35 | 36 | ## My Thinking 37 | 38 | - I make a array but before I need to make it a string cause we don't want them to add but more concat. 39 | - They told in the task we have only 2 digits so I take 2 index. 40 | -------------------------------------------------------------------------------- /javascript/CodeFights/Candies.md: -------------------------------------------------------------------------------- 1 | n children have got m pieces of candy. They want to eat as much candy as they can, but each child must eat exactly the same amount of candy as any other child. Determine how many pieces of candy will be eaten by all the children together. Individual pieces of candy cannot be split. 2 | 3 | Example 4 | 5 | For n = 3 and m = 10, the output should be 6 | candies(n, m) = 9. 7 | 8 | Each child will eat 3 pieces. So the answer is 9. 9 | 10 | Input/Output 11 | 12 | [time limit] 4000ms (js) 13 | [input] integer n 14 | 15 | The number of children. 16 | 17 | Constraints: 18 | 1 ≤ n ≤ 10. 19 | 20 | [input] integer m 21 | 22 | The number of pieces of candy. 23 | 24 | Constraints: 25 | 2 ≤ m ≤ 100. 26 | 27 | [output] integer 28 | 29 | The total number of pieces of candy the children will eat provided they eat as much as they can and all children eat the same amount. 30 | 31 | ## My Answer 32 | 33 | ```js 34 | function candies(n, m) { 35 | 'use strict'; 36 | 37 | if (m % n === 0) { 38 | return m / n; 39 | } 40 | 41 | const tempSub = m - (m % n); 42 | 43 | const numEach = tempSub / n; 44 | 45 | return numEach * n; 46 | } 47 | 48 | candies(3, 10); 49 | ``` 50 | -------------------------------------------------------------------------------- /javascript/CodeFights/Factorial.md: -------------------------------------------------------------------------------- 1 | # Factorial 2 | 3 | ## Example 4 | 5 | ```javascript 6 | For n = 5, // the output should be 7 | factorial(n) = 120. 8 | 9 | /*Here*/ 1 * 2 * 3 * 4 * 5 = 120. 10 | ``` 11 | 12 | ### Answers 13 | 14 | ```javascript 15 | function factorial(n) { 16 | // If number is less than 0 reject. 17 | if (n < 0) { 18 | return -1; 19 | } else if (n == 0) { // If number 0, return factorial 1. 20 | return 1; 21 | } else { 22 | return (n * factorial(n-1)); 23 | } 24 | } 25 | 26 | factorial(5); 27 | 28 | ``` -------------------------------------------------------------------------------- /javascript/CodeFights/Find its minimal element.md: -------------------------------------------------------------------------------- 1 | # Find its minimal element 2 | 3 | - Given an array of integers, find its minimal element. 4 | 5 | ## Example 6 | 7 | ```javascript 8 | For inputArray = [19, 32, 11, 23], the output should be 9 | arrayMinimum(inputArray) = 11. 10 | ``` 11 | 12 | ```javascript 13 | function arrayMinimum(inputArray) { // inputArray = [19, 32, 11, 23] 14 | var indexOfMinimum = 0; // Create the var indexOfMinimum. 15 | 16 | for (var i = 1; i < inputArray.length; i++) { // i is my number inside the array. 17 | if (inputArray[i] < inputArray[indexOfMinimum]) { 18 | indexOfMinimum = i; // That give me the index of the smaller one. 19 | } 20 | } 21 | 22 | return inputArray[indexOfMinimum]; 23 | } 24 | 25 | arrayMinimum([19, 32, 11, 23]); // Result: 11 26 | 27 | ``` 28 | ### Thinking 29 | 30 | Here I need to find which number is the smaller inside that array. 31 | 32 | 1. I create the var you gonna accept the index of the smaller. 33 | 2. I do a for loop for this given array. 34 | 3. If the number is smaller than the last one return the index of this one 35 | 4. Finally return the array with the index for give the number appropriate to this index 36 | -------------------------------------------------------------------------------- /javascript/CodeFights/Find the Sum of all its digits.md: -------------------------------------------------------------------------------- 1 | # Find the Sum of all its digits 2 | 3 | - Given an integer, find the sum of all its digits. 4 | 5 | ## Example 6 | ```javascript 7 | For n = 111, // the output should be 8 | digitSum(n) = 3. 9 | 10 | 1 + 1 + 1 = 3. 11 | ``` 12 | 13 | ### Answers 14 | 15 | ```javascript 16 | function digitSum(n) { 17 | var myNum = n.toString().split(''); // Result [ '6', '6', '6' ] 18 | var sum = 0; // Create the var sum 19 | 20 | for (var i = 0; i < myNum.length; i++) { // i is my digits 21 | var x = Number(myNum[i]); // String to Number 22 | 23 | sum += x; // Addition the digits 24 | } 25 | return sum; 26 | } 27 | 28 | digitSum(666); // Result: 18 29 | ``` 30 | 31 | ### Thinking 32 | 33 | Here I need to addition every digits inside a number. 34 | 35 | 1. I put the number inside a string and after I can split it and he become an array. 36 | 2. I setup the var sum for use it later. 37 | 3. I do a for loop for my array I just create so now I can use the .length. 38 | 4. I need to convert the string to a number for make a addition 39 | 5. Finally I return the sum. 40 | 41 | -------------------------------------------------------------------------------- /javascript/CodeFights/Largest Number.md: -------------------------------------------------------------------------------- 1 | Given an integer n, return the largest number that contains exactly n digits. 2 | 3 | Example 4 | 5 | For n = 2, the output should be 6 | largestNumber(n) = 99. 7 | 8 | Input/Output 9 | 10 | [time limit] 4000ms (js) 11 | [input] integer n 12 | 13 | Constraints: 14 | 1 ≤ n ≤ 7. 15 | 16 | [output] integer 17 | 18 | The largest integer of length n. 19 | 20 | ## My Answer 21 | 22 | ```js 23 | 'use strict'; 24 | 25 | function largestNumber(n) { 26 | return Math.pow(10, n) - 1; 27 | } 28 | 29 | largestNumber(2); 30 | ``` 31 | 32 | ## My Thinking 33 | 34 | - I use the `Math.pow()` you mean exponential the given number. 35 | - I subtract 1 cause 9 it's the biggest number. 36 | -------------------------------------------------------------------------------- /javascript/CodeFights/Uber new feature for drivers.md: -------------------------------------------------------------------------------- 1 | # Uber new feature for drivers 2 | 3 | Uber is introducing a new feature for drivers that tells them if they'll 4 | need gas within the next hour. It analyzes all of the drive distances 5 | (in miles) that the driver has traveled in the past 6 | 12 hours to make the recommendation. 7 | 8 | - Given the current gas level (in gallons), the drive data, and the 9 | average mileage of the vehicle, calculate the average amount of gas 10 | spent per hour and return true if the driver is likely to need a refill 11 | in the next hour, false otherwise. 12 | 13 | > Assume that the driver will need more gas if the average gas consumption per hour is greater than the amount of gas they have at the given moment. The average mileage is measured in miles per gallon and it shows how many miles a vehicle can travel on one gallon of gas. 14 | 15 | ## Example 16 | 17 | ```javascript 18 | For driveDistances = [12, 6, 17, 5, 20], currentGasLevel = 0.25 and avgMileage = 25, the output should be 19 | gasPrediction(driveDistances, currentGasLevel, avgMileage) = false. 20 | 21 | Since for driveDistances = [12, 6, 17, 5, 20], currentGasLevel = 0.25g and avgMileage = 25mpg, the total distance traveled is equal to 60 and it can be shown that on average the driver spends 0.2g of gas per hour, which is less than 0.25g. 22 | ``` 23 | 24 | ### Answers 25 | 26 | ```javascript 27 | function gasPrediction(driveDistances, currentGasLevel, avgMileage) { 28 | 29 | var totalDistance = 0; 30 | for (var i = 0; i < driveDistances.length; i++) { 31 | totalDistance += driveDistances[i]; 32 | } 33 | var gasConsumed = totalDistance / avgMileage; 34 | 35 | return currentGasLevel < gasConsumed / 12; 36 | } 37 | ``` 38 | 39 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/edgeOfTheOcean/matrixElementsSum.md: -------------------------------------------------------------------------------- 1 | After they became famous, the CodeBots all decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms. Each cell in the matrix contains an integer that represents the price of the room. Some rooms are free (their cost is 0), but that's probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots to live in. 2 | 3 | Help the bots calculate the total price of all the rooms that are suitable for them. 4 | 5 | Example 6 | 7 | For 8 | ``` 9 | matrix = [[0, 1, 1, 2], 10 | [0, 5, 0, 0], 11 | [2, 0, 3, 3]] 12 | ``` 13 | the output should be 14 | matrixElementsSum(matrix) = 9. 15 | 16 | 17 | ```js 18 | function matrixElementsSum(matrix) { 19 | for(let i = 0; i < matrix.length; i++) { 20 | for(let x = 0; x < matrix[0].length; x++){ 21 | if (matrix[i][x] === 0 && i < matrix.length - 1) { 22 | matrix[i + 1][x] = 0; 23 | } 24 | } 25 | } 26 | 27 | return matrix.reduce((prev, current) => { 28 | current.forEach(el => { 29 | prev += el 30 | }) 31 | 32 | return prev 33 | }, 0) 34 | } 35 | ``` 36 | 37 | 1. We need to loop over each array first. 38 | 2. If element === 0 and the second array exist we put this element as 0 39 | 3. We sum all the value to a single one 40 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/intro/add.md: -------------------------------------------------------------------------------- 1 | Write a function that returns the sum of two numbers. 2 | 3 | ```js 4 | function add(param1, param2) { 5 | return param1 + param2; 6 | } 7 | ``` 8 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/intro/adjacentElementsProduct.md: -------------------------------------------------------------------------------- 1 | Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. 2 | 3 | ```js 4 | function adjacentElementsProduct(inputArray) { 5 | let maxAdjacent = inputArray[0] * inputArray[1]; 6 | 7 | for (let i = 1; i < inputArray.length - 1; i++) { 8 | if (getAdjacent(inputArray, i) > maxAdjacent) { 9 | maxAdjacent = getAdjacent(inputArray, i); 10 | } 11 | } 12 | 13 | return maxAdjacent; 14 | } 15 | 16 | function getAdjacent(inputArray, index) { 17 | return inputArray[index] * inputArray[index + 1]; 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/intro/centuryFromYear.md: -------------------------------------------------------------------------------- 1 | Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc. 2 | 3 | ```js 4 | function centuryFromYear(year) { 5 | let centuryCount = 0; 6 | while (year > 0){ 7 | year = year - 100; 8 | centuryCount = centuryCount + 1; 9 | } 10 | return centuryCount; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/intro/checkPalindrome.md: -------------------------------------------------------------------------------- 1 | Given the string, check if it is a palindrome. 2 | 3 | ```js 4 | function checkPalindrome(inputString) { 5 | let rvStr = inputString.split('').reverse().join(''); 6 | 7 | let isPalindrome = false; 8 | 9 | if (rvStr === inputString) { 10 | isPalindrome = true; 11 | } 12 | 13 | return isPalindrome; 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/smoothSailing/allLongestStrings.md: -------------------------------------------------------------------------------- 1 | Given an array of strings, return another array containing all of its longest strings. 2 | 3 | Example 4 | 5 | For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be 6 | allLongestStrings(inputArray) = ["aba", "vcd", "aba"]. 7 | 8 | ```js 9 | function allLongestStrings(inputArray) { 10 | let longest = 0; 11 | 12 | for (let i = 0; i < inputArray.length; i++) { 13 | if (inputArray[i].length > longest) { 14 | longest = inputArray[i].length 15 | } 16 | } 17 | 18 | return inputArray.filter(el => el.length === longest); 19 | } 20 | ``` 21 | 22 | 1. We need first the length number of the biggest number. I do this by looping each element and get the bigger one 23 | 2. We filter each element to get those who have same length as the biggest one 24 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/smoothSailing/commonCharacterCount.md: -------------------------------------------------------------------------------- 1 | Given two strings, find the number of common characters between them. 2 | 3 | Example 4 | 5 | For s1 = "aabcc" and s2 = "adcaa", the output should be 6 | commonCharacterCount(s1, s2) = 3. 7 | 8 | Strings have 3 common characters - 2 "a"s and 1 "c". 9 | 10 | 11 | ```js 12 | function commonCharacterCount(s1, s2) { 13 | let amount = 0; 14 | 15 | const arr1 = s1.split(''); 16 | const arr2 = s2.split(''); 17 | 18 | for (let i = 0; i < arr1.length; i++) { 19 | const index = arr2.indexOf(arr1[i]); 20 | if (index !== -1) { 21 | arr2.splice(index, 1); 22 | amount += 1 23 | } 24 | } 25 | 26 | return amount 27 | } 28 | 29 | ``` 30 | 31 | 1. First split both string in array. 32 | 2. Loop over the first one and get the index of it if this match 33 | 3. if match add 1 to the amount variable and remove the index this way we dont count in double 34 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/smoothSailing/isLucky.md: -------------------------------------------------------------------------------- 1 | Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. 2 | 3 | Given a ticket number n, determine if it's lucky or not. 4 | 5 | Example 6 | 7 | For n = 1230, the output should be 8 | isLucky(n) = true; 9 | For n = 239017, the output should be 10 | isLucky(n) = false. 11 | 12 | ```js 13 | function isLucky(n) { 14 | const arr = String(n).split(''); 15 | 16 | const firstHalf = sum(arr.slice(0, arr.length / 2)); 17 | const secondHalf = sum(arr.slice(arr.length / 2)); 18 | 19 | return firstHalf === secondHalf; 20 | } 21 | 22 | function sum(arr) { 23 | return arr.reduce((prev, current) => { 24 | const _current = parseInt(current, 0); 25 | 26 | return prev += _current 27 | }, 0) 28 | } 29 | ``` 30 | 31 | 1. Split the n to an array and make sure this is a String first. 32 | 2. Create a function sum who take and array and use reduce to get a sum, make all element a number first before addition them. 33 | 3. return true or false if both value equal 34 | -------------------------------------------------------------------------------- /javascript/CodeFights/arcade/smoothSailing/sortByHeight.md: -------------------------------------------------------------------------------- 1 | Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. 2 | 3 | Example 4 | 5 | For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be 6 | sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190] 7 | 8 | ```js 9 | function sortByHeight(a) { 10 | const nums = a.filter(el => el !== -1).sort((a, b) => a - b); 11 | const result = []; 12 | 13 | for (let i = 0; i < a.length; i++) { 14 | if (a[i] === -1) { 15 | result.push(-1); 16 | } else { 17 | result.push(nums[0]); 18 | nums.splice(0, 1); 19 | } 20 | } 21 | 22 | return result; 23 | } 24 | ``` 25 | 26 | 1. We create an array who have each number who is not -1 plus also this one get sort already 27 | 2. We create an array who gonna receive the order we need 28 | 3. we loop over the argument a and we check if this is === -1. 29 | 4. if this is equal we push -1 right away 30 | 5. if not we push the first element in the nums array who is the filter one and after we remove this element from this one. So we dont have in double. 31 | -------------------------------------------------------------------------------- /javascript/CodeFights/companyChallenge/uber/fareEstimator.md: -------------------------------------------------------------------------------- 1 | Uber is building a Fare Estimator that can tell you how much your ride will cost before you request it. It works by passing approximated ride distance and ride time through this formula: 2 | 3 | (Cost per minute) * (ride time) + (Cost per mile) * (ride distance) 4 | 5 | where Cost per minute and Cost per mile depend on the car type. 6 | 7 | You are one of the engineers building the Fare Estimator, so knowing cost per minute and cost per mile for each car type, as well as ride distance and ride time, return the fare estimates for all car types. 8 | 9 | Example 10 | 11 | For 12 | ride_time = 30, 13 | ride_distance = 7, 14 | cost_per_minute = [0.2, 0.35, 0.4, 0.45], and 15 | cost_per_mile = [1.1, 1.8, 2.3, 3.5], the output should be 16 | fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile) = [13.7, 23.1, 28.1, 38]. 17 | 18 | Since: 19 | 20 | 30 * 0.2 + 7 * 1.1 = 6 + 7.7 = 13.7 21 | 30 * 0.35 + 7 * 1.8 = 10.5 + 12.6 = 23.1 22 | 30 * 0.4 + 7 * 2.3 = 12 + 16.1 = 28.1 23 | 30 * 0.45 + 7 * 3.5 = 13.5 + 24.5 = 38 24 | 25 | ## Result 26 | 27 | ```js 28 | function fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile) { 29 | const estimates = []; 30 | 31 | for (let i = 0; i < cost_per_minute.length; i++) { 32 | const currentMinute = cost_per_minute[i] 33 | const currentMile = cost_per_mile[i] 34 | 35 | const result = (ride_time * currentMinute) + (ride_distance * currentMile) 36 | 37 | estimates.push(result); 38 | } 39 | 40 | return estimates; 41 | } 42 | ``` 43 | 44 | Nothing much to say, just plain old looping and * value and pushing those inside a new array. 45 | -------------------------------------------------------------------------------- /javascript/CodeWars/Bit Counting.md: -------------------------------------------------------------------------------- 1 | Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number. 2 | 3 | Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case 4 | 5 | ```js 6 | var countBits = function(n) { 7 | // make an array with the bit result 8 | const base = (n).toString(2).split(''); 9 | 10 | // make a sum with the array and make the index a Number 11 | const result = base.reduce((sum, num) => sum + Number(num), 0); 12 | 13 | return result; 14 | }; 15 | ``` 16 | -------------------------------------------------------------------------------- /javascript/CodeWars/Does my number look big in this?.md: -------------------------------------------------------------------------------- 1 | A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits. 2 | 3 | For example, take 153 (3 digits): 4 | ``` 5 | 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 6 | ``` 7 | and 1634 (4 digits): 8 | ``` 9 | 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 10 | ``` 11 | The Challenge: 12 | 13 | Your code must return true or false depending upon whether the given number is a Narcissistic number. 14 | 15 | Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function. 16 | 17 | ### Thinking 18 | 19 | 1. I make an array with the value. But I need to transform it to string first to make it happen. I build an array so I can iterate over each element. 20 | 2. I create a variable `_result` who is a data store of the value. 21 | 3. Loop over each value and need to transform back the value to a number so I can make some Math with it. 22 | 4. I use Math.pow you give me the power of a number. The second arguments I use is the length of the value. So if the number is `123` the pow become `1^3 2^3 3^3` 23 | 5. I make sure to add the value to the _result data store. 24 | 6. We need to return only a boolean about if this is match or not. 25 | 26 | ### Solution 27 | 28 | ```js 29 | function narcissistic(value) { 30 | const _value = String(value).split(''); 31 | 32 | let _result = 0; 33 | 34 | for (ch of _value) { 35 | const num = parseInt(ch, 0) 36 | 37 | _result += Math.pow(num, _value.length); 38 | } 39 | 40 | return _result === value; 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /javascript/CodeWars/Friend or Foe?.md: -------------------------------------------------------------------------------- 1 | Make a program that filters a list of strings and returns a list with only your friends name in it. 2 | 3 | If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... 4 | 5 | Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"] 6 | 7 | Note: keep the original order of the names in the output. 8 | 9 | ### Thinking 10 | 11 | Pretty simple stuff to make here. We just need to filter each name and returning only those who have the length === 4. 12 | 13 | ### Solution 14 | 15 | ```js 16 | function friend(friends){ 17 | return friends.filter(el => el.length === 4); 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /javascript/CodeWars/Multiples of 3 and 5.md: -------------------------------------------------------------------------------- 1 | If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 2 | 3 | Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. 4 | 5 | > Note: If the number is a multiple of both 3 and 5, only count it once. 6 | 7 | ```js 8 | function solution(number){ 9 | let arr = []; 10 | let multiple = []; 11 | 12 | // push all num between 0 and the number to arr 13 | for (let i = 0; i < number; i++) { 14 | arr.push(i); 15 | } 16 | 17 | // if number in arr is a multiple or 3 or 5 push it to multiple 18 | for (let i = 0; i < arr.length; i++) { 19 | if (i % 3 === 0 || i % 5 === 0) { 20 | multiple.push(i); 21 | } 22 | } 23 | 24 | // sum the total of the multiple array 25 | const sumTotal = multiple.reduce((sum, num) => sum + num, 0); 26 | 27 | return sumTotal; 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /javascript/CodeWars/Sum of positive.md: -------------------------------------------------------------------------------- 1 | You get an array of numbers, return the sum of all of the positives ones. 2 | 3 | Example [1,-4,7,12] => 1 + 7 + 12 = 20 4 | 5 | Note: array may be empty, in this case return 0. 6 | 7 | ``` js 8 | function positiveSum(arr) { 9 | return arr.reduce((prev, current) => { 10 | if (current > 0) { 11 | return prev + current; 12 | } 13 | 14 | return prev 15 | }, 0) 16 | } 17 | ``` 18 | 19 | 1. Sum using reduce who start a 0 20 | 2. if the current is bigger than 0 than mean is position so add the value to the prev one 21 | 3. if not return the prev 22 | -------------------------------------------------------------------------------- /javascript/CodeWars/Sum of two lowest positive integers.md: -------------------------------------------------------------------------------- 1 | Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No floats or empty arrays will be passed. 2 | 3 | For example, when an array is passed like [19,5,42,2,77], the output should be 7. 4 | 5 | [10,343445353,3453445,3453545353453] should return 3453455. 6 | 7 | Hint: Do not modify the original array. 8 | -------------------------------------------------------------------------------- /javascript/CodeWars/Which are in?.md: -------------------------------------------------------------------------------- 1 | Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2. 2 | 3 | #Example 1: a1 = ["arp", "live", "strong"] 4 | 5 | a2 = ["lively", "alive", "harp", "sharp", "armstrong"] 6 | 7 | returns ["arp", "live", "strong"] 8 | 9 | #Example 2: a1 = ["tarp", "mice", "bull"] 10 | 11 | a2 = ["lively", "alive", "harp", "sharp", "armstrong"] 12 | 13 | returns [] 14 | 15 | Notes: 16 | Arrays are written in "general" notation. See "Your Test Cases" for examples in your language. 17 | 18 | In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas. 19 | 20 | Beware: r must be without duplicates. 21 | 22 | ### Thinking 23 | 24 | 1. I create a array store who receive the match 25 | 2. Loop over both array and push inside the match array those who array2 include the value of the array1 element 26 | 3. I filter each element to make sure they are unique by using indexOf. This way the only one who pass is the first value. 27 | 4. I finish with the sort function. I could have make it one line but I want to make sure all of the value is lowerCase. 28 | 29 | ### Solution 30 | 31 | ```js 32 | function inArray(array1,array2){ 33 | const _match = [] 34 | 35 | for (let i = 0; i < array2.length; i++) { 36 | for (let x = 0; x < array1.length; x++) { 37 | const a1El = array1[x]; 38 | const a2El = array2[i]; 39 | if (a2El.includes(a1El)) { 40 | _match.push(a1El) 41 | } 42 | } 43 | } 44 | 45 | return _match.filter((el, i, a) => a.indexOf(el) === i).sort((a, b) => { 46 | const _a = a.toLowerCase(); 47 | const _b = b.toLowerCase(); 48 | 49 | if (_a < _b) { 50 | return -1; 51 | } 52 | 53 | return 1; 54 | }) 55 | } 56 | ``` 57 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Caesars Cipher.md: -------------------------------------------------------------------------------- 1 | # Caesars Cipher 2 | 3 | One of the simplest and most widely known ciphers is a 4 | Caesar cipher, also known as a shift cipher. In a shift cipher 5 | the meanings of the letters are shifted by some set amount. 6 | 7 | A common modern use is the ROT13 cipher, where the values of the 8 | letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on. 9 | 10 | Write a function which takes a ROT13 encoded string as input 11 | and returns a decoded string. 12 | 13 | All letters will be uppercase. Do not transform any non-alphabetic 14 | character (i.e. spaces, punctuation), but do pass them on. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | function rot13(str) { // LBH QVQ VG! 20 | 21 | return str; 22 | } 23 | 24 | // Change the inputs below to test 25 | rot13("SERR PBQR PNZC"); 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | function rot13(str) { // LBH QVQ VG! 32 | var arr = []; 33 | 34 | for (var i = 0; i < str.length; i++) { 35 | if (str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90) { 36 | arr.push(str.charAt(i)); 37 | console.log(arr); 38 | } else if (str.charCodeAt(i) > 77) { 39 | arr.push(String.fromCharCode(str.charCodeAt(i) - 13)); 40 | } else { 41 | arr.push(String.fromCharCode(str.charCodeAt(i) + 13)); 42 | } 43 | } 44 | 45 | return arr.join(""); 46 | } 47 | 48 | // Change the inputs below to test 49 | rot13("SERR CVMMN!"); 50 | ``` 51 | 52 | ### Thinking 53 | 54 | 1. Setup a global empty arr 55 | 2. We loop the args 56 | 3. We check for getting only the alphanumeric 57 | 4. We know the code need to be inside 65 - 90 58 | 5. We transform it to be a String from the char code 59 | 6. Because it's a array of letter we join it -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Check for Palindromes.md: -------------------------------------------------------------------------------- 1 | # Check for Palindromes 2 | 3 | Return true if the given string is a palindrome. Otherwise, return false. 4 | 5 | A palindrome is a word or sentence that's spelled the same 6 | way both forward and backward, ignoring punctuation, case, and spacing. 7 | 8 | ## Note 9 | *You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.* 10 | 11 | We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others. 12 | 13 | We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2". 14 | 15 | ### Before 16 | 17 | ```javascript 18 | function palindrome(str) { 19 | // Good luck! 20 | return true; 21 | } 22 | 23 | palindrome("eye"); 24 | ``` 25 | 26 | ### Answers 27 | 28 | ```javascript 29 | function palindrome(str) { 30 | // Good luck! 31 | 32 | var newStr = str.replace(/[\W_]/g, '').toLowerCase(); 33 | 34 | var strReverse = newStr.split('').reverse().join(''); 35 | 36 | return newStr === strReverse; 37 | } 38 | 39 | palindrome("race car"); 40 | ``` 41 | 42 | ### Thinking 43 | 44 | 1. Create a new var who is the str cleaner and lowercase 45 | 2. Create a new var who take the newStr and transform it into a array for 46 | reverse it and transform back into string 47 | 3. Check if the two var are equal -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Chunky Monkey.md: -------------------------------------------------------------------------------- 1 | # Chunky Monkey 2 | 3 | Write a function that splits an array (first argument) into groups 4 | the length of size (second argument) and returns them as a 5 | two-dimensional array. 6 | 7 | ### Before 8 | 9 | ```javascript 10 | function chunkArrayInGroups(arr, size) { 11 | // Break it up. 12 | return arr; 13 | } 14 | 15 | chunkArrayInGroups(["a", "b", "c", "d"], 2); 16 | ``` 17 | 18 | ### Answers 19 | 20 | ```javascript 21 | function chunkArrayInGroups(arr, size) { 22 | var masterArr = []; 23 | 24 | var item = 0; 25 | 26 | while(item < arr.length) { 27 | masterArr.push(arr.slice(item, item += size)); 28 | } 29 | 30 | return masterArr; 31 | 32 | } 33 | 34 | chunkArrayInGroups(["a", "b", "c", "d"], 2); 35 | ``` 36 | 37 | ### Thinking 38 | 39 | 1. Setup a empty arr and and var 0 40 | 2. I use a while loop who gonna stop when the item size gonna be bigger 41 | than the arr size 42 | 3. I push the slice to the arr and I use the item for be the start index inside 43 | my slice and I sum it with the size. 44 | 45 | Because of the for loop the item size growth with the size args so I can do the 46 | loop until nothing left -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Confirm the Ending.md: -------------------------------------------------------------------------------- 1 | # Confirm the Ending 2 | 3 | Check if a string (first argument, str) ends with the given 4 | target string (second argument, target). 5 | 6 | This challenge can be solved with the `.endsWith()` method, which was 7 | introduced in ES2015. But for the purpose of this challenge, 8 | we would like you to use one of the JavaScript substring methods instead. 9 | 10 | ### Before 11 | 12 | ```javascript 13 | // "Never give up and good luck will find you." 14 | // -- Falcor 15 | return str; 16 | } 17 | 18 | confirmEnding("Bastian", "n"); 19 | ``` 20 | 21 | ### Answers 22 | 23 | ```javascript 24 | function confirmEnding(str, target) { 25 | var newStr = ''; 26 | 27 | newStr = str.substring(str.length - target.length); 28 | 29 | return newStr === target; 30 | 31 | } 32 | 33 | confirmEnding("Bastian", "n"); 34 | ``` 35 | 36 | ### Thinking 37 | 38 | 1. We setup a empty var for get the answer 39 | 2. We give to this var the value of the str and we get the last index of the string 40 | with substring. 41 | 3. If the newStr !== the target with return false -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Factorialize a Number.md: -------------------------------------------------------------------------------- 1 | # Factorialize a Number 2 | 3 | Return the factorial of the provided `integer`. 4 | 5 | If the `integer` is represented with the letter `n`, a factorial is 6 | the product of all positive integers less than or equal to `n`. 7 | 8 | Factorials are often represented with the shorthand notation n! 9 | 10 | For example:` 5! = 1 * 2 * 3 * 4 * 5 = 120` 11 | 12 | ### Before 13 | 14 | ```javascript 15 | function factorialize(num) { 16 | return num; 17 | } 18 | 19 | factorialize(5); 20 | ``` 21 | 22 | ### Answers 23 | 24 | ```javascript 25 | function factorialize(num) { 26 | if (num < 0) { 27 | return -1; 28 | } else if (num === 0) { 29 | return 1; 30 | } else { 31 | return (num * (factorialize(num -1))); 32 | } 33 | } 34 | 35 | factorialize(5); 36 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Falsy Bouncer.md: -------------------------------------------------------------------------------- 1 | # Falsy Bouncer 2 | 3 | Remove all `falsy` values from an array. 4 | 5 | `Falsy` values in JavaScript are `false`, `null`, `0`, `""`, `undefined`, and `NaN`. 6 | 7 | ### Before 8 | 9 | ```javascript 10 | function bouncer(arr) { 11 | // Don't show a false ID to this bouncer. 12 | return arr; 13 | } 14 | 15 | bouncer([7, "ate", "", false, 9]); 16 | ``` 17 | 18 | ### Answer 19 | 20 | ```javascript 21 | function bouncer(arr) { 22 | 23 | var check = arr.filter(function(i) { 24 | return Boolean(i); 25 | }); 26 | 27 | return check; 28 | } 29 | 30 | bouncer([7, "ate", "", false, 9]); 31 | ``` 32 | 33 | ### Thinking 34 | 35 | 1. Make a var you gonna get the filter item who is not falsy 36 | 2. Return this var -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Find the Longest Word in a String.md: -------------------------------------------------------------------------------- 1 | # Find the Longest Word in a String 2 | 3 | Return the length of the longest word in the provided sentence. 4 | 5 | Your response should be a number. 6 | 7 | ### Before 8 | 9 | ```javascript 10 | function findLongestWord(str) { 11 | return str.length; 12 | } 13 | 14 | findLongestWord("The quick brown fox jumped over the lazy dog"); 15 | ``` 16 | 17 | ### Answers 18 | 19 | ```javascript 20 | function findLongestWord(str) { 21 | 22 | var arr = str.split(' '); 23 | 24 | var max = 0; 25 | 26 | for (var i = 0; i < arr.length; i++) { 27 | var valStr = arr[i].length; 28 | 29 | if (valStr > max) { 30 | max = valStr; 31 | } 32 | } 33 | return max; 34 | } 35 | 36 | findLongestWord("The quick brown fox jumped over the lazy dog"); 37 | ``` 38 | 39 | ### Thinking 40 | 41 | 1. We create a arr with the str 42 | 2. Setup a var for tracked the length of the bigger 43 | 3. Loop inside the arr 44 | 4. Setup a var for shortcut the length 45 | 5. If the new one is bigger than the prev one make it the new bigger -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Mutations.md: -------------------------------------------------------------------------------- 1 | # Mutations 2 | 3 | Return `true` if the string in the first element of the array 4 | contains all of the letters of the string in the second 5 | element of the array. 6 | 7 | For example, `["hello", "Hello"]`, should return true because all of 8 | the letters in the second string are present in the first, ignoring case. 9 | 10 | The arguments `["hello", "hey"]` should return false because the string 11 | "hello" does not contain a "y". 12 | 13 | Lastly, ["Alien", "line"], should return true because all of the 14 | letters in "line" are present in "Alien". 15 | 16 | ### Before 17 | 18 | ```javascript 19 | function mutation(arr) { 20 | return arr; 21 | } 22 | 23 | mutation(["hello", "hey"]); 24 | ``` 25 | 26 | ### Answers 27 | 28 | ```javascript 29 | function mutation(arr) { 30 | var item1 = arr[0].toLowerCase(); 31 | var item2 = arr[1].toLowerCase(); 32 | 33 | for (var i = 0; i < item2.length; i++) { 34 | var match = item1.indexOf(item2[i]); 35 | 36 | if (match === -1) { 37 | return false; 38 | } 39 | } 40 | 41 | return true; 42 | } 43 | 44 | mutation(["hello", "hey"]); 45 | ``` 46 | 47 | ### Thinking 48 | 49 | 1. We need to lower case the two array 50 | 2. We loop the second array 51 | 3. We setup a var who take the number giving by th method `indexOf()` 52 | 4. If we got `-1` than mean we don't have the letter both side -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Repeat a string repeat a string.md: -------------------------------------------------------------------------------- 1 | # Repeat a string repeat a string 2 | 3 | Repeat a given string (first argument) num times (second argument). 4 | Return an empty string if num is not a positive number. 5 | 6 | ### Before 7 | 8 | ```javascript 9 | function repeatStringNumTimes(str, num) { 10 | // repeat after me 11 | return str; 12 | } 13 | 14 | repeatStringNumTimes("abc", 3); 15 | ``` 16 | 17 | ### Answers 18 | 19 | ```javascript 20 | function repeatStringNumTimes(str, num) { 21 | 22 | var newStr = ''; 23 | 24 | if (num < 0) return newStr; 25 | 26 | newStr = str.repeat(num); 27 | 28 | return newStr; 29 | 30 | } 31 | 32 | repeatStringNumTimes("abc", 3); 33 | ``` 34 | 35 | ### Thinking 36 | 37 | 1. If the number is negative we return the var newStr who is a empty string 38 | 2. I use the `.repeat()` method for multiply the string with the given number 39 | 40 | ### Links 41 | 42 | [.repeat()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Return Largest Numbers in Arrays.md: -------------------------------------------------------------------------------- 1 | # Return Largest Numbers in Arrays 2 | 3 | Return an array consisting of the largest number 4 | from each provided sub-array. For simplicity, the provided array 5 | will contain exactly 4 sub-arrays. 6 | 7 | Remember, you can iterate through an array with a simple for 8 | loop, and access each member with array syntax `arr[i]`. 9 | 10 | ### Before 11 | 12 | ```javascript 13 | function largestOfFour(arr) { 14 | // You can do this! 15 | return arr; 16 | } 17 | 18 | largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); 19 | ``` 20 | 21 | ### Answers 22 | 23 | ```javascript 24 | function largestOfFour(arr) { 25 | 26 | var arrMax = []; 27 | 28 | for (var i = 0; i < arr.length; i++) { 29 | 30 | var max = 0; 31 | 32 | for (var x = 0; x < arr[i].length; x++) { 33 | if (arr[i][x] > max) { 34 | max = arr[i][x]; 35 | } 36 | } 37 | 38 | arrMax.push(max); 39 | } 40 | 41 | return arrMax; 42 | 43 | } 44 | 45 | largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); 46 | ``` 47 | 48 | ### Thinking 49 | 50 | 1. We setup the empty arr who we gonna push the answer 51 | 2. We for loop the arr and we setup a var max for this one for get 52 | the bigger number for his array 53 | 3. We for loop in the sub array and we check for the bigger one 54 | 4. We push all the bigger number inside your array -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Reverse a String.md: -------------------------------------------------------------------------------- 1 | # Reverse a String 2 | 3 | Reverse the provided string. 4 | 5 | You may need to turn the string into an array before you can reverse it. 6 | 7 | Your result must be a string. 8 | 9 | ### Before 10 | 11 | ```javascript 12 | function reverseString(str) { 13 | return str; 14 | } 15 | 16 | reverseString("hello"); 17 | ``` 18 | 19 | ### Answers 20 | 21 | ```javascript 22 | function reverseString(str) { 23 | return str.split('').reverse().join(''); 24 | } 25 | 26 | reverseString("hello"); 27 | ``` 28 | 29 | ### Thinking 30 | 31 | 1. We take the str and we make it a array 32 | 2. We reverse it 33 | 3. And finally we join it for be a string back -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Seek and Destroy.md: -------------------------------------------------------------------------------- 1 | # Seek and Destroy 2 | 3 | You will be provided with an initial array (the first argument in 4 | the destroyer function), followed by one or more arguments. 5 | Remove all elements from the initial array that are of the 6 | same value as these arguments. 7 | 8 | ### Before 9 | 10 | ```javascript 11 | function destroyer(arr) { 12 | // Remove all the values 13 | return arr; 14 | } 15 | 16 | destroyer([1, 2, 3, 1, 2, 3], 2, 3); 17 | ``` 18 | 19 | ### Answers 20 | 21 | ```javascript 22 | function destroyer(arr) { 23 | var check = []; 24 | 25 | for (var i = 1; i < arguments.length; i++) { 26 | check.push(arguments[i]); 27 | } 28 | 29 | var itemsNotDestroy = arguments[0].filter(function(item) { 30 | var itemPass = true; 31 | for (var i = 0; i < check.length; i++) { 32 | if (item === check[i]) { 33 | itemPass = false; 34 | } 35 | } 36 | return itemPass; 37 | }); 38 | 39 | return itemsNotDestroy; 40 | } 41 | 42 | destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3); 43 | ``` 44 | 45 | ### Thinking 46 | 47 | 1. We create a arr you take the arguments we remove 48 | 2. We filter the item in the arguments 0 and filter it 49 | 3. We just return the one who pass the filter -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Slasher Flick.md: -------------------------------------------------------------------------------- 1 | # Slasher Flick 2 | 3 | Return the remaining elements of an array after chopping 4 | off `n` elements from the head. 5 | 6 | The head means the beginning of the array, or the zeroth index. 7 | 8 | ### Before 9 | 10 | ```javascript 11 | function slasher(arr, howMany) { 12 | // it doesn't always pay to be first 13 | return arr; 14 | } 15 | 16 | slasher([1, 2, 3], 2); 17 | ``` 18 | 19 | ### Answers 20 | 21 | ```javascript 22 | function slasher(arr, howMany) { 23 | var chop = []; 24 | 25 | chop = arr.splice(0, howMany); 26 | 27 | return arr; 28 | } 29 | 30 | slasher([1, 2, 3], 2); 31 | ``` 32 | 33 | ### Thinking 34 | 35 | 1. Setup empty array 36 | 2. Use this array for get the item we remove 37 | 3. Return the arr who don't have anymore this item -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Title Case a Sentence.md: -------------------------------------------------------------------------------- 1 | # Title Case a Sentence 2 | 3 | Return the provided `string` with the first letter of each 4 | word capitalized. Make sure the rest of the word is in lower case. 5 | 6 | For the purpose of this exercise, you should also 7 | capitalize connecting words like "the" and "of". 8 | 9 | ### Before 10 | 11 | ```javascript 12 | function titleCase(str) { 13 | return str; 14 | } 15 | 16 | titleCase("I'm a little tea pot"); 17 | ``` 18 | 19 | ### Answers 20 | 21 | ```javascript 22 | function titleCase(str) { 23 | 24 | var arr = str.split(' '); 25 | 26 | var newStr = ''; 27 | 28 | for (var i = 0; i < arr.length; i++) { 29 | var lower = arr[i].toLowerCase(); 30 | newStr += lower.charAt(0).toUpperCase() + lower.slice(1) + ' '; 31 | } 32 | 33 | return newStr.trim(); 34 | } 35 | 36 | titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"); 37 | ``` 38 | 39 | ### Thinking 40 | 41 | 1. We transform the str into a arr; 42 | 2. We setup a empty var; 43 | 3. We loop inside the arr 44 | 4. We put all word to lower case 45 | 5. We change only the first letter to upper case 46 | 6. We need to `trim()` the last whitespace -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Truncate a string.md: -------------------------------------------------------------------------------- 1 | # Truncate a string 2 | 3 | Truncate a string (first argument) if it is longer than the 4 | given maximum string length (second argument). Return the 5 | truncated string with a `...` ending. 6 | 7 | Note that inserting the three dots to the end will add to the string length. 8 | 9 | However, if the given maximum string length num is 10 | less than or equal to 3, then the addition of the three 11 | dots does not add to the string length in determining the truncated string. 12 | 13 | ### Before 14 | 15 | ```javascript 16 | function truncateString(str, num) { 17 | // Clear out that junk in your trunk 18 | return str; 19 | } 20 | 21 | truncateString("A-tisket a-tasket A green and yellow basket", 11); 22 | ``` 23 | 24 | ### Answers 25 | 26 | ```javascript 27 | function truncateString(str, num) { 28 | var newStr = ''; 29 | 30 | if (num <= 3) { 31 | newStr = str.slice(0, num) + '...'; 32 | } else if (str.length > num) { 33 | newStr = str.slice(0, num - 3) + '...'; 34 | } else { 35 | return str; 36 | } 37 | 38 | return newStr; 39 | } 40 | 41 | truncateString("Absolutely Longer", 2); 42 | ``` 43 | 44 | ### Thinking 45 | 46 | 1. If the num is equal or less than 3 we `slice()` and add the `...` 47 | 2. If the str is bigger the num we slice with but we need to remove 3 48 | cause of the dot 49 | 3. Else we touch nothing -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic Algorithm Scripting/Where do I belong.md: -------------------------------------------------------------------------------- 1 | # Where do I belong 2 | 3 | Return the lowest index at which a value (second argument) should be 4 | inserted into an array (first argument) once it has been sorted. 5 | The returned value should be a number. 6 | 7 | For example, getIndexToIns(`[1,2,3,4], 1.5`) should return 1 because it 8 | is greater than 1 (index 0), but less than 2 (index 1). 9 | 10 | Likewise, getIndexToIns(`[20,3,5], 19`) should return 2 because once the 11 | array has been sorted it will look like [3,5,20] and 19 is less than 12 | 20 (index 2) and greater than 5 (index 1). 13 | 14 | ### Before 15 | 16 | ```javascript 17 | function getIndexToIns(arr, num) { 18 | // Find my place in this sorted array. 19 | return num; 20 | } 21 | 22 | getIndexToIns([40, 60], 50); 23 | ``` 24 | 25 | ### Answers 26 | 27 | ```javascript 28 | function getIndexToIns(arr, num) { 29 | 30 | if(arr.indexOf(num) === -1) { 31 | arr.push(num); 32 | } 33 | 34 | var newArr = arr; 35 | 36 | function sortNumber(a,b) { 37 | return a - b; 38 | } 39 | 40 | newArr.sort(sortNumber); 41 | 42 | return newArr.indexOf(num); 43 | 44 | } 45 | 46 | getIndexToIns([3, 10, 5], 3); 47 | ``` 48 | 49 | ### Thinking 50 | 51 | 1. Check if the args num is inside the arr. If yes we don't add it in the arr 52 | 2. We create a function who gonna be a helpers to your sort cause of problem 53 | with double digits number 54 | 3. We sort the array 55 | 4. Return the `indexOf` of the args num -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Access Array Data with Indexes.md: -------------------------------------------------------------------------------- 1 | # Access Array Data with Indexes 2 | 3 | We can access the data inside arrays using `indexes`. 4 | 5 | Array indexes are written in the same bracket notation that strings use, 6 | except that instead of specifying a character, they are specifying an 7 | entry in the array. Like strings, arrays use `zero-based` indexing, so 8 | the first element in an array is element 0. 9 | 10 | ## Example 11 | 12 | ```javascript 13 | var array = [1,2,3]; 14 | array[0]; // equals 1 15 | var data = array[1]; // equals 2 16 | ``` 17 | 18 | ## Instructions 19 | - Create a variable called `myData` and set it to equal the first 20 | value of `myArray` using `bracket notation`. 21 | 22 | ### Before 23 | 24 | ```javascript 25 | // Example 26 | var ourArray = [1,2,3]; 27 | var ourData = ourArray[0]; // equals 1 28 | 29 | // Setup 30 | var myArray = [1,2,3]; 31 | 32 | // Only change code below this line. 33 | ``` 34 | 35 | ### Answers 36 | 37 | ```javascript 38 | // Example 39 | var ourArray = [1,2,3]; 40 | var ourData = ourArray[0]; // equals 1 41 | 42 | // Setup 43 | var myArray = [1,2,3]; 44 | 45 | // Only change code below this line. 46 | var myData = myArray[0]; 47 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Access MultiDimensional Arrays With Indexes.md: -------------------------------------------------------------------------------- 1 | # Access MultiDimensional Arrays With Indexes 2 | 3 | One way to think of a `multi-dimensional` array, is as an array of 4 | arrays. When you use brackets to access your array, the first set of 5 | bracket refers to the entries in the outer-most (the first level) array, 6 | and each additional pair of brackets refers to the next level of entries 7 | inside. 8 | 9 | ## Example 10 | 11 | ```javascript 12 | var arr = [ 13 | [1,2,3], 14 | [4,5,6], 15 | [7,8,9], 16 | [[10,11,12], 13, 14] 17 | ]; 18 | arr[3]; // equals [[10,11,12], 13, 14] 19 | arr[3][0]; // equals [10,11,12] 20 | arr[3][0][1]; // equals 11 21 | ``` 22 | 23 | ## Instructions 24 | - Using bracket notation select an element from `myArray` such that 25 | `myData` is equal to 8. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | // Setup 31 | var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]]; 32 | 33 | // Only change code below this line. 34 | var myData = myArray[0][0]; 35 | ``` 36 | 37 | ### Answers 38 | 39 | ```javascript 40 | // Setup 41 | var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]]; 42 | 43 | // Only change code below this line. 44 | var myData = myArray[2][1]; 45 | ``` 46 | 47 | ### Thinking 48 | 49 | Pretty straight-forward we just need to think about the first bracket 50 | notation is the outer array and the second one is inside. Don't forget we 51 | start counting at 0. -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Accessing Nested Arrays.md: -------------------------------------------------------------------------------- 1 | # Accessing Nested Arrays 2 | 3 | As we have seen in earlier examples, `objects` can contain 4 | both `nested objects` and `nested arrays`. Similar to accessing `nested objects`, 5 | `Array` `bracket notation` can be chained to access nested arrays. 6 | 7 | Here is an example of how to access a nested array: 8 | 9 | ```javascript 10 | var ourPets = [ 11 | { 12 | animalType: "cat", 13 | names: [ 14 | "Meowzer", 15 | "Fluffy", 16 | "Kit-Cat" 17 | ] 18 | }, 19 | { 20 | animalType: "dog", 21 | names: [ 22 | "Spot", 23 | "Bowser", 24 | "Frankie" 25 | ] 26 | } 27 | ]; 28 | ourPets[0].names[1]; // "Fluffy" 29 | ourPets[1].names[0]; // "Spot" 30 | ``` 31 | 32 | ## Instructions 33 | - Retrieve the second tree from the variable `myPlants` using `object` `dot` and `array bracket notation`. 34 | 35 | ### Before 36 | 37 | ```javascript 38 | // Setup 39 | var myPlants = [ 40 | { 41 | type: "flowers", 42 | list: [ 43 | "rose", 44 | "tulip", 45 | "dandelion" 46 | ] 47 | }, 48 | { 49 | type: "trees", 50 | list: [ 51 | "fir", 52 | "pine", 53 | "birch" 54 | ] 55 | } 56 | ]; 57 | 58 | // Only change code below this line 59 | 60 | var secondTree = ""; // Change this line 61 | ``` 62 | 63 | ### Answers 64 | 65 | ```javascript 66 | // Setup 67 | var myPlants = [ 68 | { 69 | type: "flowers", 70 | list: [ 71 | "rose", 72 | "tulip", 73 | "dandelion" 74 | ] 75 | }, 76 | { 77 | type: "trees", 78 | list: [ 79 | "fir", 80 | "pine", 81 | "birch" 82 | ] 83 | } 84 | ]; 85 | 86 | // Only change code below this line 87 | 88 | var secondTree = myPlants[1].list[1]; // Change this line 89 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Accessing Nested Objects.md: -------------------------------------------------------------------------------- 1 | # Accessing Nested Objects 2 | 3 | The sub-properties of objects can be accessed by chaining together the `dot` or `bracket notation`. 4 | 5 | Here is a nested object: 6 | 7 | ```javascript 8 | var ourStorage = { 9 | "desk": { 10 | "drawer": "stapler" 11 | }, 12 | "cabinet": { 13 | "top drawer": { 14 | "folder1": "a file", 15 | "folder2": "secrets" 16 | }, 17 | "bottom drawer": "soda" 18 | } 19 | } 20 | ourStorage.cabinet["top drawer"].folder2; // "secrets" 21 | ourStorage.desk.drawer; // "stapler" 22 | ``` 23 | 24 | ## Instructions 25 | - Access the `myStorage` object to retrieve the contents of the glove box. Use `bracket notation` for properties with a space in their name. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | // Setup 31 | var myStorage = { 32 | "car": { 33 | "inside": { 34 | "glove box": "maps", 35 | "passenger seat": "crumbs" 36 | }, 37 | "outside": { 38 | "trunk": "jack" 39 | } 40 | } 41 | }; 42 | 43 | // Only change code below this line 44 | 45 | var gloveBoxContents = ""; // Change this line 46 | ``` 47 | 48 | ### Answers 49 | 50 | ```javascript 51 | // Setup 52 | var myStorage = { 53 | "car": { 54 | "inside": { 55 | "glove box": "maps", 56 | "passenger seat": "crumbs" 57 | }, 58 | "outside": { 59 | "trunk": "jack" 60 | } 61 | } 62 | }; 63 | 64 | // Only change code below this line 65 | 66 | var gloveBoxContents = myStorage.car.inside["glove box"]; // Change this line 67 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Accessing Objects Properties with Bracket Notation.md: -------------------------------------------------------------------------------- 1 | # Accessing Objects Properties with Bracket Notation 2 | 3 | The second way to access the properties of an `object` is `bracket notation` (`[]`). 4 | If the property of the `object` you are trying to access has a `space` in it, 5 | you will need to use `bracket notation`. 6 | 7 | Here is a sample of using `bracket notation` to read an object property: 8 | 9 | ```javascript 10 | var myObj = { 11 | "Space Name": "Kirk", 12 | "More Space": "Spock" 13 | }; 14 | myObj["Space Name"]; // Kirk 15 | myObj['More Space']; // Spock 16 | ``` 17 | 18 | ## Note 19 | *That property names with spaces in them must be in quotes (single or double).* 20 | 21 | ## Instructions 22 | - Read the values of the properties `an entree` and `the drink` of `testObj` 23 | using `bracket notation` and assign them to `entreeValue` and `drinkValue` 24 | respectively. 25 | 26 | ### Before 27 | 28 | ```javascript 29 | // Setup 30 | var testObj = { 31 | "an entree": "hamburger", 32 | "my side": "veggies", 33 | "the drink": "water" 34 | }; 35 | 36 | // Only change code below this line 37 | 38 | var entreeValue = testObj; // Change this line 39 | var drinkValue = testObj; // Change this line 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | // Setup 46 | var testObj = { 47 | "an entree": "hamburger", 48 | "my side": "veggies", 49 | "the drink": "water" 50 | }; 51 | 52 | // Only change code below this line 53 | 54 | var entreeValue = testObj["an entree"]; // Change this line 55 | var drinkValue = testObj["the drink"]; // Change this line 56 | ``` 57 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Accessing Objects Properties with Variables.md: -------------------------------------------------------------------------------- 1 | # Accessing Objects Properties with Variables 2 | 3 | Another use of `bracket notation` on `objects` is to use a `variable` to 4 | access a property. This can be very useful for `iterating through` lists of 5 | the object properties or for doing the lookup. 6 | 7 | Here is an example of using a variable to access a property: 8 | 9 | ```javascript 10 | var someProp = "propName"; 11 | var myObj = { 12 | propName: "Some Value" 13 | } 14 | myObj[someProp]; // "Some Value" 15 | ``` 16 | 17 | Here is one more: 18 | 19 | ```javascript 20 | var myDog = "Hunter"; 21 | var dogs = { 22 | Fido: "Mutt", 23 | Hunter: "Doberman", 24 | Snoopie: "Beagle" 25 | } 26 | var breed = dogs[myDog]; 27 | console.log(breed);// "Doberman" 28 | ``` 29 | 30 | Note that we do not use `quotes` around the `variable` name when using 31 | it to access the property because we are using the `value` of the `variable`, 32 | not the name 33 | 34 | ## Instructions 35 | 36 | - Use the `playerNumber` variable to lookup player 16 in `testObj` using 37 | ` bracket notation`. 38 | 39 | ### Before 40 | 41 | ```javascript 42 | // Setup 43 | var testObj = { 44 | 12: "Namath", 45 | 16: "Montana", 46 | 19: "Unitas" 47 | }; 48 | 49 | // Only change code below this line; 50 | 51 | var playerNumber; // Change this Line 52 | var player = testObj; // Change this Line 53 | ``` 54 | 55 | ### Answers 56 | 57 | ```javascript 58 | // Setup 59 | var testObj = { 60 | 12: "Namath", 61 | 16: "Montana", 62 | 19: "Unitas" 63 | }; 64 | 65 | // Only change code below this line; 66 | 67 | var playerNumber = 16; // Change this Line 68 | var player = testObj[playerNumber]; // Change this Line 69 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Accessing Objects Properties with the Dot Operator.md: -------------------------------------------------------------------------------- 1 | # Accessing Objects Properties with the Dot Operator 2 | 3 | There are two ways to access the properties of an object: the 4 | dot operator (`.`) and `bracket notation` (`[]`), similar to an `array`. 5 | 6 | The dot operator is what you use when you 7 | know the name of the property you're trying to access ahead of time. 8 | 9 | Here is a sample of using the dot operator (`.`) to read an object property: 10 | 11 | ```javascript 12 | var myObj = { 13 | prop1: "val1", 14 | prop2: "val2" 15 | }; 16 | var prop1val = myObj.prop1; // val1 17 | var prop2val = myObj.prop2; // val2 18 | ``` 19 | 20 | ## Instructions 21 | - Read in the property values of `testObj` using `dot` notation. Set the 22 | variable `hatValue` equal to the `object` property hat and set the 23 | variable `shirtValue` equal to the `object` property `shirt`. 24 | 25 | ### Before 26 | 27 | ```javascript 28 | // Setup 29 | var testObj = { 30 | "hat": "ballcap", 31 | "shirt": "jersey", 32 | "shoes": "cleats" 33 | }; 34 | 35 | // Only change code below this line 36 | 37 | var hatValue = testObj; // Change this line 38 | var shirtValue = testObj; // Change this line 39 | ``` 40 | 41 | ### After 42 | 43 | ```javascript 44 | // Setup 45 | var testObj = { 46 | "hat": "ballcap", 47 | "shirt": "jersey", 48 | "shoes": "cleats" 49 | }; 50 | 51 | // Only change code below this line 52 | 53 | var hatValue = testObj.hat; // Change this line 54 | var shirtValue = testObj.shirt; // Change this line 55 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Add New Properties to a JavaScript Object.md: -------------------------------------------------------------------------------- 1 | # Add New Properties to a JavaScript Object 2 | 3 | You can add `new` properties to existing JavaScript `objects` the same 4 | way you would modify them. 5 | 6 | Here's how we would add a `bark` property to `ourDog`: 7 | 8 | ```javascript 9 | ourDog.bark = "bow-wow"; 10 | ``` 11 | 12 | or 13 | 14 | ```javascript 15 | ourDog["bark"] = "bow-wow"; 16 | ``` 17 | 18 | Now when we evaluate `ourDog.bark`, we'll get his bark, `bow-wow`. 19 | 20 | ## Instructions 21 | - Add a `bark` property to `myDog` and set it to a dog `sound`, such as 22 | "woof". You may use either `dot` or `bracket notation`. 23 | 24 | ### Before 25 | 26 | ```javascript 27 | // Example 28 | var ourDog = { 29 | "name": "Camper", 30 | "legs": 4, 31 | "tails": 1, 32 | "friends": ["everything!"] 33 | }; 34 | 35 | ourDog.bark = "bow-wow"; 36 | 37 | // Setup 38 | var myDog = { 39 | "name": "Happy Coder", 40 | "legs": 4, 41 | "tails": 1, 42 | "friends": ["Free Code Camp Campers"] 43 | }; 44 | 45 | // Only change code below this line. 46 | ``` 47 | 48 | ### Answers 49 | 50 | ```javascript 51 | // Example 52 | var ourDog = { 53 | "name": "Camper", 54 | "legs": 4, 55 | "tails": 1, 56 | "friends": ["everything!"] 57 | }; 58 | 59 | ourDog.bark = "bow-wow"; 60 | 61 | // Setup 62 | var myDog = { 63 | "name": "Happy Coder", 64 | "legs": 4, 65 | "tails": 1, 66 | "friends": ["Free Code Camp Campers"] 67 | }; 68 | 69 | // Only change code below this line. 70 | 71 | myDog.bark = "woof"; // Dot notation 72 | myDog["bark"] = "woof"; // Bracket notation 73 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Add Two Numbers with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Add Two Numbers with JavaScript 2 | 3 | `Number` is a data type in JavaScript which represents numeric data. 4 | 5 | Now let's try to add two numbers using JavaScript. 6 | 7 | JavaScript uses the + symbol as addition operation when placed between two numbers. 8 | 9 | ## Example 10 | 11 | ```javascript 12 | myVar = 5 + 10; // assigned 15 13 | ``` 14 | 15 | ## Instructions 16 | - Change the 0 so that sum will equal 20. 17 | 18 | ### Answers 19 | 20 | ```javascript 21 | var sum = 10 + 10; 22 | ``` 23 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Adding a default option in Switch statements.md: -------------------------------------------------------------------------------- 1 | # Adding a default option in Switch statements 2 | 3 | In a `switch` statement you may not be able to specify all 4 | possible values as `case` statements. Instead, you can add the 5 | `default` statement which will be executed if no matching case 6 | statements are found. Think of it like the final `else` statement in an 7 | `if/else` chain. 8 | 9 | A default statement should be the last case. 10 | 11 | ```javascript 12 | switch (num) { 13 | case value1: 14 | statement1; 15 | break; 16 | case value2: 17 | statement2; 18 | break; 19 | ... 20 | default: 21 | defaultStatement; 22 | } 23 | ``` 24 | 25 | ## Instructions 26 | - Write a switch statement to set answer for the following conditions: 27 | 28 | ``` 29 | "a" - "apple" 30 | "b" - "bird" 31 | "c" - "cat" 32 | default - "stuff" 33 | ``` 34 | 35 | ### Before 36 | 37 | ```javascript 38 | function switchOfStuff(val) { 39 | var answer = ""; 40 | // Only change code below this line 41 | 42 | 43 | 44 | // Only change code above this line 45 | return answer; 46 | } 47 | 48 | // Change this value to test 49 | switchOfStuff(1); 50 | ``` 51 | 52 | ### Answers 53 | 54 | ```javascript 55 | function switchOfStuff(val) { 56 | var answer = ""; 57 | // Only change code below this line 58 | 59 | switch (val) { 60 | case "a": 61 | answer = "apple"; 62 | break; 63 | case "b": 64 | answer = "bird"; 65 | break; 66 | case "c": 67 | answer = "cat"; 68 | break; 69 | default: 70 | answer = "stuff"; 71 | } 72 | 73 | 74 | // Only change code above this line 75 | return answer; 76 | } 77 | 78 | // Change this value to test 79 | switchOfStuff(1); 80 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Appending Variables to Strings.md: -------------------------------------------------------------------------------- 1 | # Appending Variables to Strings 2 | 3 | Just as we can build a string over multiple lines out of string 4 | literals, we can also append variables to a string using the plus 5 | equals (`+=`) operator. 6 | 7 | ## Instructions 8 | - Set someAdjective and append it to myStr using the `+=` operator. 9 | 10 | ### Before 11 | 12 | ```javascript 13 | // Example 14 | var anAdjective = "awesome!"; 15 | var ourStr = "Free Code Camp is "; 16 | ourStr += anAdjective; 17 | 18 | // Only change code below this line 19 | 20 | var someAdjective; 21 | var myStr = "Learning to code is "; 22 | ``` 23 | 24 | ### Answers 25 | 26 | ```javascript 27 | // Example 28 | var anAdjective = "awesome!"; 29 | var ourStr = "Free Code Camp is "; 30 | ourStr += anAdjective; 31 | 32 | // Only change code below this line 33 | 34 | var someAdjective = 'so nice'; 35 | var myStr = "Learning to code is "; 36 | myStr += someAdjective; 37 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Assignment with a Returned Value.md: -------------------------------------------------------------------------------- 1 | # Assignment with a Returned Value 2 | 3 | If you'll recall from our discussion of 4 | [Storing Values with the Assignment Operator](https://www.freecodecamp.com/challenges/storing-values-with-the-assignment-operator), 5 | everything to the right of the equal sign is resolved 6 | before the value is assigned. This means **we can take the return value of 7 | a function and assign it to a variable.** 8 | 9 | Assume we have pre-defined a function sum which adds two numbers 10 | together, then: 11 | 12 | `ourSum = sum(5, 12);` 13 | 14 | will call `sum` function, which returns a value of `17` and 15 | assigns it to `ourSum` variable. 16 | 17 | ## Instructions 18 | - Call the processArg function with an argument of `7` and assign its `return` value to 19 | the variable processed. 20 | 21 | ### Before 22 | 23 | ```javascript 24 | // Example 25 | var changed = 0; 26 | 27 | function change(num) { 28 | return (num + 5) / 3; 29 | } 30 | 31 | changed = change(10); 32 | 33 | // Setup 34 | var processed = 0; 35 | 36 | function processArg(num) { 37 | return (num + 3) / 5; 38 | } 39 | 40 | // Only change code below this line 41 | ``` 42 | 43 | ### Answers 44 | 45 | ```javascript 46 | // Example 47 | var changed = 0; 48 | 49 | function change(num) { 50 | return (num + 5) / 3; 51 | } 52 | 53 | changed = change(10); 54 | 55 | // Setup 56 | var processed = 0; 57 | 58 | function processArg(num) { 59 | return (num + 3) / 5; 60 | } 61 | 62 | // Only change code below this line 63 | processed = processArg(7); 64 | ``` 65 | 66 | ### Thinking 67 | 68 | Here what is important to understand is in JavaScript everything is a value. 69 | So that why we can assigned the value of a function into a variables. -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Build JavaScript Objects.md: -------------------------------------------------------------------------------- 1 | # Build JavaScript Objects 2 | 3 | You may have heard the term `object` before. 4 | 5 | `Objects` are similar to `arrays`, except that instead of using `indexes` 6 | to access and modify their data, you access the `data` in `objects` 7 | through what are called properties. 8 | 9 | Here's a sample `object`: 10 | 11 | ```javascript 12 | var cat = { 13 | "name": "Whiskers", 14 | "legs": 4, 15 | "tails": 1, 16 | "enemies": ["Water", "Dogs"] 17 | }; 18 | ``` 19 | 20 | `Objects` are useful for storing `data` in a structured way, and can 21 | represent real world `objects`, like a cat. 22 | 23 | ## Instructions 24 | - Make an object that represents a dog called `myDog` which contains 25 | the properties `name` (`a string`), `legs`, `tails` and `friends`. 26 | 27 | You can set these object properties to whatever values you want, as 28 | long "name" is a string, "legs" and "tails" are numbers, and "friends" 29 | is an `array`. 30 | 31 | ### Before 32 | 33 | ```javascript 34 | // Example 35 | var ourDog = { 36 | "name": "Camper", 37 | "legs": 4, 38 | "tails": 1, 39 | "friends": ["everything!"] 40 | }; 41 | 42 | // Only change code below this line. 43 | 44 | var myDog = { 45 | 46 | 47 | 48 | 49 | }; 50 | ``` 51 | 52 | ### Answers 53 | 54 | ```javascript 55 | // Example 56 | var ourDog = { 57 | "name": "Camper", 58 | "legs": 4, 59 | "tails": 1, 60 | "friends": ["everything!"] 61 | }; 62 | 63 | // Only change code below this line. 64 | 65 | var myDog = { 66 | "name": "Lola", 67 | "legs": 4, 68 | "tails": 1, 69 | "friends": ["Sam", "Sarah"] 70 | }; 71 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Chaining If Else Statements.md: -------------------------------------------------------------------------------- 1 | # Chaining If Else Statements 2 | 3 | `if/else` statements can be chained together for complex logic. 4 | Here is `pseudocode` of multiple chained `if / else if` statements: 5 | 6 | ```javascript 7 | if (condition1) { 8 | statement1 9 | } else if (condition2) { 10 | statement2 11 | } else if (condition3) { 12 | statement3 13 | . . . 14 | } else { 15 | statementN 16 | } 17 | ``` 18 | 19 | ## Instructions 20 | - Write chained `if/else if` statements to fulfill the following conditions: 21 | 22 | ```javascript 23 | num < 5 - return "Tiny" 24 | num < 10 - return "Small" 25 | num < 15 - return "Medium" 26 | num < 20 - return "Large" 27 | num >= 20 - return "Huge" 28 | ``` 29 | 30 | ### Before 31 | 32 | ```javascript 33 | function testSize(num) { 34 | // Only change code below this line 35 | 36 | 37 | return "Change Me"; 38 | // Only change code above this line 39 | } 40 | 41 | // Change this value to test 42 | testSize(7); 43 | ``` 44 | 45 | ### Answers 46 | 47 | ```javascript 48 | function testSize(num) { 49 | // Only change code below this line 50 | 51 | if (num < 5) return "Tiny"; 52 | 53 | else if (num < 10) return "Small"; 54 | 55 | else if (num < 15) return "Medium"; 56 | 57 | else if (num < 20) return "Large"; 58 | 59 | else return "Huge"; 60 | 61 | // Only change code above this line 62 | } 63 | 64 | // Change this value to test 65 | testSize(7); 66 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comment Your JavaScript Code.md: -------------------------------------------------------------------------------- 1 | # Comment Your JavaScript Code 2 | 3 | Comments are lines of code that JavaScript will intentionally ignore. 4 | Comments are a great way to leave notes to yourself and to other people who will later need to figure out what that code does. 5 | There are two ways to write comments in JavaScript: 6 | Using `//` will tell JavaScript to ignore the remainder of the 7 | text on the current line: 8 | This is an in-line comment. 9 | You can make a multi-line comment beginning with `/* and ending with */`: 10 | `/* This is a 11 | multi-line comment */` 12 | ## Best Practice 13 | As you write code, you should regularly add comments to clarify the function 14 | of parts of your code. Good commenting can help communicate the intent of 15 | your code—both for others and for your future self. 16 | ##Instructions 17 | Try creating one of each type of comment. 18 | 19 | ### Answers 20 | 21 | ```javascript 22 | // This is single-line 23 | 24 | /* 25 | This is multi-line 26 | */ 27 | ``` 28 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Greater Than Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Greater Than Operator 2 | 3 | The greater than operator (`>`) compares the values of two numbers. If 4 | the number to the left is greater than the number to the right, 5 | it returns `true`. Otherwise, it returns `false`. 6 | 7 | Like the equality operator, greater than operator will 8 | **convert data types of values while comparing.** 9 | 10 | ## Examples 11 | 12 | ```javascript 13 | 5 > 3 // true 14 | 7 > '3' // true 15 | 2 > 3 // false 16 | '1' > 9 // false 17 | ``` 18 | 19 | ## Instructions 20 | - Add the greater than operator to the indicated lines so that the 21 | return statements make sense. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | function testGreaterThan(val) { 27 | if (val) { // Change this line 28 | return "Over 100"; 29 | } 30 | 31 | if (val) { // Change this line 32 | return "Over 10"; 33 | } 34 | 35 | return "10 or Under"; 36 | } 37 | 38 | // Change this value to test 39 | testGreaterThan(10); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | function testGreaterThan(val) { 46 | if (val > 100) { // Change this line 47 | return "Over 100"; 48 | } 49 | 50 | if (val > 10) { // Change this line 51 | return "Over 10"; 52 | } 53 | 54 | return "10 or Under"; 55 | } 56 | 57 | // Change this value to test 58 | testGreaterThan(10); 59 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Greater Than Or Equal To Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Greater Than Or Equal To Operator 2 | 3 | The greater than or equal to operator (`>=`) compares the values of 4 | two numbers. If the number to the left is greater than or equal to 5 | the number to the right, it returns true. Otherwise, it returns false. 6 | 7 | Like the equality operator, greater than or equal to operator 8 | will **convert data types while comparing.** 9 | 10 | ## Examples 11 | 12 | ```javascript 13 | 6 >= 6 // true 14 | 7 >= '3' // true 15 | 2 >= 3 // false 16 | '7' >= 9 // false 17 | ``` 18 | 19 | ## Instructions 20 | - Add the greater than or equal to operator to the indicated lines 21 | so that the return statements make sense. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | function testGreaterOrEqual(val) { 27 | if (val) { // Change this line 28 | return "20 or Over"; 29 | } 30 | 31 | if (val) { // Change this line 32 | return "10 or Over"; 33 | } 34 | 35 | return "9 or Under"; 36 | } 37 | 38 | // Change this value to test 39 | testGreaterOrEqual(10); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | function testGreaterOrEqual(val) { 46 | if (val >= 20) { // Change this line 47 | return "20 or Over"; 48 | } 49 | 50 | if (val >= 10) { // Change this line 51 | return "10 or Over"; 52 | } 53 | 54 | return "9 or Under"; 55 | } 56 | 57 | // Change this value to test 58 | testGreaterOrEqual(10); 59 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Inequality Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Inequality Operator 2 | 3 | The inequality operator (`!=`) is the **opposite** of the equality operator. 4 | It means `"Not Equal"` and returns false where equality would return 5 | true and vice versa. Like the equality operator, **the inequality operator 6 | will convert data types of values while comparing.** 7 | 8 | ## Examples 9 | 10 | ```javascript 11 | 1 != 2 // true 12 | 1 != "1" // false 13 | 1 != '1' // false 14 | 1 != true // false 15 | 0 != false // false 16 | ``` 17 | 18 | ## Instructions 19 | 20 | - Add the inequality operator `!=` in the `if` statement so that the 21 | function will return `"Not Equal"` when val is not equivalent to `99` 22 | 23 | ### Before 24 | 25 | ```javascript 26 | // Setup 27 | function testNotEqual(val) { 28 | if (val) { // Change this line 29 | return "Not Equal"; 30 | } 31 | return "Equal"; 32 | } 33 | 34 | // Change this value to test 35 | testNotEqual(10); 36 | ``` 37 | 38 | ### Answers 39 | 40 | ```javascript 41 | // Setup 42 | function testNotEqual(val) { 43 | if (val != 99) { // Change this line 44 | return "Not Equal"; 45 | } 46 | return "Equal"; 47 | } 48 | 49 | // Change this value to test 50 | testNotEqual(10); 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Less Than Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Less Than Operator 2 | 3 | The less than operator (`<`) compares the values of two numbers. 4 | If the number to the left is less than the number to the right, 5 | it returns `true`. Otherwise, it returns `false`. Like the equality 6 | operator, less than operator **converts data types while comparing.** 7 | 8 | ## Examples 9 | 10 | ```javascript 11 | 2 < 5 // true 12 | '3' < 7 // true 13 | 5 < 5 // false 14 | 3 < 2 // false 15 | '8' < 4 // false 16 | ``` 17 | 18 | ## Instructions 19 | - Add the less than operator to the indicated lines so that the 20 | return statements make sense. 21 | 22 | ### Before 23 | 24 | ```javascript 25 | function testLessThan(val) { 26 | if (val) { // Change this line 27 | return "Under 25"; 28 | } 29 | 30 | if (val) { // Change this line 31 | return "Under 55"; 32 | } 33 | 34 | return "55 or Over"; 35 | } 36 | 37 | // Change this value to test 38 | testLessThan(10); 39 | ``` 40 | 41 | ### Answers 42 | 43 | ```javascript 44 | function testLessThan(val) { 45 | if (val < 25) { // Change this line 46 | return "Under 25"; 47 | } 48 | 49 | if (val < 55) { // Change this line 50 | return "Under 55"; 51 | } 52 | 53 | return "55 or Over"; 54 | } 55 | 56 | // Change this value to test 57 | testLessThan(10); 58 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Less Than Or Equal To Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Less Than Or Equal To Operator 2 | 3 | The less than or equal to operator (`<=`) compares the values of two numbers. 4 | If the number to the left is less than or equal the number to the right, 5 | it returns true. If the number on the left is greater than the number on 6 | the right, it returns false. Like the equality operator, less than or 7 | equal to **converts data types.** 8 | 9 | ## Examples 10 | 11 | ```javascript 12 | 4 <= 5 // true 13 | '7' <= 7 // true 14 | 5 <= 5 // true 15 | 3 <= 2 // false 16 | '8' <= 4 // false 17 | ``` 18 | 19 | ## Instructions 20 | - Add the less than or equal to operator to the indicated lines so that 21 | the return statements make sense. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | function testLessOrEqual(val) { 27 | if (val) { // Change this line 28 | return "Smaller Than or Equal to 12"; 29 | } 30 | 31 | if (val) { // Change this line 32 | return "Smaller Than or Equal to 24"; 33 | } 34 | 35 | return "25 or More"; 36 | } 37 | 38 | // Change this value to test 39 | testLessOrEqual(10); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | function testLessOrEqual(val) { 46 | if (val <= 12) { // Change this line 47 | return "Smaller Than or Equal to 12"; 48 | } 49 | 50 | if (val <= 24) { // Change this line 51 | return "Smaller Than or Equal to 24"; 52 | } 53 | 54 | return "25 or More"; 55 | } 56 | 57 | // Change this value to test 58 | testLessOrEqual(10); 59 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Strict Equality Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Strict Equality Operator 2 | 3 | Strict equality (`===`) is the **counterpart** to the equality operator (`==`). 4 | Unlike the equality operator, strict equality **tests both the 5 | data type and value of the compared elements**. 6 | 7 | ## Examples 8 | 9 | ```javascript 10 | 3 === 3 // true 11 | 3 === '3' // false 12 | ``` 13 | 14 | In the second example, `3` is a `Number` type and `3` is a `String` type. 15 | 16 | ## Instructions 17 | - Use the strict equality operator in the `if` statement so the 18 | function will return `Equal` when val is strictly equal to `7` 19 | 20 | ### Before 21 | 22 | ```javascript 23 | // Setup 24 | function testStrict(val) { 25 | if (val) { // Change this line 26 | return "Equal"; 27 | } 28 | return "Not Equal"; 29 | } 30 | 31 | // Change this value to test 32 | testStrict(10); 33 | ``` 34 | 35 | ### Answers 36 | 37 | ```javascript 38 | // Setup 39 | function testStrict(val) { 40 | if (val === 7) { // Change this line 41 | return "Equal"; 42 | } 43 | return "Not Equal"; 44 | } 45 | 46 | // Change this value to test 47 | testStrict(10); 48 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparison with the Strict Inequality Operator.md: -------------------------------------------------------------------------------- 1 | # Comparison with the Strict Inequality Operator 2 | 3 | The strict inequality operator (`!==`) is the opposite of the strict 4 | equality operator. It means `"Strictly Not Equal"` and returns 5 | false where strict equality would return true and vice versa. 6 | **Strict inequality will not convert data types.** 7 | 8 | ## Examples 9 | 10 | ```javascript 11 | 3 !== 3 // false 12 | 3 !== '3' // true 13 | 4 !== 3 // true 14 | ``` 15 | 16 | ## Instructions 17 | - Add the strict inequality operator to the `if` statement so the function 18 | will return `Not Equal` when `val` is not strictly equal to `17` 19 | 20 | ### Before 21 | 22 | ```javascript 23 | // Setup 24 | function testStrictNotEqual(val) { 25 | // Only Change Code Below this Line 26 | 27 | if (val) { 28 | 29 | // Only Change Code Above this Line 30 | 31 | return "Not Equal"; 32 | } 33 | return "Equal"; 34 | } 35 | 36 | // Change this value to test 37 | testStrictNotEqual(10); 38 | ``` 39 | 40 | ### Answers 41 | 42 | ```javascript 43 | // Setup 44 | function testStrictNotEqual(val) { 45 | // Only Change Code Below this Line 46 | 47 | if (val !== 17) { 48 | 49 | // Only Change Code Above this Line 50 | 51 | return "Not Equal"; 52 | } 53 | return "Equal"; 54 | } 55 | 56 | // Change this value to test 57 | testStrictNotEqual(10); 58 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparisons with the Logical And Operator.md: -------------------------------------------------------------------------------- 1 | # Comparisons with the Logical And Operator 2 | 3 | Sometimes you will need to test more than one thing at a time. The logical 4 | and operator (`&&`) returns `true` if and only if the operands to the left 5 | and right of it are `true`. 6 | 7 | The same effect could be achieved by nesting an if statement 8 | inside another if: 9 | 10 | ```javascript 11 | if (num > 5) { 12 | if (num < 10) { 13 | return "Yes"; 14 | } 15 | } 16 | return "No"; 17 | ``` 18 | 19 | will only return `Yes` if `num` is between `6` and `9` (`6 and 9 included`). 20 | The same logic can be written as: 21 | 22 | ```javascript 23 | if (num > 5 && num < 10) { 24 | return "Yes"; 25 | } 26 | return "No"; 27 | ``` 28 | 29 | ## Instructions 30 | - Combine the two `if` statements into one statement which will return `Yes` 31 | if val is less than or equal to `50` and greater than or equal to 32 | `25`. `Otherwise`, will return `No`. 33 | 34 | ### Before 35 | 36 | ```javascript 37 | function testLogicalAnd(val) { 38 | // Only change code below this line 39 | 40 | if (val) { 41 | if (val) { 42 | return "Yes"; 43 | } 44 | } 45 | 46 | // Only change code above this line 47 | return "No"; 48 | } 49 | 50 | // Change this value to test 51 | testLogicalAnd(10); 52 | ``` 53 | 54 | ### Answers 55 | 56 | ```javascript 57 | function testLogicalAnd(val) { 58 | // Only change code below this line 59 | 60 | if (val <= 50 && val >= 25) { 61 | return 'Yes'; 62 | } 63 | 64 | // Only change code above this line 65 | return "No"; 66 | } 67 | 68 | // Change this value to test 69 | testLogicalAnd(10); 70 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Comparisons with the Logical Or Operator.md: -------------------------------------------------------------------------------- 1 | # Comparisons with the Logical Or Operator 2 | 3 | The logical or operator (`||`) returns true if either of the 4 | operands is `true`. `Otherwise`, it returns `false`. 5 | 6 | The pattern below should look familiar from prior waypoints: 7 | 8 | ```javascript 9 | if (num > 10) { 10 | return "No"; 11 | } 12 | if (num < 5) { 13 | return "No"; 14 | } 15 | return "Yes"; 16 | ``` 17 | 18 | will return `Yes` only if `num` is between `5` and `10` (`5 and 10 included`). 19 | The same logic can be written as: 20 | 21 | ```javascript 22 | if (num > 10 || num < 5) { 23 | return "No"; 24 | } 25 | return "Yes"; 26 | ``` 27 | 28 | ## Instructions 29 | - Combine the two if statements into one statement which returns 30 | "Outside" if `val` is not between `10` and `20`, inclusive. `Otherwise`, 31 | return `"Inside"`. 32 | 33 | ### Before 34 | 35 | ```javascript 36 | function testLogicalOr(val) { 37 | // Only change code below this line 38 | 39 | if (val) { 40 | return "Outside"; 41 | } 42 | 43 | if (val) { 44 | return "Outside"; 45 | } 46 | 47 | // Only change code above this line 48 | return "Inside"; 49 | } 50 | 51 | // Change this value to test 52 | testLogicalOr(15); 53 | ``` 54 | 55 | ### Answers 56 | 57 | ```javascript 58 | function testLogicalOr(val) { 59 | // Only change code below this line 60 | 61 | if (val < 10 || val > 20) { 62 | return "Outside"; 63 | } 64 | 65 | // Only change code above this line 66 | return "Inside"; 67 | } 68 | 69 | // Change this value to test 70 | testLogicalOr(15); 71 | 72 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Compound Assignment With Augmented Addition.md: -------------------------------------------------------------------------------- 1 | # Compound Assignment With Augmented Addition 2 | 3 | In programming, it is common to use assignments to modify the contents of a variable. 4 | Remember that everything to the right of the equals sign is evaluated first, so we can say: 5 | 6 | ```javascript 7 | myVar = myVar + 5; 8 | ``` 9 | 10 | to add 5 to myVar. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step. 11 | 12 | One such operator is the += operator. 13 | 14 | `myVar += 5;` will add 5 to myVar. 15 | 16 | ## Instructions 17 | - Convert the assignments for a, b, and c to use the `+=` operator. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | var a = 3; 23 | var b = 17; 24 | var c = 12; 25 | 26 | // Only modify code below this line 27 | 28 | a = a + 12; 29 | b = 9 + b; 30 | c = c + 7; 31 | ``` 32 | 33 | ### Answers 34 | 35 | ```javascript 36 | var a = 3; 37 | var b = 17; 38 | var c = 12; 39 | 40 | // Only modify code below this line 41 | 42 | a += 12; 43 | b += 9; 44 | c += 7; 45 | ``` 46 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Compound Assignment With Augmented Division.md: -------------------------------------------------------------------------------- 1 | # Compound Assignment With Augmented Division 2 | 3 | The `/=` operator divides a variable by another number. 4 | 5 | `myVar = myVar / 5;` 6 | 7 | Will divide myVar by 5. This can be rewritten as: 8 | 9 | `myVar /= 5;` 10 | 11 | ## Instructions 12 | - Convert the assignments for a, b, and c to use the `/=` operator. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | var a = 48; 18 | var b = 108; 19 | var c = 33; 20 | 21 | // Only modify code below this line 22 | 23 | a = a / 12; 24 | b = b / 4; 25 | c = c / 11; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | var a = 48; 32 | var b = 108; 33 | var c = 33; 34 | 35 | // Only modify code below this line 36 | 37 | a /= 12; 38 | b /= 4; 39 | c /= 11; 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Compound Assignment With Augmented Multiplication.md: -------------------------------------------------------------------------------- 1 | # Compound Assignment With Augmented Multiplication 2 | 3 | The `*=` operator multiplies a variable by a number. 4 | 5 | `myVar = myVar * 5;` 6 | 7 | will multiply myVar by 5. This can be rewritten as: 8 | 9 | `myVar *= 5;` 10 | 11 | ## Instructions 12 | - Convert the assignments for a, b, and c to use the `*=` operator. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | var a = 5; 18 | var b = 12; 19 | var c = 4.6; 20 | 21 | // Only modify code below this line 22 | 23 | a = a * 5; 24 | b = 3 * b; 25 | c = c * 10; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | var a = 5; 32 | var b = 12; 33 | var c = 4.6; 34 | 35 | // Only modify code below this line 36 | 37 | a *= 5; 38 | b *= 3; 39 | c *= 10; 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Compound Assignment With Augmented Subtraction.md: -------------------------------------------------------------------------------- 1 | # Compound Assignment With Augmented Subtraction 2 | 3 | Like the `+=` operator, `-=` subtracts a number from a variable. 4 | 5 | `myVar = myVar - 5;` 6 | 7 | will subtract 5 from myVar. This can be rewritten as: 8 | 9 | `myVar -= 5;` 10 | 11 | ## Instructions 12 | - Convert the assignments for a, b, and c to use the `-=` operator. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | var a = 11; 18 | var b = 9; 19 | var c = 3; 20 | 21 | // Only modify code below this line 22 | 23 | a = a - 6; 24 | b = b - 15; 25 | c = c - 1; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | var a = 11; 32 | var b = 9; 33 | var c = 3; 34 | 35 | // Only modify code below this line 36 | 37 | a -= 6; 38 | b -= 15; 39 | c -= 1; 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Concatenating Strings with Plus Operator.md: -------------------------------------------------------------------------------- 1 | # Concatenating Strings with Plus Operator 2 | 3 | In JavaScript, when the `+` operator is used with a `String` value, 4 | it is called the `concatenation` operator. You can build a new string out 5 | of other strings by concatenating them together. 6 | 7 | ## Example 8 | 9 | `'My name is Alan,' + ' I concatenate.'` 10 | 11 | ## Note 12 | *Watch out for spaces. Concatenation does not add spaces between 13 | concatenated strings, so you'll need to add them yourself.* 14 | 15 | ## Instructions 16 | - Build myStr from the strings `"This is the start. "` and `"This is the end."` using the `+` operator. 17 | 18 | ### Before 19 | 20 | ```javascript 21 | // Example 22 | var ourStr = "I come first. " + "I come second."; 23 | 24 | // Only change code below this line 25 | 26 | var myStr; 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | // Example 33 | var ourStr = "I come first. " + "I come second."; 34 | 35 | // Only change code below this line 36 | 37 | var myStr = 'This is the start. ' + 'This is the end.'; 38 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Concatenating Strings with the Plus Equals Operator.md: -------------------------------------------------------------------------------- 1 | # Concatenating Strings with the Plus Equals Operator 2 | 3 | We can also use the `+=` operator to concatenate a string onto the 4 | end of an existing string variable. This can be very helpful to break a 5 | long string over several lines. 6 | 7 | ## Note 8 | *Watch out for spaces. Concatenation does not add spaces between concatenated 9 | strings, so you'll need to add them yourself.* 10 | 11 | ## Instructions 12 | - Build `myStr` over several lines by concatenating these two strings: 13 | `This is the first sentence. ` and `This is the second sentence.` using the `+=` operator. 14 | 15 | ### Before 16 | 17 | ```javascript 18 | // Example 19 | var ourStr = "I come first. "; 20 | ourStr += "I come second."; 21 | 22 | // Only change code below this line 23 | 24 | var myStr; 25 | ``` 26 | 27 | ### Answers 28 | 29 | ```javascript 30 | // Example 31 | var ourStr = "I come first. "; 32 | ourStr += "I come second."; 33 | 34 | // Only change code below this line 35 | 36 | var myStr = 'This is the first sentence. '; 37 | myStr +='This is the second sentence.'; 38 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Constructing Strings with Variables.md: -------------------------------------------------------------------------------- 1 | # Constructing Strings with Variables 2 | 3 | Sometimes you will need to build a string, [Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs) style. 4 | By using the concatenation operator (`+`), you can insert one or more 5 | variables into a string you're building. 6 | 7 | ## Instructions 8 | - Set myName to a string equal to your name and build myStr with myName 9 | between the strings `"My name is "` and `" and I am swell!"` 10 | 11 | ### Before 12 | 13 | ```javascript 14 | // Example 15 | var ourName = "Free Code Camp"; 16 | var ourStr = "Hello, our name is " + ourName + ", how are you?"; 17 | 18 | // Only change code below this line 19 | var myName; 20 | var myStr; 21 | ``` 22 | 23 | ### Answers 24 | 25 | ```javascript 26 | // Example 27 | var ourName = "Free Code Camp"; 28 | var ourStr = "Hello, our name is " + ourName + ", how are you?"; 29 | 30 | // Only change code below this line 31 | var myName = 'Emanuel'; 32 | var myStr = 'My name is ' + myName + ' and I am swell!'; 33 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Convert Celsius to Fahrenheit.md: -------------------------------------------------------------------------------- 1 | # Convert Celsius to Fahrenheit 2 | 3 | To test your learning, you will create a solution "from scratch". 4 | Place your code between the indicated lines and it will be tested against 5 | multiple test cases. 6 | 7 | The algorithm to convert from Celsius to Fahrenheit is the temperature 8 | in Celsius times 9/5, plus 32. 9 | 10 | You are given a variable celsius representing a temperature in Celsius. 11 | Use the variable fahrenheit already defined and apply the algorithm to assign 12 | it the corresponding temperature in Fahrenheit. 13 | 14 | ## Note 15 | Don't worry too much about the function and return statements as they will be covered in future challenges. For now, only use operators that you have already learned. 16 | 17 | ### Before 18 | 19 | ```javascript 20 | function convertToF(celsius) { 21 | var fahrenheit; 22 | // Only change code below this line 23 | 24 | 25 | // Only change code above this line 26 | return fahrenheit; 27 | } 28 | 29 | // Change the inputs below to test your code 30 | convertToF(30); 31 | ``` 32 | 33 | ### Thinking Process 34 | 35 | We only need to apply the formula to the var fahrenheit. So we multiply 36 | the args celsius to 9/5 and we finally add 32 at the end. 37 | 38 | ### Answers 39 | 40 | ```javascript 41 | function convertToF(celsius) { 42 | 43 | var fahrenheit; 44 | // Only change code below this line 45 | 46 | fahrenheit = celsius * (9/5) + 32; 47 | 48 | // Only change code above this line 49 | return fahrenheit; 50 | } 51 | 52 | // Change the inputs below to test your code 53 | convertToF(30); 54 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Count Backwards With a For Loop.md: -------------------------------------------------------------------------------- 1 | # Count Backwards With a For Loop 2 | 3 | A `for loop` can also count backwards, so long as we can define 4 | the right conditions. 5 | 6 | In order to count backwards by twos, we'll need to change our 7 | initialization, condition, and final-expression. 8 | 9 | We'll start at `i = 10` and loop while `i > 0`. We'll decrement `i by 2` each 10 | loop with `i -= 2`. 11 | 12 | ```javascript 13 | var ourArray = []; 14 | for (var i=10; i > 0; i-=2) { 15 | ourArray.push(i); 16 | } 17 | ``` 18 | 19 | ourArray will now contain `[10,8,6,4,2]`. 20 | 21 | Let's change our initialization and final-expression 22 | so we can count backward by twos by odd numbers. 23 | 24 | ## Instructions 25 | - Push the odd numbers from 9 through 1 to `myArray` using a `for loop`. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | // Example 31 | var ourArray = []; 32 | 33 | for (var i = 10; i > 0; i -= 2) { 34 | ourArray.push(i); 35 | } 36 | 37 | // Setup 38 | var myArray = []; 39 | 40 | // Only change code below this line. 41 | ``` 42 | 43 | ### Answers 44 | 45 | ```javascript 46 | // Example 47 | var ourArray = []; 48 | 49 | for (var i = 10; i > 0; i -= 2) { 50 | ourArray.push(i); 51 | } 52 | 53 | // Setup 54 | var myArray = []; 55 | 56 | for (var i = 9; i > 0; i -= 2) { 57 | myArray.push(i); 58 | } 59 | 60 | // Only change code below this line. 61 | ``` 62 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Create Decimal Numbers with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Create Decimal Numbers with JavaScript 2 | 3 | We can store decimal numbers in variables too. Decimal numbers are sometimes 4 | referred to as `floating` point `numbers` or `floats`. 5 | 6 | ## Note 7 | Not all real numbers can accurately be represented in floating point. 8 | This can lead to rounding errors. [Details Here](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). 9 | 10 | ## Instructions 11 | - Create a variable myDecimal and give it a decimal value with a fractional part (e.g. 5.7). 12 | 13 | ### Before 14 | 15 | ```javascript 16 | var ourDecimal = 5.7; 17 | 18 | // Only change code below this line 19 | ``` 20 | 21 | ### Answers 22 | 23 | ```javascript 24 | 25 | var ourDecimal = 5.7; 26 | 27 | // Only change code below this line 28 | 29 | var myDecimal = 5.7; 30 | ``` 31 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Declare JavaScript Variables.md: -------------------------------------------------------------------------------- 1 | # Declare JavaScript Variables 2 | 3 | In computer science, data is anything that is meaningful to the computer. JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object. 4 | 5 | For example, computers distinguish between numbers, such as the number 12, and strings, such as "12", "dog", or "123 cats", which are collections of characters. Computers can perform mathematical operations on a number, but not on a string. 6 | 7 | Variables allow computers to store and manipulate data in a dynamic fashion. They do this by using a "label" to point to the data rather than using the data itself. Any of the seven data types may be stored in a variable. 8 | 9 | Variables are similar to the x and y variables you use in mathematics, which means they're a simple name to represent the data we want to refer to. Computer variables differ from mathematical variables in that they can store different values at different times. 10 | 11 | We tell JavaScript to create or declare a variable by putting the keyword var in front of it, like so: 12 | 13 | `var ourName;` 14 | 15 | creates a variable called ourName. In JavaScript we end statements with semicolons. 16 | 17 | Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number. 18 | 19 | ## Instructions 20 | Use the var keyword to create a variable called myName. 21 | 22 | ## Hint 23 | Look at the ourName example if you get stuck. 24 | 25 | ### Answers 26 | 27 | ```javascript 28 | // Example 29 | var ourName; 30 | 31 | // Define myName below this line 32 | var myName; 33 | 34 | ``` 35 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Declare String Variables.md: -------------------------------------------------------------------------------- 1 | # Declare String Variables 2 | 3 | Previously we have used the code 4 | 5 | `var myName = "your name";` 6 | 7 | "your name" is called a string literal. It is a string because it is a 8 | series of zero or more characters enclosed in single or double quotes. 9 | 10 | ## Instructions 11 | - Create two new string variables: myFirstName and myLastName and assign 12 | them the values of your first and last name, respectively. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | // Example 18 | var firstName = "Alan"; 19 | var lastName = "Turing"; 20 | 21 | // Only change code below this line 22 | ``` 23 | 24 | ### Answers 25 | 26 | ```javascript 27 | // Example 28 | var firstName = "Alan"; 29 | var lastName = "Turing"; 30 | 31 | // Only change code below this line 32 | var myFirstName = "Emanuel"; 33 | var myLastName = "Quimper"; 34 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Decrement a Number with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Decrement a Number with JavaScript 2 | 3 | You can easily `decrement` or `decrease` a variable by one with the `--` operator. 4 | 5 | ```javascript 6 | i--; 7 | ``` 8 | 9 | is the equivalent of 10 | 11 | ```javascript 12 | i = i - 1; 13 | ``` 14 | 15 | ## Note 16 | The entire line becomes `i--;`, eliminating the need for the equal sign. 17 | 18 | ## Instructions 19 | - Change the code to use the `--` operator on myVar. 20 | 21 | ### Before 22 | 23 | ```javascript 24 | var myVar = 11; 25 | 26 | // Only change code below this line 27 | myVar = myVar - 1; 28 | ``` 29 | 30 | ### Answers 31 | 32 | ```javascript 33 | var myVar = 11; 34 | 35 | // Only change code below this line 36 | myVar--; 37 | ``` 38 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Delete Properties from a JavaScript Object.md: -------------------------------------------------------------------------------- 1 | # Delete Properties from a JavaScript Object 2 | 3 | We can also delete properties from objects like this: 4 | 5 | ```javascript 6 | delete ourDog.bark; 7 | ``` 8 | 9 | ## Instructions 10 | - Delete the `tails` property from `myDog`. You may use either `dot` or 11 | `bracket notation`. 12 | 13 | ### Before 14 | 15 | ```javascript 16 | // Example 17 | var ourDog = { 18 | "name": "Camper", 19 | "legs": 4, 20 | "tails": 1, 21 | "friends": ["everything!"], 22 | "bark": "bow-wow" 23 | }; 24 | 25 | delete ourDog.bark; 26 | 27 | // Setup 28 | var myDog = { 29 | "name": "Happy Coder", 30 | "legs": 4, 31 | "tails": 1, 32 | "friends": ["Free Code Camp Campers"], 33 | "bark": "woof" 34 | }; 35 | 36 | // Only change code below this line. 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | // Example 43 | var ourDog = { 44 | "name": "Camper", 45 | "legs": 4, 46 | "tails": 1, 47 | "friends": ["everything!"], 48 | "bark": "bow-wow" 49 | }; 50 | 51 | delete ourDog.bark; 52 | 53 | // Setup 54 | var myDog = { 55 | "name": "Happy Coder", 56 | "legs": 4, 57 | "tails": 1, 58 | "friends": ["Free Code Camp Campers"], 59 | "bark": "woof" 60 | }; 61 | 62 | // Only change code below this line. 63 | 64 | delete myDog.tails; 65 | ``` 66 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Divide One Number by Another with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Divide One Number by Another with JavaScript 2 | 3 | We can also divide one number by another. 4 | 5 | JavaScript uses the `/` symbol for division. 6 | 7 | ## Example 8 | 9 | ```javascript 10 | myVar = 16 / 2; // assigned 8 11 | ``` 12 | 13 | ## Instructions 14 | - Change the 0 so that the quotient is equal to 2. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | var quotient = 66 / 0; 20 | ``` 21 | 22 | ### Answers 23 | 24 | ```javascript 25 | var quotient = 66 / 33; 26 | ``` 27 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Divide one Decimal by Another with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Divide one Decimal by Another with JavaScript 2 | 3 | Now let's divide one decimal by another. 4 | 5 | ## Instructions 6 | - Change the 0.0 so that quotient will equal to 2.2. 7 | 8 | ### Before 9 | 10 | ```javascript 11 | var quotient = 0.0 / 2.0; 12 | ``` 13 | 14 | ### Answers 15 | 16 | ```javascript 17 | var quotient = 4.4 / 2.0; 18 | ``` 19 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Escape Sequences in Strings.md: -------------------------------------------------------------------------------- 1 | # Escape Sequences in Strings 2 | 3 | Quotes are not the only characters that can be escaped inside a string. 4 | Here is a table of common `escape` sequences: 5 | 6 | | Code | Output| 7 | -------|-------- 8 | \' | single quote 9 | \" | double quote 10 | \\ | backslash 11 | \n | new line 12 | \r | carriage return 13 | \t | tab 14 | \b | backspace 15 | \f | form feed 16 | 17 | *Note that the backslash itself must be escaped in order 18 | to display as a backslash.* 19 | 20 | ## Instructions 21 | - Assign the following two lines of text into the single variable myStr using escape sequences. 22 | 23 | `Here is a backslash: \. 24 | Here is a new line with two tabs.` 25 | 26 | You will need to use escape sequences to insert special 27 | characters correctly. You will also need to follow the spacing 28 | as it looks above with no additional spaces between each escape sequence. 29 | 30 | Here is the text with the escape sequences written out. 31 | 32 | `Here is a backslash: backslash.newline tab tab Here 33 | is a new line with two tabs.` 34 | 35 | ### Before 36 | 37 | ```javascript 38 | var myStr; // Change this line 39 | ``` 40 | 41 | ### Answers 42 | 43 | ```javascript 44 | var myStr = 'Here is a backslash: \\.\n\t\tHere is a new line with two tabs.'; 45 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Escaping Literal Quotes in Strings.md: -------------------------------------------------------------------------------- 1 | # Escaping Literal Quotes in Strings 2 | 3 | When you are defining a string you must start and end with a single 4 | or double quote. What happens when you need a literal quote: `"` or `'` inside 5 | of your string? 6 | 7 | In JavaScript, you can escape a quote from considering it as an end 8 | of string quote by placing a backslash (\) in front of the quote. 9 | 10 | `var sampleStr = "Alan said, \"Peter is learning JavaScript\".";` 11 | 12 | This signals to JavaScript that the following quote is not the end of the 13 | string, but should instead appear inside the string. So if you were to print 14 | this to the console, you would get: 15 | 16 | `Alan said, "Peter is learning JavaScript".` 17 | 18 | ## Instructions 19 | - Use backslashes to assign a string to the myStr variable so that if you were to print it to the console, you would see: 20 | 21 | `I am a "double quoted" string inside "double quotes".` 22 | 23 | ### Before 24 | 25 | ```javascript 26 | var myStr; // Change this line 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | var myStr = "I am a \"double quoted\" string inside \"double quotes\"."; 33 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Find Numbers with Regular Expressions.md: -------------------------------------------------------------------------------- 1 | # Find Numbers with Regular Expressions 2 | 3 | We can use special selectors in `Regular Expressions` to select a particular 4 | type of value. 5 | 6 | One such selector is the digit selector `\d` which is used to 7 | retrieve one digit (e.g. numbers 0 to 9) in a string. 8 | 9 | In JavaScript, it is used like this: `/\d/g`. 10 | 11 | Appending a plus sign (`+`) after the selector, e.g. `/\d+/g`, allows 12 | this regular expression to match one or more digits. 13 | 14 | The trailing `g` is short for '`global`', which allows this regular 15 | expression to find all matches rather than stop at the first match. 16 | 17 | ## Instructions 18 | - Use the `\d` selector to select the number of numbers 19 | in the string, allowing for the possibility of one or more digit. 20 | 21 | ### Before 22 | 23 | ```javascript 24 | // Setup 25 | var testString = "There are 3 cats but 4 dogs."; 26 | 27 | // Only change code below this line. 28 | 29 | var expression = /.+/g; // Change this line 30 | 31 | // Only change code above this line 32 | 33 | // This code counts the matches of expression in testString 34 | var digitCount = testString.match(expression).length; 35 | ``` 36 | 37 | ### Answers 38 | 39 | ```javascript 40 | // Setup 41 | var testString = "There are 3 cats but 4 dogs."; 42 | 43 | // Only change code below this line. 44 | 45 | var expression = /\d+/g; // Change this line 46 | 47 | // Only change code above this line 48 | 49 | // This code counts the matches of expression in testString 50 | var digitCount = testString.match(expression).length; 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Find Whitespace with Regular Expressions.md: -------------------------------------------------------------------------------- 1 | # Find Whitespace with Regular Expressions 2 | 3 | We can also use `regular expression` selectors like `\s` to find 4 | whitespace in a string. 5 | 6 | The whitespace characters are `" "` (space), `\r` (the carriage return), 7 | `\n` (newline), `\t` (tab), and `\f` (the form feed). 8 | 9 | The whitespace regular expression looks like this: 10 | 11 | `/\s+/g` 12 | 13 | ## Instructions 14 | - Use `\s` to select all the whitespace characters in the sentence string. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | // Setup 20 | var testString = "How many spaces are there in this sentence?"; 21 | 22 | // Only change code below this line. 23 | 24 | var expression = /.+/g; // Change this line 25 | 26 | // Only change code above this line 27 | 28 | // This code counts the matches of expression in testString 29 | var spaceCount = testString.match(expression).length; 30 | ``` 31 | 32 | ### Answers 33 | 34 | ```javascript 35 | // Setup 36 | var testString = "How many spaces are there in this sentence?"; 37 | 38 | // Only change code below this line. 39 | 40 | var expression = /\s+/g; // Change this line 41 | 42 | // Only change code above this line 43 | 44 | // This code counts the matches of expression in testString 45 | var spaceCount = testString.match(expression).length; 46 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Find the Length of a String.md: -------------------------------------------------------------------------------- 1 | # Find the Length of a String 2 | 3 | You can find the length of a `String` value by writing `.length` after 4 | the string variable or string literal. 5 | 6 | `"Alan Peter".length; // 10` 7 | 8 | For example, if we created a variable `var firstName = "Charles"`, we 9 | could find out how long the string `"Charles"` is by using the 10 | `firstName.length` property. 11 | 12 | ## Instructions 13 | - Use the `.length` property to count the number of characters in 14 | the lastName variable and assign it to `lastNameLength`. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | // Example 20 | var firstNameLength = 0; 21 | var firstName = "Ada"; 22 | 23 | firstNameLength = firstName.length; 24 | 25 | // Setup 26 | var lastNameLength = 0; 27 | var lastName = "Lovelace"; 28 | 29 | // Only change code below this line. 30 | 31 | lastNameLength = lastName; 32 | ``` 33 | 34 | ### Answers 35 | 36 | ```javascript 37 | // Example 38 | var firstNameLength = 0; 39 | var firstName = "Ada"; 40 | 41 | firstNameLength = firstName.length; 42 | 43 | // Setup 44 | var lastNameLength = 0; 45 | var lastName = "Lovelace"; 46 | 47 | // Only change code below this line. 48 | 49 | lastNameLength = lastName.length; 50 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Finding a Remainder in JavaScript.md: -------------------------------------------------------------------------------- 1 | # Finding a Remainder in JavaScript 2 | 3 | The remainder operator `%` gives the remainder of the division of two numbers. 4 | 5 | ## Example 6 | 7 | ```javascript 8 | 5 % 2 = 1 because 9 | Math.floor(5 / 2) = 2 (Quotient) 10 | 2 * 2 = 4 11 | 5 - 4 = 1 (Remainder) 12 | ``` 13 | 14 | ## Usage 15 | In mathematics, a number can be checked even or odd by checking the remainder 16 | of the division of the number by 2. 17 | 18 | ```javascript 19 | 17 % 2 = 1 (17 is Odd) 20 | 48 % 2 = 0 (48 is Even) 21 | ``` 22 | 23 | ## Note 24 | The remainder operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, 25 | but does not work properly with negative numbers. 26 | 27 | ## Instructions 28 | - Set remainder equal to the remainder of 11 divided by 3 using the remainder (%) operator. 29 | 30 | ### Before 31 | 32 | ```javascript 33 | // Only change code below this line 34 | 35 | var remainder; 36 | ``` 37 | 38 | ### Answers 39 | 40 | ```javascript 41 | // Only change code below this line 42 | 43 | var remainder = 11 % 3; 44 | ``` 45 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Generate Random Fractions with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Generate Random Fractions with JavaScript 2 | 3 | Random numbers are useful for creating random behavior. 4 | 5 | JavaScript has a `Math.random()` function that 6 | generates a random decimal number between 0 (inclusive) and 7 | not quite up to 1 (exclusive). Thus `Math.random()` can return a 0 but 8 | never quite return a 1 9 | 10 | ## Note 11 | *Like Storing Values with the Equal Operator, all function calls will be 12 | resolved before the return executes, 13 | so we can simply return the value of the Math.random() function.* 14 | 15 | ## Instructions 16 | - Change `randomFraction` to return a random number instead of returning 0. 17 | 18 | ### Before 19 | 20 | ```javascript 21 | function randomFraction() { 22 | 23 | // Only change code below this line. 24 | 25 | return 0; 26 | 27 | // Only change code above this line. 28 | } 29 | ``` 30 | 31 | ### Answers 32 | 33 | ```javascript 34 | function randomFraction() { 35 | 36 | // Only change code below this line. 37 | 38 | return Math.random(); 39 | 40 | // Only change code above this line. 41 | } 42 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Generate Random Whole Numbers with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Generate Random Whole Numbers with JavaScript 2 | 3 | It's great that we can generate random decimal numbers, 4 | but it's even more useful if we use it to generate random whole numbers. 5 | 6 | 1. Use `Math.random()` to generate a random decimal. 7 | 2. Multiply that random decimal by 20. 8 | 3. Use another function, `Math.floor()` to round the number 9 | down to its nearest whole number. 10 | 11 | Remember that `Math.random()` can never quite return a 1 and, 12 | because we're rounding down, it's impossible to actually get 20. 13 | This technique will give us a whole number between 0 and 19. 14 | 15 | Putting everything together, this is what our code looks like: 16 | 17 | `Math.floor(Math.random() * 20);` 18 | 19 | We are calling `Math.random()`, multiplying the result by 20, then 20 | passing the value to `Math.floor()` function to round the value down to 21 | the nearest whole number. 22 | 23 | ## Instructions 24 | - Use this technique to generate and return a random whole number between 25 | 0 and 9. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | var randomNumberBetween0and19 = Math.floor(Math.random() * 20); 31 | 32 | function randomWholeNum() { 33 | 34 | // Only change code below this line. 35 | 36 | return Math.random(); 37 | } 38 | ``` 39 | 40 | ### Answers 41 | 42 | ```javascript 43 | var randomNumberBetween0and19 = Math.floor(Math.random() * 20); 44 | 45 | function randomWholeNum() { 46 | 47 | // Only change code below this line. 48 | 49 | return Math.floor(Math.random() * 10); 50 | } 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Generate Random Whole Numbers within a Range.md: -------------------------------------------------------------------------------- 1 | # Generate Random Whole Numbers within a Range 2 | 3 | Instead of generating a random number between zero and a 4 | given number like we did before, we can generate a random number 5 | that falls within a range of two specific numbers. 6 | 7 | To do this, we'll define a minimum number min and a maximum number max. 8 | 9 | Here's the formula we'll use. Take a moment to read it 10 | and try to understand what this code is doing: 11 | 12 | `Math.floor(Math.random() * (max - min + 1)) + min` 13 | 14 | ## Instructions 15 | - Create a function called `randomRange` that takes a range `myMin` and `myMax` 16 | and returns a random number that's greater than or equal to `myMin`, and is 17 | less than or equal to `myMax`, inclusive. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | // Example 23 | function ourFunction(ourMin, ourMax) { 24 | 25 | return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin; 26 | } 27 | 28 | ourFunction(1, 9); 29 | 30 | // Only change code below this line. 31 | 32 | function randomRange(myMin, myMax) { 33 | 34 | return 0; // Change this line 35 | 36 | } 37 | 38 | // Change these values to test your function 39 | var myRandom = randomRange(5, 15); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | // Example 46 | function ourFunction(ourMin, ourMax) { 47 | 48 | return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin; 49 | } 50 | 51 | ourFunction(1, 9); 52 | 53 | // Only change code below this line. 54 | 55 | function randomRange(myMin, myMax) { 56 | 57 | return Math.floor(Math.random() * (myMax - myMin + 1) + myMin); 58 | 59 | } 60 | 61 | // Change these values to test your function 62 | var myRandom = randomRange(5, 15); 63 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Global Scope and Functions.md: -------------------------------------------------------------------------------- 1 | # Global Scope and Functions 2 | 3 | In JavaScript, `scope` refers to the visibility of variables. 4 | Variables which are defined outside of a function block have 5 | Global scope. **This means, they can be seen everywhere in your 6 | JavaScript code.** 7 | 8 | Variables which are **used without the var keyword are automatically 9 | created in the global scope**. This **can create unintended consequences 10 | elsewhere** in your code or when running a function again. **You should 11 | always declare your variables with var.** 12 | 13 | ## Instructions 14 | - Declare a global variable `myGlobal` outside of any function. 15 | Initialize it to have a value of `10` 16 | 17 | - Inside function `fun1`, assign `5` to `oopsGlobal` without using the var keyword. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | // Declare your variable here 23 | 24 | 25 | function fun1() { 26 | // Assign 5 to oopsGlobal Here 27 | 28 | } 29 | 30 | // Only change code above this line 31 | function fun2() { 32 | var output = ""; 33 | if (typeof myGlobal != "undefined") { 34 | output += "myGlobal: " + myGlobal; 35 | } 36 | if (typeof oopsGlobal != "undefined") { 37 | output += " oopsGlobal: " + oopsGlobal; 38 | } 39 | console.log(output); 40 | } 41 | ``` 42 | 43 | ### Answers 44 | 45 | ```javascript 46 | // Declare your variable here 47 | var myGlobal = 10; 48 | 49 | function fun1() { 50 | // Assign 5 to oopsGlobal Here 51 | oopsGlobal = 5; 52 | } 53 | 54 | // Only change code above this line 55 | function fun2() { 56 | var output = ""; 57 | if (typeof myGlobal != "undefined") { 58 | output += "myGlobal: " + myGlobal; 59 | } 60 | if (typeof oopsGlobal != "undefined") { 61 | output += " oopsGlobal: " + oopsGlobal; 62 | } 63 | console.log(output); 64 | 65 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Global vs Local Scope in Functions.md: -------------------------------------------------------------------------------- 1 | # Global vs Local Scope in Functions 2 | 3 | It is possible to have both `local` and `global` variables with the 4 | same name. When you do this, the `local` variable **takes precedence** 5 | over the `global` variable. 6 | 7 | ## Example 8 | 9 | ```javascript 10 | var someVar = "Hat"; 11 | function myFun() { 12 | var someVar = "Head"; 13 | return someVar; 14 | } 15 | ``` 16 | 17 | The function `myFun` will return `Head` because the `local` version of 18 | the variable is present. 19 | 20 | ## Instructions 21 | - Add a local variable to `myOutfit` to override the value of `outerWear` 22 | with `sweater` 23 | 24 | ### Before 25 | 26 | ```javascript 27 | // Setup 28 | var outerWear = "T-Shirt"; 29 | 30 | function myOutfit() { 31 | // Only change code below this line 32 | 33 | 34 | 35 | // Only change code above this line 36 | return outerWear; 37 | } 38 | 39 | myOutfit(); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | // Setup 46 | var outerWear = "T-Shirt"; 47 | 48 | function myOutfit() { 49 | // Only change code below this line 50 | 51 | var outerWear = "sweater"; 52 | 53 | 54 | // Only change code above this line 55 | return outerWear; 56 | } 57 | 58 | myOutfit(); 59 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Golf Code.md: -------------------------------------------------------------------------------- 1 | # Golf Code 2 | 3 | In the game of golf each hole has a `par` meaning the average number of 4 | `strokes` a golfer is expected to make in order to sink the ball in a 5 | hole to complete the play. Depending on how far above or below par 6 | your `strokes` are, there is a different nickname. 7 | 8 | Your function will be passed `par` and `strokes` `arguments`. Return the 9 | correct `string` according to this table which lists the strokes 10 | in order of priority; `top (highest) to bottom (lowest)`: 11 | 12 | Strokes | Return 13 | --- | --- 14 | 1 | "Hole-in-one!" 15 | <= par - 2 | "Eagle" 16 | par - 1 | "Birdie" 17 | par | "Par" 18 | par + 1 | "Bogey" 19 | par + 2 | "Double Bogey" 20 | >= par + 3 | "Go Home!" 21 | 22 | `par` and `strokes` will always be numeric and positive. 23 | 24 | ### Before 25 | 26 | ```javascript 27 | function golfScore(par, strokes) { 28 | // Only change code below this line 29 | 30 | 31 | return "Change Me"; 32 | // Only change code above this line 33 | } 34 | 35 | // Change these values to test 36 | golfScore(5, 4); 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | function golfScore(par, strokes) { 43 | // Only change code below this line 44 | 45 | if (strokes === 1) return "Hole-in-one!"; 46 | 47 | else if ((strokes - par) <= -2) return 'Eagle'; 48 | 49 | else if ((strokes - par) === -1) return 'Birdie'; 50 | 51 | else if (strokes === par) return 'Par'; 52 | 53 | else if ((strokes - par) === 1) return 'Bogey'; 54 | 55 | else if ((strokes - par) === 2) return 'Double Bogey'; 56 | 57 | else return 'Go Home!'; 58 | // Only change code above this line 59 | } 60 | 61 | // Change these values to test 62 | golfScore(5, 1); 63 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Increment a Number with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Increment a Number with JavaScript 2 | 3 | 4 | You can easily increment or add one to a variable with the `++` operator. 5 | 6 | ```javascript 7 | i++; 8 | ``` 9 | 10 | is the equivalent of 11 | 12 | ```javascript 13 | i = i + 1; 14 | ``` 15 | 16 | ## Note 17 | The entire line becomes `i++`;, eliminating the need for the equal sign. 18 | 19 | ## Instructions 20 | - Change the code to use the ++ operator on myVar. 21 | 22 | ## Hint 23 | 24 | Learn more about [Arithmetic operators - Increment (++)](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()) 25 | 26 | ### Before 27 | 28 | ```javascript 29 | var myVar = 87; 30 | 31 | // Only change code below this line 32 | //myVar = myVar + 1; 33 | 34 | myVar++; 35 | ``` 36 | 37 | ### Answers 38 | 39 | ```javascript 40 | var myVar = 87; 41 | 42 | // Only change code below this line 43 | myVar++; 44 | 45 | ``` 46 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Introducing Else If Statements.md: -------------------------------------------------------------------------------- 1 | # Introducing Else If Statements 2 | 3 | If you have multiple conditions that need to be addressed, 4 | you can chain `if` statements together with `else if` statements. 5 | 6 | ```javascript 7 | if (num > 15) { 8 | return "Bigger than 15"; 9 | } else if (num < 5) { 10 | return "Smaller than 5"; 11 | } else { 12 | return "Between 5 and 15"; 13 | } 14 | ``` 15 | 16 | ## Instructions 17 | -Convert the logic to use `else if` statements. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | function testElseIf(val) { 23 | if (val > 10) { 24 | return "Greater than 10"; 25 | } 26 | 27 | if (val < 5) { 28 | return "Smaller than 5"; 29 | } 30 | 31 | return "Between 5 and 10"; 32 | } 33 | 34 | // Change this value to test 35 | testElseIf(7); 36 | ``` 37 | 38 | ### Answers 39 | 40 | ```javascript 41 | function testElseIf(val) { 42 | if (val > 10) { 43 | return "Greater than 10"; 44 | } else if(val < 5) { 45 | return "Smaller than 5"; 46 | } else { 47 | return "Between 5 and 10"; 48 | } 49 | 50 | } 51 | 52 | // Change this value to test 53 | testElseIf(7); 54 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Introducing Else Statements.md: -------------------------------------------------------------------------------- 1 | # Introducing Else Statements 2 | 3 | When a condition for an `if` statement is `true`, the block of code 4 | following it is `executed`. What about when that condition is 5 | `false`? Normally nothing would happen. With an `else` statement, 6 | an `alternate` block of code can be executed. 7 | 8 | ```javascript 9 | if (num > 10) { 10 | return "Bigger than 10"; 11 | } else { 12 | return "10 or Less"; 13 | } 14 | ``` 15 | 16 | ## Instructions 17 | - Combine the `if` statements into a single `if/else` statement. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | function testElse(val) { 23 | var result = ""; 24 | // Only change code below this line 25 | 26 | if (val > 5) { 27 | result = "Bigger than 5"; 28 | } 29 | 30 | if (val <= 5) { 31 | result = "5 or Smaller"; 32 | } 33 | 34 | // Only change code above this line 35 | return result; 36 | } 37 | 38 | // Change this value to test 39 | testElse(4); 40 | ``` 41 | 42 | ### Answers 43 | 44 | ```javascript 45 | function testElse(val) { 46 | var result = ""; 47 | // Only change code below this line 48 | 49 | if (val > 5) { 50 | result = "Bigger than 5"; 51 | } else { 52 | result = "5 or Smaller"; 53 | } 54 | 55 | // Only change code above this line 56 | return result; 57 | } 58 | 59 | // Change this value to test 60 | testElse(4); 61 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Invert Regular Expression Matches with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Invert Regular Expression Matches with JavaScript 2 | 3 | You can **invert** any match by using the `uppercase` version of 4 | the regular expression selector. 5 | 6 | For example, `\s` will match any whitespace, and `\S` will match 7 | anything that isn't whitespace. 8 | 9 | ## Instructions 10 | - Use `/\S/g` to count the number of non-whitespace characters in testString. 11 | 12 | ### Before 13 | 14 | ```javascript 15 | // Setup 16 | var testString = "How many non-space characters are there in this sentence?"; 17 | 18 | // Only change code below this line. 19 | 20 | var expression = /.+/g; // Change this line 21 | 22 | // Only change code above this line 23 | 24 | // This code counts the matches of expression in testString 25 | var nonSpaceCount = testString.match(expression).length; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | // Setup 32 | var testString = "How many non-space characters are there in this sentence?"; 33 | 34 | // Only change code below this line. 35 | 36 | var expression = /\S/g; // Change this line 37 | 38 | // Only change code above this line 39 | 40 | // This code counts the matches of expression in testString 41 | var nonSpaceCount = testString.match(expression).length; 42 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Iterate Odd Numbers With a For Loop.md: -------------------------------------------------------------------------------- 1 | # Iterate Odd Numbers With a For Loop 2 | 3 | For loops don't have to iterate one at a time. By changing 4 | our final-expression, we can count by even numbers. 5 | 6 | We'll start at `i = 0` and loop while `i < 10`. We'll increment 7 | `i by 2` each loop with `i += 2`. 8 | 9 | ```javascript 10 | var ourArray = []; 11 | for (var i = 0; i < 10; i += 2) { 12 | ourArray.push(i); 13 | } 14 | ``` 15 | 16 | ourArray will now contain `[0,2,4,6,8]`. 17 | 18 | Let's change our initialization so we can count by odd numbers. 19 | 20 | ## Instructions 21 | - Push the odd numbers from 1 through 9 to `myArray` using a `for loop`. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | // Example 27 | var ourArray = []; 28 | 29 | for (var i = 0; i < 10; i += 2) { 30 | ourArray.push(i); 31 | } 32 | 33 | // Setup 34 | var myArray = []; 35 | 36 | // Only change code below this line. 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | // Example 43 | var ourArray = []; 44 | 45 | for (var i = 0; i < 10; i += 2) { 46 | ourArray.push(i); 47 | } 48 | 49 | // Setup 50 | var myArray = []; 51 | 52 | for (var i = 1; i < 10; i += 2) { 53 | myArray.push(i); 54 | } 55 | // Only change code below this line. 56 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Iterate Through an Array with a For Loop.md: -------------------------------------------------------------------------------- 1 | # Iterate Through an Array with a For Loop 2 | 3 | A common task in JavaScript is to iterate through the contents 4 | of an `array`. One way to do that is with a `for loop`. This code will 5 | output each element of the `array` arr to the console: 6 | 7 | ```javascript 8 | var arr = [10,9,8,7,6]; 9 | for (var i=0; i < arr.length; i++) { 10 | console.log(arr[i]); 11 | } 12 | ``` 13 | 14 | Remember that `Arrays` have `zero-based` numbering, which means 15 | the **last index of the array is length - 1**. Our condition for 16 | this loop is `i < arr.length`, which stops when `i is at length - 1`. 17 | 18 | ## Instructions 19 | - Declare and initialize a variable total to 0. Use a `for loop` to 20 | add the value of each element of the `myArr` array to total. 21 | 22 | ### Before 23 | 24 | ```javascript 25 | // Example 26 | var ourArr = [ 9, 10, 11, 12]; 27 | var ourTotal = 0; 28 | 29 | for (var i = 0; i < ourArr.length; i++) { 30 | ourTotal += ourArr[i]; 31 | } 32 | 33 | // Setup 34 | var myArr = [ 2, 3, 4, 5, 6]; 35 | 36 | // Only change code below this line 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | // Example 43 | var ourArr = [ 9, 10, 11, 12]; 44 | var ourTotal = 0; 45 | 46 | for (var i = 0; i < ourArr.length; i++) { 47 | ourTotal += ourArr[i]; 48 | } 49 | 50 | // Setup 51 | var myArr = [ 2, 3, 4, 5, 6]; 52 | var total = 0; 53 | 54 | for (var i = 0; i < myArr.length; i++) { 55 | total += myArr[i]; 56 | } 57 | // Only change code below this line 58 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Iterate with JavaScript While Loops.md: -------------------------------------------------------------------------------- 1 | # Iterate with JavaScript While Loops 2 | 3 | You can run the same code multiple times by using a loop. 4 | 5 | Another type of JavaScript loop is called a `while loop`, because 6 | it runs `while` a specified condition is true and **stops once 7 | that condition is no longer true**. 8 | 9 | ```javascript 10 | var ourArray = []; 11 | var i = 0; 12 | while(i < 5) { 13 | ourArray.push(i); 14 | i++; 15 | } 16 | ``` 17 | 18 | Let's try getting a while loop to work by pushing values to an array. 19 | 20 | ## Instructions 21 | - Push the numbers 0 through 4 to `myArray` using a `while loop`. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | // Setup 27 | var myArray = []; 28 | 29 | // Only change code below this line. 30 | ``` 31 | 32 | ### Answers 33 | 34 | ```javascript 35 | // Setup 36 | var myArray = []; 37 | var i = 0 ; 38 | while (i < 5) { 39 | myArray.push(i); 40 | i++; 41 | } 42 | 43 | // Only change code below this line. 44 | ``` 45 | 46 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Local Scope and Functions.md: -------------------------------------------------------------------------------- 1 | # Local Scope and Functions 2 | 3 | Variables which are declared within a `function`, as well as 4 | the `function parameters` have `local scope`. That means, **they are only 5 | visible within that function.** 6 | 7 | Here is a function `myTest` with a local variable called `loc`. 8 | 9 | ## Example 10 | 11 | ```javascript 12 | function myTest() { 13 | var loc = "foo"; 14 | console.log(loc); 15 | } 16 | myTest(); // "foo" 17 | console.log(loc); // "undefined" 18 | ``` 19 | 20 | `loc` is not defined outside of the function. 21 | 22 | ## Instructions 23 | - Declare a local variable `myVar` inside `myLocalScope`. Run the tests and 24 | then follow the instructions commented out in the editor. 25 | 26 | ## Hint 27 | - Refreshing the page may help if you get stuck. 28 | 29 | ### Before 30 | 31 | ```javascript 32 | function myLocalScope() { 33 | 'use strict'; 34 | 35 | 36 | console.log(myVar); 37 | } 38 | myLocalScope(); 39 | 40 | // Run and check the console 41 | // myVar is not defined outside of myLocalScope 42 | console.log(myVar); 43 | 44 | // Now remove the console log line to pass the test 45 | ``` 46 | 47 | ### Answers 48 | 49 | ```javascript 50 | function myLocalScope() { 51 | 'use strict'; 52 | 53 | var myVar = "Hello"; 54 | 55 | console.log(myVar); 56 | } 57 | myLocalScope(); 58 | 59 | // Run and check the console 60 | // myVar is not defined outside of myLocalScope 61 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Manipulate Arrays With pop.md: -------------------------------------------------------------------------------- 1 | # Manipulate Arrays With pop 2 | 3 | Another way to change the data in an array is with the `.pop()` function. 4 | 5 | .`pop()` is used to "pop" a value off of the end of an array. We can 6 | store this "popped off" value by assigning it to a variable. 7 | 8 | Any type of entry can be "popped" off of an 9 | `array - numbers, strings`, even `nested arrays`. 10 | 11 | ## Example 12 | 13 | ```javascript 14 | var oneDown = [1, 4, 6].pop(); 15 | ``` 16 | 17 | the variable oneDown now holds the value 6 and the array becomes `[1, 4]`. 18 | 19 | ## Instructions 20 | - Use the `.pop()` function to remove the last item from `myArray`, 21 | assigning the "popped off" value to `removedFromMyArray`. 22 | 23 | ### Before 24 | 25 | ```javascript 26 | // Example 27 | var ourArray = [1,2,3]; 28 | var removedFromOurArray = ourArray.pop(); 29 | // removedFromOurArray now equals 3, and ourArray now equals [1,2] 30 | 31 | // Setup 32 | var myArray = [["John", 23], ["cat", 2]]; 33 | 34 | // Only change code below this line. 35 | var removedFromMyArray; 36 | ``` 37 | 38 | ### Answers 39 | 40 | ```javascript 41 | // Example 42 | var ourArray = [1,2,3]; 43 | var removedFromOurArray = ourArray.pop(); 44 | // removedFromOurArray now equals 3, and ourArray now equals [1,2] 45 | 46 | // Setup 47 | var myArray = [["John", 23], ["cat", 2]]; 48 | 49 | // Only change code below this line. 50 | var removedFromMyArray = myArray.pop(); 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Manipulate Arrays With push.md: -------------------------------------------------------------------------------- 1 | # Manipulate Arrays With push 2 | 3 | An easy way to append data to the end of an array is via 4 | the `push()` function. 5 | 6 | `.push()` takes one or more `parameters` and 7 | "pushes" them onto the end of the array. 8 | 9 | ## Example 10 | 11 | ```javascript 12 | var arr = [1,2,3]; 13 | arr.push(4); 14 | // arr is now [1,2,3,4] 15 | ``` 16 | 17 | ## Instructions 18 | - Push `["dog", 3]` onto the end of the `myArray` variable. 19 | 20 | ### Before 21 | 22 | ```javascript 23 | // Example 24 | var ourArray = ["Stimpson", "J", "cat"]; 25 | ourArray.push(["happy", "joy"]); 26 | // ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]] 27 | 28 | // Setup 29 | var myArray = [["John", 23], ["cat", 2]]; 30 | 31 | // Only change code below this line. 32 | ``` 33 | 34 | ### Answers 35 | 36 | ```javascript 37 | // Example 38 | var ourArray = ["Stimpson", "J", "cat"]; 39 | ourArray.push(["happy", "joy"]); 40 | // ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]] 41 | 42 | // Setup 43 | var myArray = [["John", 23], ["cat", 2]]; 44 | 45 | // Only change code below this line. 46 | myArray.push(['dog', 3]); 47 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Manipulate Arrays With shift.md: -------------------------------------------------------------------------------- 1 | # Manipulate Arrays With shift 2 | 3 | `pop()` always removes the last element of an array. What if you want 4 | to remove the first? 5 | 6 | That's where `.shift()` comes in. It works just like `.pop()`, except it 7 | **removes the first element instead of the last**. 8 | 9 | ## Instructions 10 | - Use the `.shift()` function to remove the first item 11 | from `myArray`, assigning the "shifted off" value to `removedFromMyArray`. 12 | 13 | ### Before 14 | 15 | ```javascript 16 | // Example 17 | var ourArray = ["Stimpson", "J", ["cat"]]; 18 | removedFromOurArray = ourArray.shift(); 19 | // removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]]. 20 | 21 | // Setup 22 | var myArray = [["John", 23], ["dog", 3]]; 23 | 24 | // Only change code below this line. 25 | var removedFromMyArray; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | // Example 32 | var ourArray = ["Stimpson", "J", ["cat"]]; 33 | removedFromOurArray = ourArray.shift(); 34 | // removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]]. 35 | 36 | // Setup 37 | var myArray = [["John", 23], ["dog", 3]]; 38 | 39 | // Only change code below this line. 40 | var removedFromMyArray = myArray.shift(); 41 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Manipulate Arrays With unshift.md: -------------------------------------------------------------------------------- 1 | # Manipulate Arrays With unshift 2 | 3 | Not only can you `shift` elements off of the beginning of an array, 4 | you can also unshift elements to the beginning of an array i.e. 5 | add elements in front of the array. 6 | 7 | `.unshift()` works exactly like `.push()`, but instead of adding the 8 | element at the end of the array, `unshift()` **adds the element at the 9 | beginning of the array**. 10 | 11 | ## Instructions 12 | - Add `["Paul",35]` to the beginning of the `myArray` variable using `unshift()`. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | // Example 18 | var ourArray = ["Stimpson", "J", "cat"]; 19 | ourArray.shift(); // ourArray now equals ["J", "cat"] 20 | ourArray.unshift("Happy"); 21 | // ourArray now equals ["Happy", "J", "cat"] 22 | 23 | // Setup 24 | var myArray = [["John", 23], ["dog", 3]]; 25 | myArray.shift(); 26 | 27 | // Only change code below this line. 28 | ``` 29 | 30 | ### Answers 31 | 32 | ```javascript 33 | // Example 34 | var ourArray = ["Stimpson", "J", "cat"]; 35 | ourArray.shift(); // ourArray now equals ["J", "cat"] 36 | ourArray.unshift("Happy"); 37 | // ourArray now equals ["Happy", "J", "cat"] 38 | 39 | // Setup 40 | var myArray = [["John", 23], ["dog", 3]]; 41 | myArray.shift(); 42 | 43 | // Only change code below this line. 44 | myArray.unshift(["Paul", 35]); 45 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Modify Array Data With Indexes.md: -------------------------------------------------------------------------------- 1 | # Modify Array Data With Indexes 2 | 3 | Unlike strings, the entries of arrays are 4 | `mutable` and can be changed freely. 5 | 6 | ## Example 7 | 8 | ```javascript 9 | var ourArray = [3,2,1]; 10 | ourArray[0] = 1; // equals [1,2,1] 11 | ``` 12 | 13 | ## Instructions 14 | - Modify the data stored at index `0` of `myArray` to a value of `3`. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | // Example 20 | var ourArray = [1,2,3]; 21 | ourArray[1] = 3; // ourArray now equals [1,3,3]. 22 | 23 | // Setup 24 | var myArray = [1,2,3]; 25 | 26 | // Only change code below this line. 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | // Example 33 | var ourArray = [1,2,3]; 34 | ourArray[1] = 3; // ourArray now equals [1,3,3]. 35 | 36 | // Setup 37 | var myArray = [1,2,3]; 38 | 39 | // Only change code below this line. 40 | 41 | myArray[0] = 3; 42 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Multiple Identical Options in Switch Statements.md: -------------------------------------------------------------------------------- 1 | # Multiple Identical Options in Switch Statements 2 | 3 | If the `break` statement is omitted from a `switch` statement's `case`, the 4 | following `case` statement(s) are executed until a `break` is encountered. 5 | If you have multiple inputs with the same output, you can represent 6 | them in a `switch` statement like this: 7 | 8 | ```javascript 9 | switch(val) { 10 | case 1: 11 | case 2: 12 | case 3: 13 | result = "1, 2, or 3"; 14 | break; 15 | case 4: 16 | result = "4 alone"; 17 | } 18 | ``` 19 | 20 | Cases for 1, 2, and 3 will all produce the same result. 21 | 22 | ## Instructions 23 | - Write a `switch` statement to set answer for the following ranges: 24 | ``` 25 | 1-3 - "Low" 26 | 4-6 - "Mid" 27 | 7-9 - "High" 28 | ``` 29 | 30 | ## Note 31 | *You will need to have a case statement for each number in the range.* 32 | 33 | ### Before 34 | 35 | ```javascript 36 | function sequentialSizes(val) { 37 | var answer = ""; 38 | // Only change code below this line 39 | 40 | 41 | 42 | // Only change code above this line 43 | return answer; 44 | } 45 | 46 | // Change this value to test 47 | sequentialSizes(1); 48 | ``` 49 | 50 | ### Answers 51 | 52 | ```javascript 53 | function sequentialSizes(val) { 54 | var answer = ""; 55 | // Only change code below this line 56 | 57 | switch (val) { 58 | case 1: 59 | case 2: 60 | case 3: 61 | answer = "Low"; 62 | break; 63 | case 4: 64 | case 5: 65 | case 6: 66 | answer = "Mid"; 67 | break; 68 | case 7: 69 | case 8: 70 | case 9: 71 | answer = "High"; 72 | break; 73 | } 74 | 75 | 76 | // Only change code above this line 77 | return answer; 78 | } 79 | 80 | // Change this value to test 81 | sequentialSizes(1); 82 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Multiply Two Decimals with JavaScript.md: -------------------------------------------------------------------------------- 1 | # Multiply Two Decimals with JavaScript 2 | 3 | In JavaScript, you can also perform calculations with decimal 4 | numbers, just like whole numbers. 5 | 6 | Let's multiply two decimals together to get their product. 7 | 8 | ## Instructions 9 | - Change the 0.0 so that product will equal 5.0. 10 | 11 | 12 | ### Before 13 | 14 | ```javascript 15 | var product = 2.0 * 0.0; 16 | ``` 17 | 18 | ### Answers 19 | 20 | ```javascript 21 | var product = 2.0 * 2.5; 22 | ``` 23 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Multiply Two Numbers with JavaScript .md: -------------------------------------------------------------------------------- 1 | # Multiply Two Numbers with JavaScript 2 | 3 | We can also multiply one number by another. 4 | 5 | JavaScript uses the `*` symbol for multiplication of two numbers. 6 | 7 | ## Example 8 | 9 | ```javascript 10 | myVar = 13 * 13; // assigned 169 11 | ``` 12 | 13 | ## Instructions 14 | - Change the 0 so that product will equal 80. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | var product = 8 * 0; 20 | ``` 21 | 22 | ### Answers 23 | 24 | ```javascript 25 | var product = 8 * 10; 26 | ``` 27 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Nest one Array within Another Array.md: -------------------------------------------------------------------------------- 1 | # Nest one Array within Another Array 2 | 3 | You can also nest arrays within other arrays, 4 | like this: `[["Bulls", 23], ["White Sox", 45]]`. This is also called 5 | a Multi-dimensional Array. 6 | 7 | ## Instructions 8 | - Create a nested array called myArray. 9 | 10 | ### Before 11 | 12 | ```javascript 13 | // Example 14 | var ourArray = [["the universe", 42], ["everything", 101010]]; 15 | 16 | // Only change code below this line. 17 | var myArray = []; 18 | ``` 19 | 20 | ### Answers 21 | 22 | ```javascript 23 | // Example 24 | var ourArray = [["the universe", 42], ["everything", 101010]]; 25 | 26 | // Only change code below this line. 27 | var myArray = [["blabla", 40], ["I don't know", 1000000]]; 28 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Nesting For Loops.md: -------------------------------------------------------------------------------- 1 | # Nesting For Loops 2 | 3 | If you have a multi-dimensional array, you can use the same 4 | logic as the prior waypoint to loop through both the array and 5 | any sub-arrays. Here is an example: 6 | 7 | ```javascript 8 | var arr = [ 9 | [1,2], [3,4], [5,6] 10 | ]; 11 | for (var i=0; i < arr.length; i++) { 12 | for (var j=0; j < arr[i].length; j++) { 13 | console.log(arr[i][j]); 14 | } 15 | } 16 | ``` 17 | 18 | This outputs each sub-element in `arr` one at a time. Note that for 19 | the inner loop, we are checking the `.length` of `arr[i]`, since `arr[i]` 20 | is itself an `array`. 21 | 22 | ## Instructions 23 | - Modify function `multiplyAll` so that it multiplies the product 24 | variable by each number in the sub-arrays of `arr` 25 | 26 | ### Before 27 | 28 | ```javascript 29 | function multiplyAll(arr) { 30 | var product = 1; 31 | // Only change code below this line 32 | 33 | // Only change code above this line 34 | return product; 35 | } 36 | 37 | // Modify values below to test your code 38 | multiplyAll([[1,2],[3,4],[5,6,7]]); 39 | ``` 40 | 41 | ### Answers 42 | 43 | ```javascript 44 | function multiplyAll(arr) { 45 | var product = 1; 46 | // Only change code below this line 47 | 48 | for (var i = 0; i < arr.length; i++) { 49 | for (var x = 0; x < arr[i].length; x++) { 50 | product *= arr[i][x]; 51 | } 52 | } 53 | 54 | 55 | // Only change code above this line 56 | return product; 57 | } 58 | 59 | // Modify values below to test your code 60 | multiplyAll([[1,2],[3,4],[5,6,7]]); 61 | ``` 62 | 63 | ### Thinking 64 | 65 | 1. Need to loop the first array for find all the sub one 66 | 2. Loop inside the sub one for get all the values 67 | 3. Use *= for multiply product to the values of the sub arr -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Passing Values to Functions with Arguments.md: -------------------------------------------------------------------------------- 1 | # Passing Values to Functions with Arguments 2 | 3 | Parameters are variables that act as placeholders for the 4 | values that are to be input to a function when it is called. 5 | When a function is defined, it is typically defined along with 6 | one or more parameters. The actual values that are input (or "passed") 7 | into a function when it is called are known as `arguments`. 8 | 9 | Here is a function with two parameters, `param1` and `param2`: 10 | 11 | ```javascript 12 | function testFun(param1, param2) { 13 | console.log(param1, param2); 14 | } 15 | ``` 16 | 17 | Then we can call testFun: 18 | 19 | `testFun("Hello", "World");` 20 | 21 | We have passed two arguments, `"Hello"` and `"World"`. Inside the function, 22 | param1 will equal "Hello" and param2 will equal "World". Note that 23 | you could call testFun again with different arguments and the 24 | parameters would take on the value of the new arguments. 25 | 26 | ## Instructions 27 | - Create a function called `functionWithArgs` that accepts two arguments and 28 | outputs their `sum` to the dev console. 29 | - Call the function. 30 | 31 | ### Before 32 | 33 | ```javascript 34 | // Example 35 | function functionWithArgs(a, b) { 36 | console.log(a - b); 37 | } 38 | functionWithArgs(10, 5); // Outputs 5 39 | 40 | // Only change code below this line. 41 | ``` 42 | 43 | ### Answers 44 | 45 | ```javascript 46 | // Example 47 | function functionWithArgs(a, b) { 48 | console.log(a + b); 49 | } 50 | functionWithArgs(10, 5); // Outputs 5 51 | 52 | // Only change code below this line. 53 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Quoting Strings with Single Quotes.md: -------------------------------------------------------------------------------- 1 | # Quoting Strings with Single Quotes 2 | 3 | String values in JavaScript may be written with single or double quotes, 4 | so long as you start and end with the same type of quote. 5 | Unlike some languages, single and double quotes are functionally 6 | identical in JavaScript. 7 | 8 | `"This string has \"double quotes\" in it"` 9 | 10 | The value in using one or the other has to do with the need 11 | to escape quotes of the same type. Unless they are escaped, you cannot 12 | have more than one pair of whichever quote type begins a string. 13 | 14 | If you have a string with many double quotes, this can be difficult 15 | to read and write. Instead, use single quotes: 16 | 17 | `'This string has "double quotes" in it. And "probably" lots of them.'` 18 | 19 | ## Instructions 20 | - Change the provided string from double to single quotes and remove the escaping. 21 | 22 | ### Before 23 | 24 | ```javascript 25 | var myStr = "Link"; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | var myStr = 'Link'; 32 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Return Early Pattern for Functions.md: -------------------------------------------------------------------------------- 1 | # Return Early Pattern for Functions 2 | 3 | When a return statement is reached, the execution of the current 4 | function stops and control returns to the calling location. 5 | 6 | ## Example 7 | 8 | ```javascript 9 | function myFun() { 10 | console.log("Hello"); 11 | return "World"; 12 | console.log("byebye") 13 | } 14 | myFun(); 15 | ``` 16 | 17 | The above outputs `"Hello"` to the console, returns `"World"`, but 18 | `"byebye"` is never output, because the **function exits** at the **return statement.** 19 | 20 | ## Instructions 21 | - Modify the function `abTest` so that `if` `a` or `b` are less than `0` the 22 | function will immediately exit with a value of `undefined`. 23 | 24 | ## Hint 25 | - Remember that `undefined` is a keyword, not a `string`. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | function abTest(a, b) { 31 | // Only change code below this line 32 | 33 | 34 | 35 | // Only change code above this line 36 | 37 | return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); 38 | } 39 | 40 | // Change values below to test your code 41 | abTest(2,2); 42 | ``` 43 | 44 | ### Answers 45 | 46 | ```javascript 47 | // Setup 48 | function abTest(a, b) { 49 | // Only change code below this line 50 | 51 | if (a < 0 || b < 0) return undefined; 52 | 53 | // Only change code above this line 54 | 55 | return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); 56 | } 57 | 58 | // Change values below to test your code 59 | abTest(2,2); 60 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Return a Value from a Function with Return.md: -------------------------------------------------------------------------------- 1 | # Return a Value from a Function with Return 2 | 3 | We can pass values into a function with arguments. You can use a 4 | `return` statement to send a value back out of a `function`. 5 | 6 | ## Example 7 | 8 | ```javascript 9 | function plusThree(num) { 10 | return num + 3; 11 | } 12 | var answer = plusThree(5); // 8 13 | ``` 14 | 15 | `plusThree` takes an argument for `num` and returns a value equal to `num + 3`. 16 | 17 | ## Instructions 18 | - Create a function `timesFive` that accepts one argument, multiplies it by `5`, 19 | and returns the new value. 20 | 21 | ### Before 22 | 23 | ```javascript 24 | // Example 25 | function minusSeven(num) { 26 | return num - 7; 27 | } 28 | 29 | // Only change code below this line 30 | ``` 31 | 32 | ### Answers 33 | 34 | ```javascript 35 | // Example 36 | function minusSeven(num) { 37 | return num - 7; 38 | } 39 | 40 | // Only change code below this line 41 | function timesFive(num) { 42 | return num * 5; 43 | } 44 | 45 | var answer = timesFive(5); 46 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Returning Boolean Values from Functions.md: -------------------------------------------------------------------------------- 1 | # Returning Boolean Values from Functions 2 | 3 | You may recall from [Comparison with the Equality Operator](https://www.freecodecamp.com/challenges/waypoint-comparison-with-the-equality-operator) 4 | that all comparison operators return a `boolean` `true` or `false` value. 5 | 6 | A common anti-pattern is to use an `if/else` statement to do a 7 | comparison and then return `true/false`: 8 | 9 | ```javascript 10 | function isEqual(a,b) { 11 | if (a === b) { 12 | return true; 13 | } else { 14 | return false; 15 | } 16 | } 17 | ``` 18 | 19 | Since `===` returns `true` or `false`, we can simply return the 20 | result of the comparison: 21 | 22 | ```javascript 23 | function isEqual(a,b) { 24 | return a === b; 25 | } 26 | ``` 27 | ## Instructions 28 | - Fix the function `isLess` to remove the `if/else` statements. 29 | 30 | ### Before 31 | 32 | ```javascript 33 | function isLess(a, b) { 34 | // Fix this code 35 | if (a < b) { 36 | return true; 37 | } else { 38 | return false; 39 | } 40 | } 41 | 42 | // Change these values to test 43 | isLess(10, 15); 44 | ``` 45 | 46 | ### Answers 47 | 48 | ```javascript 49 | function isLess(a, b) { 50 | // Fix this code 51 | return a < b; 52 | } 53 | 54 | // Change these values to test 55 | isLess(10, 15); 56 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Shopping List.md: -------------------------------------------------------------------------------- 1 | # Shopping List 2 | 3 | Create a shopping list in the variable `myList`. The list 4 | should be a multi-dimensional array containing several sub-arrays. 5 | 6 | The first element in each sub-array should contain a string 7 | with the name of the item. The second element should be a number 8 | representing the quantity i.e. 9 | 10 | `["Chocolate Bar", 15]` 11 | 12 | There should be at least 5 sub-arrays in the list. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | var myList = []; 18 | ``` 19 | 20 | ### Answers 21 | 22 | ```javascript 23 | var myList = [["Banana", 3], ["Apple", 4], ["Orange", 5], ["Peach", 6], ["Pineaple", 7]]; 24 | ``` 25 | 26 | ### Thinking 27 | 28 | Here we just need to setup 5 sub-array and each array need to have a string 29 | and a number. Pretty straight-forward. -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Stand in Line.md: -------------------------------------------------------------------------------- 1 | # Stand in Line 2 | 3 | In Computer Science a `queue` is an abstract `Data Structure` 4 | where items are kept in order. New items can be added at the back 5 | of the `queue` and old items are taken off from the front of the `queue`. 6 | 7 | ## Instructions 8 | - Write a function `nextInLine` which takes an `array` 9 | (`arr`) and a `number` (`item`) as `arguments`. 10 | - Add the `number` to the `end of the array`. 11 | - Then remove the `first element of array`. The `nextInLine` function 12 | should then return the element that was removed. 13 | 14 | ### Before 15 | 16 | ```javascript 17 | function nextInLine(arr, item) { 18 | // Your code here 19 | 20 | return item; // Change this line 21 | } 22 | 23 | // Test Setup 24 | var testArr = [1,2,3,4,5]; 25 | 26 | // Display Code 27 | console.log("Before: " + JSON.stringify(testArr)); 28 | console.log(nextInLine(testArr, 6)); // Modify this line to test 29 | console.log("After: " + JSON.stringify(testArr)); 30 | ``` 31 | 32 | ### Answers 33 | 34 | ```javascript 35 | function nextInLine(arr, item) { 36 | // Your code here 37 | 38 | var queue = arr.push(item); 39 | 40 | var removeItem = arr.shift(); 41 | 42 | return removeItem; // Change this line 43 | } 44 | 45 | // Test Setup 46 | var testArr = [1,2,3,4,5]; 47 | 48 | // Display Code 49 | console.log("Before: " + JSON.stringify(testArr)); 50 | console.log(nextInLine(testArr, 6)); // Modify this line to test 51 | console.log("After: " + JSON.stringify(testArr)); 52 | 53 | ``` 54 | 55 | ### Thinking 56 | 57 | Here I just follow what they ask 58 | 1. Add number at the end of the array. So I use `.push()` 59 | 2. Remove the first element. `.shift()` 60 | 3. Return the element was removed. `return removeItem` After I setup the var 61 | with the `.shift()` method. -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Store Multiple Values in one Variable using JavaScript Arrays.md: -------------------------------------------------------------------------------- 1 | # Store Multiple Values in one Variable using JavaScript Arrays 2 | 3 | With JavaScript `array` variables, we can store several pieces of data 4 | in one place. 5 | 6 | You start an array declaration with an opening square bracket, 7 | end it with a closing square bracket, and put a comma between each 8 | entry, like this: 9 | 10 | `var sandwich = ["peanut butter", "jelly", "bread"].` 11 | 12 | ## Instructions 13 | - Modify the new array `myArray` so that it contains both a `string` and a 14 | `number` (in that order). 15 | 16 | ## Hint 17 | - Refer to the example code in the text editor if you get stuck. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | // Example 23 | var array = ["John", 23]; 24 | 25 | // Only change code below this line. 26 | var myArray = []; 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | // Example 33 | var array = ["John", 23]; 34 | 35 | // Only change code below this line. 36 | var myArray = ["Emanuel", 25]; 37 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Storing Values With The Assignment Operator.md: -------------------------------------------------------------------------------- 1 | # Storing Values With the assignment Operator 2 | 3 | In JavaScript, you can store a value in a variable with the assignment operator. 4 | 5 | `myVariable = 5;` 6 | 7 | Assigns the Number value 5 to myVariable. 8 | 9 | >Assignment always goes from right to left. 10 | 11 | Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator. 12 | 13 | ```javascript 14 | myVar = 5; 15 | myNum = myVar; 16 | 17 | ``` 18 | 19 | Assigns 5 to myVar and then resolves myVar to 5 again and assigns it to myNum. 20 | 21 | ## Instructions 22 | 23 | - Assign the value 7 to variable a. 24 | - Assign the contents of a to variable b. 25 | 26 | ### Answers 27 | 28 | ```javascript 29 | // Setup 30 | var a; 31 | var b = 2; 32 | 33 | // Only change code below this line 34 | a = 7; 35 | b = 7; 36 | b = a; // Assign the contents of a to var b /> so we need to have b at left cause we assign the right 37 | 38 | ``` 39 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Subtract One Number from Another with JavaScript .md: -------------------------------------------------------------------------------- 1 | # Subtract One Number from Another with JavaScript 2 | 3 | We can also subtract one number from another. 4 | 5 | JavaScript uses the `-` symbol for subtraction. 6 | 7 | ## Example 8 | 9 | ```javascript 10 | myVar = 12 - 6; // assigned 6 11 | ``` 12 | 13 | ## Instructions 14 | - Change the 0 so the difference is 12. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | var difference = 45 - 0; 20 | ``` 21 | 22 | ### Answers 23 | 24 | ```javascript 25 | var difference = 45 - 33; 26 | ``` 27 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Testing Objects for Properties.md: -------------------------------------------------------------------------------- 1 | # Testing Objects for Properties 2 | 3 | Sometimes it is useful to check if the property of a given `object` exists 4 | or not. We can use the `.hasOwnProperty(propname)` method of `objects` to 5 | determine if that object has the given property name. `.hasOwnProperty()` 6 | returns `true` or `false` `if` the property is found or not. 7 | 8 | ## Example 9 | 10 | ```javascript 11 | var myObj = { 12 | top: "hat", 13 | bottom: "pants" 14 | }; 15 | myObj.hasOwnProperty("top"); // true 16 | myObj.hasOwnProperty("middle"); // false 17 | ``` 18 | 19 | ## Instructions 20 | - Modify the function `checkObj` to test `myObj` for `checkProp`. If 21 | the property is found, return that property's value. If not, return "Not Found". 22 | 23 | ### Before 24 | 25 | ```javascript 26 | // Setup 27 | var myObj = { 28 | gift: "pony", 29 | pet: "kitten", 30 | bed: "sleigh" 31 | }; 32 | 33 | function checkObj(checkProp) { 34 | // Your Code Here 35 | 36 | return "Change Me!"; 37 | } 38 | 39 | // Test your code by modifying these values 40 | checkObj("gift"); 41 | ``` 42 | 43 | ### Answers 44 | 45 | ```javascript 46 | // Setup 47 | var myObj = { 48 | gift: "pony", 49 | pet: "kitten", 50 | bed: "sleigh" 51 | }; 52 | 53 | function checkObj(checkProp) { 54 | // Your Code Here 55 | if (myObj.hasOwnProperty(checkProp)) return myObj[checkProp]; // If true return the prop 56 | 57 | return "Not Found"; 58 | } 59 | 60 | // Test your code by modifying these values 61 | checkObj("gift"); 62 | ``` 63 | 64 | ### Thinking 65 | 66 | Here because the args pass is a string, that mean he have quote. We need 67 | to use bracket notation. -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Understand String Immutability.md: -------------------------------------------------------------------------------- 1 | # Understand String Immutability 2 | 3 | In JavaScript, `String` values are `immutable`, which means that 4 | they cannot be altered once created. 5 | 6 | For example, the following code: 7 | 8 | ```javascript 9 | var myStr = "Bob"; 10 | myStr[0] = "J"; 11 | ``` 12 | cannot change the value of `myStr` to "Job", because the contents 13 | of `myStr` cannot be altered. Note that this does not mean that `myStr` 14 | cannot be changed, just that the individual characters of a string 15 | literal cannot be changed. The only way to change myStr would be to assign 16 | it with a new string, like this: 17 | 18 | ```javascript 19 | var myStr = "Bob"; 20 | myStr = "Job"; 21 | ``` 22 | 23 | ## Instructions 24 | - Correct the assignment to `myStr` to achieve the desired effect. 25 | 26 | ### Before 27 | 28 | ```javascript 29 | // Setup 30 | var myStr = "Jello World"; 31 | 32 | // Only change code below this line 33 | 34 | myStr[0] = "H"; // Fix Me 35 | ``` 36 | 37 | ### Answers 38 | 39 | ```javascript 40 | // Setup 41 | var myStr = "Jello World"; 42 | 43 | // Only change code below this line 44 | 45 | var newStr = myStr.slice(1); 46 | 47 | myStr = 'H' + newStr; 48 | ``` 49 | 50 | ### Thinking 51 | 52 | Here I got a problem with the string so I try to figured out a answer. 53 | 54 | 1. I want to remove the first letter of `myStr` so I make a new var and use `.slice` 55 | 2. I push a new letter to `myStr` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Understanding Boolean Values.md: -------------------------------------------------------------------------------- 1 | # Understanding Boolean Values 2 | 3 | Another data type is the `Boolean`. `Booleans` may only be one of two 4 | values: `true` or `false`. They are basically **little on-off switches**, 5 | where `true is "on"` and `false is "off."` These **two states** are mutually 6 | exclusive. 7 | 8 | ## Note 9 | *Boolean values are never written with quotes. The strings "true" 10 | and "false" are not Boolean and have no special meaning in JavaScript.* 11 | 12 | ## Instructions 13 | - Modify the `welcomeToBooleans` function so that it returns `true` instead of 14 | `false` when the run button is clicked. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | function welcomeToBooleans() { 20 | 21 | // Only change code below this line. 22 | 23 | return false; // Change this line 24 | 25 | // Only change code above this line. 26 | } 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | function welcomeToBooleans() { 33 | 34 | // Only change code below this line. 35 | 36 | return true; // Change this line 37 | 38 | // Only change code above this line. 39 | } 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Understanding Case Sensitivity in Variables.md: -------------------------------------------------------------------------------- 1 | # Understanding Case Sensitivity in Variables 2 | 3 | In JavaScript all variables and function names are case sensitive. This means that capitalization matters. 4 | 5 | MYVAR is not the same as MyVar nor myvar. It is possible to 6 | have multiple distinct 7 | variables with the same name but different casing. 8 | It is strongly recommended that for the sake of 9 | clarity, you do not use this language feature. 10 | 11 | ## Best Practice 12 | Write variable names in Javascript in `camelCase`. In `camelCase`, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized. 13 | 14 | ## Examples: 15 | 16 | ```javascript 17 | var someVariable; 18 | var anotherVariableName; 19 | var thisVariableNameIsTooLong; 20 | ``` 21 | 22 | ## Instructions 23 | - Modify the existing declarations and assignments so their names use camelCase. 24 | - Do not create any new variables. 25 | 26 | 27 | ### Answers 28 | 29 | ```javascript 30 | // Declarations 31 | var studlyCapVar; 32 | var properCamelCase; 33 | var titleCaseOver; 34 | 35 | // Assignments 36 | studlyCapVar = 10; 37 | properCamelCase = "A String"; 38 | titleCaseOver = 9000; 39 | 40 | ``` 41 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Understanding Uninitialized Variables.md: -------------------------------------------------------------------------------- 1 | # Understanding Uninitialized Variables 2 | 3 | When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical 4 | operation on an undefined variable your result will be `NaN` which means "Not a Number". 5 | If you concatenate a string with an undefined variable, you will get a literal string of "undefined". 6 | 7 | ## Instructions 8 | - Initialize the three variables a, b, and c with 5, 10, and "I am a" respectively so that they will not be undefined. 9 | 10 | ### Answers 11 | 12 | ```javascript 13 | // Initialize these three variables 14 | var a = 5; 15 | var b = 10; 16 | var c = 'I am a'; 17 | 18 | // Do not change code below this line 19 | 20 | a = a + 1; 21 | b = b + 5; 22 | c = c + " String!"; 23 | 24 | ``` 25 | 26 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Use Bracket Notation to Find the First Character in a String.md: -------------------------------------------------------------------------------- 1 | # Use Bracket Notation to Find the First Character in a String 2 | 3 | `Bracket notation` is a way to get a character at a specific index 4 | within a string. 5 | 6 | Most modern programming languages, like JavaScript, **don't start counting 7 | at 1 like humans do. They start at 0.** This is referred to as Zero-based 8 | indexing. 9 | 10 | For example, the character at index 0 in the word "Charles" is "C". So if `var 11 | firstName = "Charles"`, you can get the value of the first letter 12 | of the string by using `firstName[0]`. 13 | 14 | ## Instructions 15 | - Use bracket notation to find the first character in the `lastName` 16 | variable and assign it to `firstLetterOfLastName`. 17 | 18 | ## Hint 19 | - Try looking at the `firstLetterOfFirstName` variable declaration if you 20 | get stuck. 21 | 22 | ### Before 23 | 24 | ```javascript 25 | // Example 26 | var firstLetterOfFirstName = ""; 27 | var firstName = "Ada"; 28 | 29 | firstLetterOfFirstName = firstName[0]; 30 | 31 | // Setup 32 | var firstLetterOfLastName = ""; 33 | var lastName = "Lovelace"; 34 | 35 | // Only change code below this line 36 | firstLetterOfLastName = lastName; 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | // Example 43 | var firstLetterOfFirstName = ""; 44 | var firstName = "Ada"; 45 | 46 | firstLetterOfFirstName = firstName[0]; 47 | 48 | // Setup 49 | var firstLetterOfLastName = ""; 50 | var lastName = "Lovelace"; 51 | 52 | // Only change code below this line 53 | firstLetterOfLastName = lastName[0]; 54 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Use Bracket Notation to Find the Last Character in a String.md: -------------------------------------------------------------------------------- 1 | # Use Bracket Notation to Find the Last Character in a String 2 | 3 | In order to get the last letter of a string, you can subtract one 4 | from the string's length. 5 | 6 | For example, if `var firstName = "Charles",` you can get the value of the 7 | last letter of the string by using `firstName[firstName.length - 1]`. 8 | 9 | ## Instructions 10 | - Use `bracket notation` to find the last character in the `lastName` variable. 11 | 12 | ## Hint 13 | - Try looking at the `lastLetterOfFirstName` variable declaration if you get 14 | stuck. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | // Example 20 | var firstName = "Ada"; 21 | var lastLetterOfFirstName = firstName[firstName.length - 1]; 22 | 23 | // Setup 24 | var lastName = "Lovelace"; 25 | 26 | // Only change code below this line. 27 | var lastLetterOfLastName = lastName; 28 | ``` 29 | 30 | ### Answers 31 | 32 | ```javascript 33 | // Example 34 | var firstName = "Ada"; 35 | var lastLetterOfFirstName = firstName[firstName.length - 1]; 36 | 37 | // Setup 38 | var lastName = "Lovelace"; 39 | 40 | // Only change code below this line. 41 | var lastLetterOfLastName = lastName[lastName.length - 1]; 42 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Use Bracket Notation to Find the Nth Character in a String.md: -------------------------------------------------------------------------------- 1 | # Use Bracket Notation to Find the Nth Character in a String 2 | 3 | You can also use `bracket notation` to get the character at other positions 4 | within a string. 5 | 6 | Remember that computers start counting at 0, so the first character is 7 | actually the zeroth character. 8 | 9 | ## Instructions 10 | - Let's try to set thirdLetterOfLastName to equal the third letter of the 11 | `lastName` variable using `bracket notation`. 12 | 13 | ##Hint 14 | - Try looking at the `secondLetterOfFirstName` variable declaration if you get 15 | stuck. 16 | 17 | ### Before 18 | 19 | ```javascript 20 | // Example 21 | var firstName = "Ada"; 22 | var secondLetterOfFirstName = firstName[1]; 23 | 24 | // Setup 25 | var lastName = "Lovelace"; 26 | 27 | // Only change code below this line. 28 | var thirdLetterOfLastName = lastName; 29 | ``` 30 | 31 | ### Answers 32 | 33 | ```javascript 34 | // Example 35 | var firstName = "Ada"; 36 | var secondLetterOfFirstName = firstName[1]; 37 | 38 | // Setup 39 | var lastName = "Lovelace"; 40 | 41 | // Only change code below this line. 42 | var thirdLetterOfLastName = lastName[2]; 43 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Use Bracket Notation to Find the NthtoLast Character in a String.md: -------------------------------------------------------------------------------- 1 | # Use Bracket Notation to Find the NthtoLast Character in a String 2 | 3 | You can use the same principle we just used to retrieve the last 4 | character in a string to retrieve the Nth-to-last character. 5 | 6 | For example, you can get the value of the third-to-last letter of the 7 | `var firstName = "Charles"` string by using `firstName[firstName.length - 3]` 8 | 9 | ## Instructions 10 | - Use `bracket notation` to find the second-to-last character in the `lastName` 11 | string. 12 | 13 | ## Hint 14 | - Try looking at the `thirdToLastLetterOfFirstName` variable declaration if you get stuck. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | // Example 20 | var firstName = "Ada"; 21 | var thirdToLastLetterOfFirstName = firstName[firstName.length - 3]; 22 | 23 | // Setup 24 | var lastName = "Lovelace"; 25 | 26 | // Only change code below this line 27 | var secondToLastLetterOfLastName = lastName; 28 | ``` 29 | 30 | ### Answers 31 | 32 | ```javascript 33 | // Example 34 | var firstName = "Ada"; 35 | var thirdToLastLetterOfFirstName = firstName[firstName.length - 3]; 36 | 37 | // Setup 38 | var lastName = "Lovelace"; 39 | 40 | // Only change code below this line 41 | var secondToLastLetterOfLastName = lastName[lastName.length - 2]; 42 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Word Blanks.md: -------------------------------------------------------------------------------- 1 | # Word Blanks 2 | 3 | We will now use our knowledge of strings to build a "Mad Libs" style word 4 | game we're calling "Word Blanks". You will create an (optionally humorous) 5 | "Fill in the Blanks" style sentence. 6 | 7 | You will need to use string operators to build a new string, result, 8 | using the provided variables: `myNoun, myAdjective, myVerb,` and `myAdverb`. 9 | 10 | You will also need to use additional strings, which will not change, 11 | and must be in between all of the provided words. The output should be a 12 | complete sentence. 13 | 14 | We have provided a framework for testing your results with different 15 | words. The tests will run your function with several different inputs 16 | to make sure all of the provided words appear in the output, as well as 17 | your extra strings. 18 | 19 | ### Before 20 | 21 | ```javascript 22 | function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { 23 | var result = ""; 24 | // Your code below this line 25 | 26 | 27 | // Your code above this line 28 | return result; 29 | } 30 | 31 | // Change the words here to test your function 32 | wordBlanks("dog", "big", "ran", "quickly"); 33 | ``` 34 | 35 | ### Answers 36 | 37 | ```javascript 38 | function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { 39 | var result = ""; 40 | // Your code below this line 41 | 42 | result = "My " + myNoun + " is " + myAdjective + " and " + myVerb + " " + myAdverb + "!"; 43 | 44 | // Your code above this line 45 | return result; 46 | } 47 | 48 | // Change the words here to test your function 49 | wordBlanks("dog", "big", "ran", "quickly"); 50 | 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/Write Reusable JavaScript with Functions.md: -------------------------------------------------------------------------------- 1 | # Write Reusable JavaScript with Functions 2 | 3 | In JavaScript, we can divide up our code into reusable parts called `functions`. 4 | 5 | Here's an example of a function: 6 | 7 | ```javascript 8 | function functionName() { 9 | console.log("Hello World"); 10 | } 11 | ``` 12 | 13 | You can call or invoke this function by using its name followed by parentheses, 14 | like this: 15 | 16 | `functionName();` 17 | 18 | Each time the function is called it will print out the message 19 | `Hello World` on the dev console. All of the code between 20 | the curly braces will be executed every time the function is called. 21 | 22 | ## Instructions 23 | - Create a function called `reusableFunction` which prints `"Hi World"` to 24 | the dev console. 25 | - Call the function. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | // Example 31 | function reusableFunction() { 32 | console.log("Heyya, World"); 33 | } 34 | 35 | reusableFunction(); 36 | 37 | // Only change code below this line 38 | ``` 39 | 40 | ### Answers 41 | 42 | ```javascript 43 | // Example 44 | function reusableFunction() { 45 | console.log("Hi World"); 46 | } 47 | 48 | reusableFunction(); 49 | 50 | // Only change code below this line 51 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Basic JavaScript/initializingVariablesWithTheAssignmentOperator.md: -------------------------------------------------------------------------------- 1 | # Initializing Variables With the Assignment Operator 2 | 3 | It is common to initialize a variable to an initial value in the same line as it is declared. 4 | `var myVar = 0;` 5 | Creates a new variable called myVar and assigns it an initial value of 0. 6 | ## Instructions 7 | Define a variable a with var and initialize it to a value of 9. 8 | 9 | ### Before 10 | 11 | ```javascript 12 | // Example 13 | var ourVar = 19; 14 | 15 | // Only change code below this line 16 | ``` 17 | 18 | ### Answers 19 | 20 | ```javascript 21 | // Example 22 | var ourVar = 19; 23 | 24 | // Only change code below this line 25 | var a = 9; 26 | 27 | ``` 28 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Apply Functional Programming to Convert Strings to URL Slugs.md: -------------------------------------------------------------------------------- 1 | # Apply Functional Programming to Convert Strings to URL Slugs 2 | 3 | Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace. Here are the requirements: 4 | 5 | The input is a string with spaces and title-cased words 6 | 7 | The output is a string with the spaces between words replaced by a hyphen (-) 8 | 9 | The output should be all lower-cased letters 10 | 11 | The output should not have any spaces 12 | 13 | ### Before 14 | 15 | ```js 16 | // the global variable 17 | var globalTitle = "Winter Is Coming"; 18 | 19 | // Add your code below this line 20 | function urlSlug(title) { 21 | 22 | 23 | } 24 | // Add your code above this line 25 | 26 | var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming" 27 | ``` 28 | 29 | ### Answer 30 | 31 | ```js 32 | // the global variable 33 | var globalTitle = "Winter Is Coming"; 34 | 35 | // Add your code below this line 36 | function urlSlug(title) { 37 | const parseTitle = title.toLowerCase().trim(); 38 | 39 | return parseTitle.split(/\W/).filter(el => el.length > 0).join('-'); 40 | } 41 | // Add your code above this line 42 | 43 | var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming" 44 | ``` 45 | 46 | ### Thinking 47 | 48 | I'm so bad in regex so I filter those empty space :) 49 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Combine an Array into a String Using the join Method.md: -------------------------------------------------------------------------------- 1 | # Combine an Array into a String Using the join Method 2 | 3 | Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string. For example, "I-like-Star-Wars" would be converted to "I like Star Wars". For this challenge, do not use the replace method. 4 | 5 | ### Before 6 | 7 | ```js 8 | function sentensify(str) { 9 | // Add your code below this line 10 | 11 | 12 | // Add your code above this line 13 | } 14 | sentensify("May-the-force-be-with-you"); 15 | ``` 16 | 17 | ### Answer 18 | 19 | ```js 20 | function sentensify(str) { 21 | // Add your code below this line 22 | 23 | return str.split(/\W/).join(' '); 24 | // Add your code above this line 25 | } 26 | sentensify("May-the-force-be-with-you"); 27 | ``` 28 | 29 | ### Thinking 30 | 31 | 1. We need to split first on non-letters and join right after with a space. 32 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Return a Sorted Array Without Changing the Original Array.md: -------------------------------------------------------------------------------- 1 | # Return a Sorted Array Without Changing the Original Array 2 | 3 | Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray variable. 4 | 5 | ### Before 6 | 7 | ```js 8 | var globalArray = [5, 6, 3, 2, 9]; 9 | function nonMutatingSort(arr) { 10 | // Add your code below this line 11 | 12 | 13 | // Add your code above this line 14 | } 15 | nonMutatingSort(globalArray); 16 | ``` 17 | 18 | ### Answer 19 | 20 | ```js 21 | var globalArray = [5, 6, 3, 2, 9]; 22 | function nonMutatingSort(arr) { 23 | // Add your code below this line 24 | 25 | return [...arr].sort() 26 | // Add your code above this line 27 | } 28 | nonMutatingSort(globalArray); 29 | ``` 30 | 31 | ### Thinking 32 | 33 | 1. We need a copy of the array before sorting, cause we do not want to mutated it. So by using `[...arr]` we make a copy and now we can sort. 34 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Sort an Array Alphabetically using the sort Method.md: -------------------------------------------------------------------------------- 1 | # Sort an Array Alphabetically using the sort Method 2 | 3 | ### Before 4 | 5 | ```js 6 | function alphabeticalOrder(arr) { 7 | // Add your code below this line 8 | 9 | 10 | // Add your code above this line 11 | } 12 | alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); 13 | ``` 14 | 15 | ### Answer 16 | 17 | ```js 18 | function alphabeticalOrder(arr) { 19 | // Add your code below this line 20 | 21 | return arr.sort() 22 | // Add your code above this line 23 | } 24 | alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); 25 | ``` 26 | 27 | ### Thinking 28 | 29 | NOTHING TO SAY :) 30 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Split a String into an Array Using the split Method.md: -------------------------------------------------------------------------------- 1 | # Split a String into an Array Using the split Method 2 | 3 | Use the split method inside the splitify function to split str into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation. 4 | 5 | ### Before 6 | 7 | ```js 8 | function splitify(str) { 9 | // Add your code below this line 10 | 11 | 12 | // Add your code above this line 13 | } 14 | splitify("Hello World,I-am code"); 15 | ``` 16 | 17 | ### Answer 18 | 19 | ```js 20 | function splitify(str) { 21 | // Add your code below this line 22 | 23 | return str.split(/\W/); 24 | // Add your code above this line 25 | } 26 | splitify("Hello World,I-am code"); 27 | ``` 28 | 29 | ### Thinking 30 | 31 | 1. We want to split on non letters 32 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Functional Programming/Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem.md: -------------------------------------------------------------------------------- 1 | # Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem 2 | 3 | We have defined a function named squareList. You need to complete the code for the squareList function using any combination of map(), filter(), and reduce() so that it returns a new array containing only the square of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array containing only real numbers is [-3, 4.8, 5, 3, -3.2]. 4 | 5 | Note: Your function should not use any kind of for or while loops or the forEach() function. 6 | 7 | 8 | ### Before 9 | 10 | ```js 11 | const squareList = (arr) => { 12 | // only change code below this line 13 | return arr; 14 | // only change code above this line 15 | }; 16 | 17 | // test your code 18 | const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]); 19 | console.log(squaredIntegers); 20 | ``` 21 | 22 | ### After 23 | 24 | ```js 25 | const squareList = (arr) => { 26 | // only change code below this line 27 | return arr.filter(el => el > 0 && el % 1 === 0).map(el => el * el); 28 | // only change code above this line 29 | }; 30 | 31 | // test your code 32 | const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]); 33 | console.log(squaredIntegers); 34 | ``` 35 | 36 | ### Thinking 37 | 38 | 1. First we need to remove the non-positive number so filter element who are smaller than 0 39 | 2. We also need to make sure those number are not decimal. The way to deal with that is checking with `%` and 1 to see if that equal 0. The reminder of a real 40 | integer will always be 0 so the others are decimals number and should be remove. 41 | 3. We map over the rest and square those numbers. 42 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Boo Who.md: -------------------------------------------------------------------------------- 1 | Check if a value is classified as a boolean primitive. Return true or false. 2 | 3 | Boolean primitives are true and false. 4 | 5 | ```js 6 | function booWho(bool) { 7 | // just need to understand what is a boolean primitive 8 | // return with the typeof do the job 9 | return typeof bool === 'boolean'; 10 | } 11 | 12 | booWho(NaN); 13 | ``` 14 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Convert HTML Entities.md: -------------------------------------------------------------------------------- 1 | Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities. 2 | 3 | ```js 4 | function convertHTML(str) { 5 | // make an array wth the string 6 | // much more easy to work with an 7 | // array than a string 8 | var arr = str.split(''); 9 | // loop over the array 10 | for (var i = 0; i < arr.length; i++) { 11 | // use a switch statement for check every index 12 | switch (arr[i]) { 13 | case '&': 14 | arr[i] = '&'; 15 | break; 16 | case '<': 17 | arr[i] = '<'; 18 | break; 19 | case '>': 20 | arr[i] = '>'; 21 | break; 22 | case '"': 23 | arr[i] = '"'; 24 | break; 25 | case "'": 26 | arr[i] = '''; 27 | break; 28 | } 29 | } 30 | // return as a string back 31 | return arr.join(''); 32 | } 33 | 34 | convertHTML("Dolce & Gabbana"); 35 | ``` 36 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Diff Two Arrays.md: -------------------------------------------------------------------------------- 1 | # Diff Two Arrays 2 | 3 | Compare two `arrays` and return a new `array` with any items only found in one of the two given `arrays`, but not both. In other words, return the symmetric difference of the two `arrays`. 4 | 5 | ### Before 6 | 7 | ```js 8 | function diffArray(arr1, arr2) { 9 | var newArr = []; 10 | // Same, same; but different. 11 | return newArr; 12 | } 13 | 14 | diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); 15 | ``` 16 | 17 | ### Answers 18 | 19 | ```js 20 | function diffArray(arr1, arr2) { 21 | var newArr = []; 22 | 23 | newArr = arr1.concat(arr2); 24 | 25 | function checkNum(num) { 26 | if (arr1.indexOf(num) === -1 || arr2.indexOf(num) === -1) { 27 | return num; 28 | } 29 | } 30 | 31 | return newArr.filter(checkNum); 32 | 33 | } 34 | 35 | diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); 36 | ``` 37 | 38 | ### Thinking 39 | 40 | 1. Create an empty array and passed it the value of the both array concat. That's gonna help alot if the value is in the same array. 41 | 2. Just create a function who check in a num is the indexOf and if the answer is no that give us -1. 42 | 3. Pass the action in the `filter()` method. 43 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Missing letters.md: -------------------------------------------------------------------------------- 1 | Find the missing letter in the passed letter range and return it. 2 | 3 | If all letters are present in the range, return undefined. 4 | 5 | Answer 6 | 7 | ```js 8 | 9 | function fearNotLetter(str) { 10 | // Declare variable 11 | var arrToCheck = []; 12 | var notHere; 13 | 14 | // Loop over the str and push the charCode number 15 | for (var i = 0; i < str.length; i++) { 16 | arrToCheck.push(str.charCodeAt(i)); 17 | } 18 | 19 | // loop over the arrToCheck and check which number is missing 20 | for (var x = 1; x < arrToCheck.length; x++) { 21 | if (arrToCheck[x] - arrToCheck[x - 1] !== 1) { 22 | // arrToCheck is the number + 1 so that why we -1 23 | notHere = arrToCheck[x] - 1; 24 | } 25 | } 26 | 27 | // Return undefined if no miss 28 | if (!notHere) { 29 | notHere = undefined; 30 | } else { 31 | // Return the string from the char code 32 | notHere = String.fromCharCode(notHere); 33 | } 34 | 35 | return notHere; 36 | } 37 | 38 | fearNotLetter("abce"); 39 | ``` 40 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Search and Replace.md: -------------------------------------------------------------------------------- 1 | # Search and Replace 2 | 3 | Perform a search and replace on the sentence using the arguments provided and return the new sentence. 4 | 5 | First argument is the sentence to perform the search and replace on. 6 | 7 | Second argument is the word that you will be replacing (before). 8 | 9 | Third argument is what you will be replacing the second argument with (after). 10 | 11 | NOTE: Preserve the case of the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog" 12 | 13 | ## Solutions 14 | 15 | ```js 16 | function myReplace(str, before, after) { 17 | // create a variable who gonna contains the after var if need to be change 18 | var nAfter; 19 | // create a function who return a string with the first letter uppercase 20 | function makeFirstUpperCase(s) { 21 | return s.charAt(0).toUpperCase() + s.slice(1); 22 | } 23 | // check if the first letter of "before" is uppercase 24 | if (before.charAt(0) === before.charAt(0).toUpperCase()) { 25 | // if it's uppercase make the after variable first letter uppercase with the function we create. 26 | nAfter = makeFirstUpperCase(after); 27 | } else { 28 | // if not just return the variable 29 | nAfter = after; 30 | } 31 | // split the str with the word we want to replace and join that with the after variable 32 | return str.split(before).join(nAfter); 33 | } 34 | 35 | myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"); 36 | ``` 37 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Sorted Union.md: -------------------------------------------------------------------------------- 1 | Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays. 2 | 3 | In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array. 4 | 5 | The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order. 6 | 7 | ```js 8 | function uniteUnique(arr) { 9 | // make an array with the arguments object 10 | var args = Array.from(arguments); 11 | // concat each array in args to make one 12 | var arrConcat = args.reduce(function(a, b) { 13 | return a.concat(b); 14 | }, []); 15 | // function for return unique element 16 | var uniqueArr = function(a) { 17 | // reduce the array give 18 | return a.reduce(function(b, c) { 19 | // find if this is unique 20 | if (b.indexOf(c) < 0) { 21 | // if unique push it in the array 22 | b.push(c); 23 | } 24 | // return the array 25 | return b; 26 | }, []); 27 | }; 28 | 29 | return uniqueArr(arrConcat); 30 | } 31 | 32 | uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); 33 | ``` 34 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Sum All Numbers in a Range.md: -------------------------------------------------------------------------------- 1 | # Sum All Numbers in a Range 2 | 3 | We'll pass you an array of two numbers. Return the sum of those two numbers and all numbers between them. 4 | 5 | The lowest number will not always come first. 6 | 7 | ### Before 8 | 9 | ```javascript 10 | function sumAll(arr) { 11 | return 1; 12 | } 13 | 14 | sumAll([1, 4]); 15 | ``` 16 | 17 | ### Answer 18 | 19 | ```javascript 20 | function sumAll(arr) { 21 | let biggerNumber = Math.max(...arr); 22 | let smallerNumber = Math.min(...arr) 23 | 24 | let total = biggerNumber + smallerNumber 25 | 26 | for (let i = smallerNumber + 1; i < biggerNumber; i++) { 27 | total += i 28 | } 29 | 30 | return total 31 | } 32 | 33 | sumAll([1, 4]); 34 | ``` 35 | 36 | ### Thinking 37 | 38 | 1. Get the biggest and the smallest number; 39 | 2. Create a var total who initialize with the biggestNumber sum to the smallest one 40 | 3. Loop between between the range and add this to the total 41 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Intermediate Algorithm Scripting/Wherefore art thou.md: -------------------------------------------------------------------------------- 1 | # Wherefore art thou 2 | 3 | Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array. 4 | 5 | For example, if the first argument is `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`, and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the property and its value, that was passed on as the second argument. 6 | 7 | ## Solution 8 | 9 | ```js 10 | function whatIsInAName(collection, source) { 11 | // What's in a name? 12 | var arr = []; 13 | // Only change code below this line 14 | // make an array of keys 15 | var keysToCheck = Object.keys(source); 16 | // filter the collection 17 | arr = collection.filter(function(item) { 18 | // check if every key in the item exist as property and have the good value 19 | return keysToCheck.every(function(key) { 20 | return item.hasOwnProperty(key) && item[key] === source[key]; 21 | }); 22 | }); 23 | 24 | // Only change code above this line 25 | return arr; 26 | } 27 | 28 | whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }); 29 | ``` 30 | -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Concatenate Arrays with concat.md: -------------------------------------------------------------------------------- 1 | # Concatenate Arrays with concat 2 | 3 | `concat` can be used to merge the contents of two `arrays` into one. 4 | 5 | `concat` takes an `array` as an argument and returns a `new array` with 6 | the elements of this `array` concatenated onto the **end**. 7 | 8 | Here is an example of concat being used to concatenate 9 | `otherArray` onto the end of `oldArray`: 10 | 11 | `newArray = oldArray.concat(otherArray);` 12 | 13 | Use `.concat()` to concatenate `concatMe` onto the end of `oldArray` and 14 | assign it to newArray. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | var oldArray = [1,2,3]; 20 | var newArray = []; 21 | 22 | var concatMe = [4,5,6]; 23 | 24 | // Only change code below this line. 25 | 26 | newArray = oldArray; 27 | ``` 28 | 29 | ### Answers 30 | 31 | ```javascript 32 | var oldArray = [1,2,3]; 33 | var newArray = []; 34 | 35 | var concatMe = [4,5,6]; 36 | 37 | // Only change code below this line. 38 | 39 | newArray = oldArray.concat(concatMe); 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Condense arrays with reduce.md: -------------------------------------------------------------------------------- 1 | # Condense arrays with reduce 2 | 3 | The `array` method `reduce` is used to iterate through an 4 | array and condense it into one value. 5 | 6 | To use reduce you pass in a callback whose 7 | arguments are an accumulator (in this case, `previousVal`) and the 8 | current value (`currentVal`). 9 | 10 | The accumulator is like a total that reduce keeps 11 | track of after each operation. The current value is just 12 | the next element in the array you're iterating through. 13 | 14 | reduce has an optional second argument which can be used to set 15 | the initial value of the accumulator. If no initial value is 16 | specified it will be the first array element and 17 | currentVal will start with the second array element. 18 | 19 | Here is an example of reduce being used to subtract all the values of an array: 20 | 21 | ```javascript 22 | var singleVal = array.reduce(function(previousVal, currentVal) { 23 | return previousVal - currentVal; 24 | }, 0); 25 | ``` 26 | 27 | Use the reduce method to `sum` all the values in array and assign 28 | it to `singleVal`. 29 | 30 | ### Before 31 | 32 | ```javascript 33 | var array = [4,5,6,7,8]; 34 | var singleVal = 0; 35 | 36 | // Only change code below this line. 37 | 38 | singleVal = array; 39 | ``` 40 | 41 | ### Answers 42 | 43 | ```javascript 44 | var array = [4,5,6,7,8]; 45 | var singleVal = 0; 46 | 47 | // Only change code below this line. 48 | 49 | singleVal = array.reduce(function(total, val) { 50 | return total + val; 51 | }); 52 | ``` 53 | 54 | ### Link 55 | [Airpair](https://www.airpair.com/javascript/javascript-array-reduce) -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Construct JavaScript Objects with Functions.md: -------------------------------------------------------------------------------- 1 | # Construct JavaScript Objects with Functions 2 | 3 | We are also able to create objects using `constructor` functions. 4 | 5 | A `constructor` function is given a capitalized name 6 | to make it clear that it is a constructor. 7 | 8 | Here's an example of a constructor function: 9 | 10 | ```javascript 11 | var Car = function() { 12 | this.wheels = 4; 13 | this.engines = 1; 14 | this.seats = 1; 15 | }; 16 | ``` 17 | 18 | In a constructor the this variable refers to the new 19 | object being created by the constructor. So when we write, 20 | 21 | `this.wheels = 4;` 22 | 23 | inside of the `constructor` we are giving the new object it 24 | creates a property called wheels with a value of 4. 25 | 26 | You can think of a `constructor` as a description for the object 27 | it will create. 28 | 29 | Have your MotorBike `constructor` describe an object with wheels, 30 | engines and seats properties and set them to numbers. 31 | 32 | ### Before 33 | 34 | ```javascript 35 | var Car = function() { 36 | this.wheels = 4; 37 | this.engines = 1; 38 | this.seats = 1; 39 | }; 40 | 41 | // Only change code below this line. 42 | 43 | var MotorBike = function() { 44 | 45 | }; 46 | ``` 47 | 48 | ### Answers 49 | 50 | ```javascript 51 | var Car = function() { 52 | this.wheels = 4; 53 | this.engines = 1; 54 | this.seats = 1; 55 | }; 56 | 57 | // Only change code below this line. 58 | 59 | var MotorBike = function() { 60 | this.wheels = 2; 61 | this.engines = 1; 62 | this.seats = 1; 63 | }; 64 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Declare JavaScript Objects as Variables.md: -------------------------------------------------------------------------------- 1 | # Declare JavaScript Objects as Variables 2 | 3 | Before we dive into `Object Oriented Programming`, let's revisit 4 | JavaScript objects. 5 | 6 | Give your `motorBike` object a wheels, engines and seats 7 | attribute and set them to numbers. 8 | 9 | ### Before 10 | 11 | ```javascript 12 | var car = { 13 | "wheels":4, 14 | "engines":1, 15 | "seats":5 16 | }; 17 | 18 | var motorBike = { 19 | 20 | // Only change code below this line. 21 | 22 | }; 23 | ``` 24 | 25 | ### Answers 26 | 27 | ```javascript 28 | var car = { 29 | "wheels":4, 30 | "engines":1, 31 | "seats":5 32 | }; 33 | 34 | var motorBike = { 35 | 36 | "wheels": 2, 37 | "engines": 1, 38 | "seats": 1 39 | 40 | }; 41 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Filter Arrays with filter.md: -------------------------------------------------------------------------------- 1 | # Filter Arrays with filter 2 | 3 | The `filter` method is used to iterate through an array and **filter out 4 | elements where a given condition is not true**. 5 | 6 | `filter` is passed a callback function which takes the current 7 | value (we've called that val) as an argument. 8 | 9 | **Any array element for which the callback returns true 10 | will be kept and elements that return false will be filtered out.** 11 | 12 | The following code is an example of using filter to remove array 13 | elements that are equal to five: 14 | 15 | ## Note 16 | *We omit the second and third arguments since we only need the value* 17 | 18 | ```javascript 19 | array = array.filter(function(val) { 20 | return val !== 5; 21 | }); 22 | ``` 23 | 24 | Use filter to create a new array with all the values 25 | from `oldArray` which are less than 6. The `oldArray` should not change. 26 | 27 | ### Before 28 | 29 | ```javascript 30 | var oldArray = [1,2,3,4,5,6,7,8,9,10]; 31 | 32 | // Only change code below this line. 33 | 34 | var newArray = oldArray; 35 | ``` 36 | 37 | ### Answers 38 | 39 | ```javascript 40 | var oldArray = [1,2,3,4,5,6,7,8,9,10]; 41 | 42 | // Only change code below this line. 43 | 44 | var newArray = oldArray.filter(function(val) { 45 | return val < 6; 46 | }); 47 | ``` 48 | 49 | ### Links 50 | 51 | [EggHead](https://egghead.io/lessons/javascript-the-array-filter-method) -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Iterate over Arrays with map.md: -------------------------------------------------------------------------------- 1 | # Iterate over Arrays with map 2 | 3 | The `map` method is a convenient way to iterate through arrays. 4 | Here's an example usage: 5 | 6 | ```javascript 7 | var timesFour = oldArray.map(function(val){ 8 | return val * 4; 9 | }); 10 | ``` 11 | 12 | The map method will iterate through every element of the array, 13 | creating a new array with values that have been modified by the 14 | callback function, and return it. Note that it does not modify 15 | the original array. 16 | 17 | In our example the callback only uses the value of the array 18 | element (the val argument) but your callback can also include 19 | arguments for the index and array being acted on. 20 | 21 | Use the `map` function to add 3 to every value in the variable `oldArray`, 22 | and save the results into variable newArray. oldArray should not change. 23 | 24 | ### Before 25 | 26 | ```javascript 27 | var oldArray = [1,2,3,4,5]; 28 | 29 | // Only change code below this line. 30 | 31 | var newArray = oldArray; 32 | ``` 33 | 34 | ### Answers 35 | 36 | ```javascript 37 | var oldArray = [1,2,3,4,5]; 38 | 39 | // Only change code below this line. 40 | var addThree = oldArray.map(function(val) { 41 | return val + 3; 42 | }); 43 | 44 | var newArray = addThree; 45 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Join Strings with join.md: -------------------------------------------------------------------------------- 1 | # Join Strings with join 2 | 3 | We can use the `join` method to `join` each element of 4 | an `array` into a `string` separated by whatever 5 | delimiter you provide as an argument. 6 | 7 | The following is an example of using `join` to `join` all of the 8 | elements of an array into a `string` with all the elements 9 | separated by word and: 10 | 11 | ```javascript 12 | var veggies = ["Celery", "Radish", "Carrot", "Potato"]; 13 | var salad = veggies.join(" and "); 14 | console.log(salad); // "Celery and Radish and Carrot and Potato" 15 | ``` 16 | 17 | Use the `join` method to create a `string` from `joinMe` with `spaces` 18 | in between each element and assign it to `joinedString`. 19 | 20 | ### Before 21 | 22 | ```javascript 23 | var joinMe = ["Split","me","into","an","array"]; 24 | var joinedString = ''; 25 | 26 | // Only change code below this line. 27 | 28 | joinedString = joinMe; 29 | ``` 30 | 31 | ### Answers 32 | 33 | ```javascript 34 | var joinMe = ["Split","me","into","an","array"]; 35 | var joinedString = ''; 36 | 37 | // Only change code below this line. 38 | 39 | joinedString = joinMe.join(" "); 40 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Make Instances of Objects with a Constructor Function.md: -------------------------------------------------------------------------------- 1 | # Make Instances of Objects with a Constructor Function 2 | 3 | Now let's put that great `constructor` function we made in 4 | the last lesson to use! 5 | 6 | To use a `constructor` function we call it with the new 7 | keyword in front of it like: 8 | 9 | `var myCar = new Car();` 10 | 11 | `myCar` is now an instance of the `Car` constructor that looks like 12 | the object it described: 13 | 14 | ``` 15 | { 16 | wheels: 4, 17 | engines: 1, 18 | seats: 1 19 | } 20 | ``` 21 | Note that it is important to use the new keyword 22 | when calling a constructor. This is how Javascript knows 23 | to create a new object and that all the references to this 24 | inside the constructor should be referring to this new object. 25 | 26 | Now, once the `myCar` instance is created it can be used like 27 | any other object and can have its properties accessed and 28 | modified the same way you would usually. For example: 29 | 30 | `myCar.turboType = "twin";` 31 | 32 | Our myCar variable now has a property `turboType` with a value of "twin". 33 | 34 | In the editor, use the Car constructor to create a new instance and 35 | assign it to `myCar`. 36 | 37 | Then give myCar a nickname property with a string value. 38 | 39 | ### Before 40 | 41 | ```javascript 42 | var Car = function() { 43 | this.wheels = 4; 44 | this.engines = 1; 45 | this.seats = 1; 46 | }; 47 | 48 | // Only change code below this line. 49 | 50 | var myCar; 51 | ``` 52 | 53 | ### Answers 54 | 55 | ```javascript 56 | var Car = function() { 57 | this.wheels = 4; 58 | this.engines = 1; 59 | this.seats = 1; 60 | }; 61 | 62 | // Only change code below this line. 63 | 64 | var myCar = new Car(); 65 | 66 | myCar.nickname = "Lambo"; 67 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Make Unique Objects by Passing Parameters to our Constructor.md: -------------------------------------------------------------------------------- 1 | # Make Unique Objects by Passing Parameters to our Constructor 2 | 3 | The `constructor` we have is great, but what if we don't 4 | always want to create the same object? 5 | 6 | To solve this we can add parameters to our constructor. 7 | We do this like the following example: 8 | 9 | ```javascript 10 | var Car = function(wheels, seats, engines) { 11 | this.wheels = wheels; 12 | this.seats = seats; 13 | this.engines = engines; 14 | }; 15 | ``` 16 | 17 | Now we can pass in arguments when we call our constructor. 18 | 19 | `var myCar = new Car(6, 3, 1);` 20 | 21 | This code will create an object that uses the arguments we 22 | passed in and looks like: 23 | 24 | ``` 25 | { 26 | wheels: 6, 27 | seats: 3, 28 | engines: 1 29 | } 30 | ``` 31 | 32 | Now give it a try yourself! Alter the Car constructor to use 33 | parameters to assign values to the wheels, seats, and engines properties. 34 | 35 | Then call your new constructor with three number arguments and 36 | assign it to myCar to see it in action. 37 | 38 | ### Before 39 | 40 | ```javascript 41 | var Car = function() { 42 | //Change this constructor 43 | this.wheels = 4; 44 | this.seats = 1; 45 | this.engines = 1; 46 | }; 47 | 48 | //Try it out here 49 | var myCar; 50 | ``` 51 | 52 | ### Answers 53 | 54 | ```javascript 55 | var Car = function(wheels, seats, engines) { 56 | //Change this constructor 57 | this.wheels = wheels; 58 | this.seats = seats; 59 | this.engines = engines; 60 | }; 61 | 62 | //Try it out here 63 | var myCar = new Car(4, 4, 1); 64 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Reverse Arrays with reverse.md: -------------------------------------------------------------------------------- 1 | # Reverse Arrays with reverse 2 | 3 | You can use the `reverse` method to `reverse` the elements of an `array`. 4 | 5 | `reverse` is another `array` method that alters the `array` in place, 6 | but it also returns the reversed array. 7 | 8 | ```javascript 9 | var myArray = [1, 2, 3]; 10 | myArray.reverse(); 11 | returns [3, 2, 1] 12 | ``` 13 | 14 | Use reverse to reverse the array variable and assign it to newArray. 15 | 16 | ### Before 17 | 18 | ```javascript 19 | var array = [1,2,3,4,5,6,7]; 20 | var newArray = []; 21 | 22 | // Only change code below this line. 23 | 24 | newArray = array; 25 | ``` 26 | 27 | ### Answers 28 | 29 | ```javascript 30 | var array = [1,2,3,4,5,6,7]; 31 | var newArray = []; 32 | 33 | // Only change code below this line. 34 | 35 | newArray = array.reverse(); 36 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Sort Arrays with sort.md: -------------------------------------------------------------------------------- 1 | # Sort Arrays with sort 2 | 3 | You can use the method `sort` to easily `sort` the values in an 4 | array alphabetically or numerically. 5 | 6 | Unlike the previous array methods we have been looking at, 7 | `sort` actually alters the array in place. However, it also 8 | returns this sorted array. 9 | 10 | sort can be passed a compare function as a callback. The compare 11 | function should return a negative number if `a` should be before `b`, `a` 12 | positive number if a should be after b, or 0 if they are equal. 13 | 14 | If no compare (callback) function is passed in, it will convert 15 | the values to strings and sort alphabetically. 16 | 17 | Here is an example of using sort with a compare function 18 | that will sort the elements from smallest to largest number: 19 | 20 | ```javascript 21 | var array = [1, 12, 21, 2]; 22 | array.sort(function(a, b) { 23 | return a - b; 24 | }); 25 | ``` 26 | 27 | Use sort to sort array from largest to smallest. 28 | 29 | ### Before 30 | 31 | ```javascript 32 | var array = [1, 12, 21, 2]; 33 | 34 | // Only change code below this line. 35 | 36 | array.sort(); 37 | ``` 38 | 39 | ### Answers 40 | 41 | ```javascript 42 | var array = [1, 12, 21, 2]; 43 | 44 | // Only change code below this line. 45 | 46 | array.sort(function(a, b) { 47 | return b - a; 48 | }); 49 | ``` -------------------------------------------------------------------------------- /javascript/FreeCodeCamps/Object Oriented and Functional Programming/Split Strings with split.md: -------------------------------------------------------------------------------- 1 | # Split Strings with split 2 | 3 | You can use the `split` method to `split` a `string` into an `array`. 4 | 5 | `split` uses the argument you pass in as a delimiter to 6 | determine which points the `string` should be `split` at. 7 | 8 | Here is an example of `split` being used to `split` a `string` at every 9 | `s` character: 10 | 11 | ```javascript 12 | var array = string.split('s'); 13 | ``` 14 | 15 | Use split to create an `array` of words from `string` and assign it to `array`. 16 | 17 | ### Before 18 | 19 | ```javascript 20 | var string = "Split me into an array"; 21 | var array = []; 22 | 23 | // Only change code below this line. 24 | 25 | array = string; 26 | ``` 27 | 28 | ### Answers 29 | 30 | ```javascript 31 | var string = "Split me into an array"; 32 | var array = []; 33 | 34 | // Only change code below this line. 35 | 36 | array = string.split(/\s/g); 37 | ``` 38 | 39 | ### Thinking 40 | 41 | Here I use the regex `\s` for find the space -------------------------------------------------------------------------------- /javascript/Problems/FizzBuzz.md: -------------------------------------------------------------------------------- 1 | # FizzBuzz 2 | 3 | 1 to 100, print Fizz when you can divided by 3. Print Buzz when you can divided by 5 4 | and FizzBuzz when you can do this with both 3 and 5. 5 | 6 | ### Answers 7 | 8 | ```javascript 9 | for (var i = 1; i <= 100; i++) { 10 | if (i % 3 === 0 && i % 5 === 0) { 11 | console.log('FizzBuzz'); 12 | } else if (i % 3 === 0) { 13 | console.log('Fizz'); 14 | } else if (i % 5 === 0) { 15 | console.log('Buzz'); 16 | } else { 17 | console.log(i); 18 | } 19 | } 20 | ``` 21 | 22 | ### Thinking 23 | 24 | 1. Loop to all number 25 | 2. See when divide by number if whe have 0 26 | 3. We need to start whit FizzBuzz if we want it to work -------------------------------------------------------------------------------- /sql/CodeFights/Always Leave Table in ORDER/volleyballResults.md: -------------------------------------------------------------------------------- 1 | You are creating a website that will help you and your friends keep track of the results of volleyball teams from all around the world. Your website regularly crawls the web searching for new games, and adds the results of these games to the results table stored in your local database. After each update, the table should be sorted in ascending order by the total number of games won. This year's results are quite marvelous - at any given moment there are no two teams that have won the same number of games! 2 | 3 | The results table contains the following columns: 4 | 5 | name - the unique name of the team; 6 | country - the country of the team; 7 | scored - the number of scored goals; 8 | missed - the number of missed goals; 9 | wins - the total number of games the team has won. 10 | Your task is to sort the given results table in ascending order by the number of wins. 11 | 12 | ``` sql 13 | CREATE PROCEDURE volleyballResults() 14 | BEGIN 15 | SELECT * FROM results ORDER BY wins; 16 | END 17 | ``` 18 | -------------------------------------------------------------------------------- /sql/CodeFights/Welcome to the Table/automaticNotifications.md: -------------------------------------------------------------------------------- 1 | The application you've been working on for the past year is a huge success! It already has a large and active user community. You know the ID number, username, and email of each user. Each user also has a specific role that shows their position in the community. Information about the users is stored in the database as a table users, which has the following structure: 2 | 3 | id: the unique user ID; 4 | username: the username of the user; 5 | role the user's role; 6 | email: the user's email. 7 | You want to send users automatic notifications to let them know about the most recent updates. However, not all users should get these notifications: Administrators don't need notifications since they know about the updates already, and premium users don't need them since they get personalized weekly updates. 8 | 9 | Given the users table, your task is to return the emails of all the users who should get notifications, i.e. those whose role is not equal to "admin" or "premium". Note that roles are case insensitive, so users with roles of "Admin", "pReMiUm", etc. should also be excluded. 10 | 11 | The resulting table should contain a single email column and be sorted by emails in ascending order. 12 | 13 | ``` sql 14 | CREATE PROCEDURE automaticNotifications() 15 | SELECT email 16 | FROM users 17 | WHERE role NOT IN ("admin", "premium") 18 | 19 | ORDER BY email; 20 | ``` 21 | 22 | 1. We dont want the role to be admin or premium. NOT IN gonna make sure then this is nothing inside the () 23 | -------------------------------------------------------------------------------- /sql/CodeFights/Welcome to the Table/countriesSelection.md: -------------------------------------------------------------------------------- 1 | Your friend wants to become a professional tour guide and travel all around the world. In pursuit of this dream, she enrolled in tour guide school. The professors in this school turned out to be very demanding, and one of them gave your friend a difficult assignment that she has to finish over the weekend. 2 | 3 | Here's the assignment: Given a list of countries, your friend should identify all the countries that are in Africa. To help her, you have decided to write a function that will find all such countries from any set of countries. The countries table in which the countries are stored has the following structure: 4 | 5 | name: the name of the country; 6 | continent: the continent on which the country is situated; 7 | population: the country's population. 8 | Your task is to return a new table that has the same columns, but that only contains the countries from Africa. The countries should be sorted alphabetically by their names. 9 | 10 | ```sql 11 | CREATE PROCEDURE countriesSelection() 12 | BEGIN 13 | SELECT * FROM countries WHERE continent = 'Africa'; 14 | END 15 | ``` 16 | 17 | 1. Select every element but make sure from the continent Africa 18 | -------------------------------------------------------------------------------- /sql/CodeFights/Welcome to the Table/monthlyScholarships.md: -------------------------------------------------------------------------------- 1 | Students at your university get scholarships that are paid out throughout the year. 2 | 3 | Information about the scholarships is stored in the table scholarships, which has the structure: 4 | 5 | id: the unique student id; 6 | scholarship: the amount of the annual scholarship the student has been awarded. 7 | Now you need to calculate the amount of money each student should get per month. Given the table scholarships, build the resulting table as follows: The table should have the same columns as the initial table, but the scholarship column should contain the amount of the student's monthly scholarship payout. The rows should be ordered by the students' ids. 8 | 9 | ``` sql 10 | CREATE PROCEDURE monthlyScholarships() 11 | BEGIN 12 | SELECT id, (scholarship / 12) AS scholarship FROM scholarships; 13 | END 14 | ``` 15 | 16 | 1. We need to divide the scholarship by 12 to get the montly paiment and we want the return value to be call scholarship 17 | -------------------------------------------------------------------------------- /sql/CodeFights/Welcome to the Table/projectList.md: -------------------------------------------------------------------------------- 1 | Your boss wants to identify the successful projects running in your company, so he asked you to prepare a list of all the currently active projects and their average monthly income. 2 | 3 | You have stored the information about these projects in a simple database with a single Projects table that has five columns: 4 | 5 | internal_id: the company's internal identifier for the project; 6 | project_name: the official name of the project; 7 | team_size: the number of employees working on the project; 8 | team_lead: the name of the project manager; 9 | income: the average monthly income of the project. 10 | Your boss says that internal project ids are irrelevant to him and that he isn't interested in how big the teams are. Since that's the case, he wants you to create another table by removing the internal_id and team_size columns from the existing Projects table. Return it sorted by internal_id in ascending order. 11 | 12 | ```sql 13 | CREATE PROCEDURE projectList() 14 | BEGIN 15 | select project_name, team_lead, income from Projects; 16 | END 17 | ``` 18 | 19 | 1. Just need to select value need from the tables Projects 20 | -------------------------------------------------------------------------------- /sql/CodeFights/Welcome to the Table/projectsTeam.md: -------------------------------------------------------------------------------- 1 | You've been promoted and assigned to a new project. The problem is, you don't know who you are working with and your predecessor has vanished without a trace! Luckily, each project in your company keeps its own activity database, which you are going to use to find out the names of your new colleagues. 2 | 3 | Information about the project's activity is stored in table projectLog, which has the following structure: 4 | 5 | id: unique action id; 6 | name: the name of the person who performed the action; 7 | description: the description of the action; 8 | timestamp: the timestamp of the action. 9 | You only have access to the project's most recent history, but this should be enough for you. You've decided that finding everyone who interacted with the project in this period is the best way to start. 10 | 11 | Given the table projectLog, build a new results table with a single name column that contains the names of the project's contributors sorted in ascending order. 12 | 13 | ``` sql 14 | BEGIN 15 | SELECT DISTINCT name FROM projectLog ORDER BY name ASC; 16 | END 17 | ``` 18 | 19 | 1. Make use of DISTINCT to remove duplicate value 20 | 2. Order by the name in Ascending order 21 | -------------------------------------------------------------------------------- /swift/shuffledArray.swift: -------------------------------------------------------------------------------- 1 | let numbers = [1, 3, 5, 7, 9] 2 | let names = ["Bob", "Jon", "Alice"] 3 | 4 | func shuffledArray(arr: [T]) -> [T] { 5 | var mutateArray = arr; 6 | var shuffledArraySoFar = [T]() 7 | 8 | while mutateArray.count > 0 { 9 | let removeIndex = Int.random(in: 0..