Příspěvky

FreeCodeCamp.com - Basic JavaSript - Iterate with JavaScript For Loops

Link to FreeCodeCamp You can run the same code multiple times by using a loop. The most common type of JavaScript loop is called a " for loop " because it runs "for" a specific number of times. For loops are declared with three optional expressions separated by semicolons: for ([initialization]; [condition]; [final-expression]) The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable. The condition statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to true . When condition is false at the start of the iteration, the loop will stop executing. This means if condition starts as false , your loop will never execute. The final-expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter. In the following example we i...

FreeCodeCamp.com - Basic JavaSript - Accessing Nested Arrays

Link to FreeCodeCamp As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays. Here is an example of how to access a nested array: var ourPets = [ { animalType: "cat", names: [ "Meowzer", "Fluffy", "Kit-Cat" ] }, { animalType: "dog", names: [ "Spot", "Bowser", "Frankie" ] } ]; ourPets[0].names[1]; // "Fluffy" ourPets[1].names[0]; // "Spot" Instructions Retrieve the second tree from the variable myPlants using object dot and array bracket notation. Sollution // Setup var myPlants = [   {      type: "flowers",     list: [       "rose",       "tulip",       "dandelion"     ]   },   {     type: "trees...

FreeCodeCamp.com - Basic JavaSript - Accessing Objects Properties with Variables

Link to FreeCodeCamp A big thank you goes for  amykotas    fro giving this advice in the feed. Another use of bracket notation on objects is to use a variable to access a property. This can be very useful for iterating through lists of the object properties or for doing the lookup. Here is an example of using a variable to access a property: var someProp = "propName"; var myObj = { propName: "Some Value" } myObj[someProp]; // "Some Value" Here is one more: var myDog = "Hunter"; var dogs = { Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle" } var breed = dogs[myDog]; console.log(breed);// "Doberman" Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name Instructions Use the playerNumber variable to lookup player 16 in testObj using bracket notation. Sollution // Set...

FreeCodeCamp.com - Basic JavaSript - Using Objects for Lookups

Link to FreeCodeCamp A big thank you goes to  SaintPeter , for giving this advice . Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to "lookup" values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range. Here is an example of a simple reverse alphabet lookup: var alpha = { 1:"Z", 2:"Y", 3:"X", 4:"W", ... 24:"C", 25:"B", 26:"A" }; alpha[2]; // "Y" alpha[24]; // "C" var value = 2; alpha[value]; // "Y" Instructions Convert the switch statement into a lookup table called lookup . Use it to lookup val and assign the associated string to the result variable. Sollution // Setup function phoneticLookup(val) {   var result = "";   // Only change code below this line var lo...

FreeCodeCamp.com - Basic JavaSript - Understand String Immutability

Link to FreeCodeCamp In JavaScript, String values are immutable , which means that they cannot be altered once created. For example, the following code: var myStr = "Bob"; myStr[0] = "J"; cannot change the value of myStr to "Job", because the contents of myStr cannot be altered. Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed. The only way to change myStr would be to assign it with a new string, like this: var myStr = "Bob"; myStr = "Job"; Instructions Correct the assignment to myStr to achieve the desired effect. Sollution // Setup var myStr = "Jello World"; // Only change code below this line myStr = "Hello World";

FreeCodeCamp.com - Basic JavaSript - Word Blanks

Link to FreeCodeCamp We will now use our knowledge of strings to build a " Mad Libs " style word game we're calling "Word Blanks". You will create an (optionally humorous) "Fill in the Blanks" style sentence. You will need to use string operators to build a new string, result , using the provided variables: myNoun , myAdjective , myVerb , and myAdverb . You will also need to use additional strings, which will not change, and must be in between all of the provided words. The output should be a complete sentence. We have provided a framework for testing your results with different words. The tests will run your function with several different inputs to make sure all of the provided words appear in the output, as well as your extra strings. Sollution function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {   var result = "";   // Your code below this line   result+= "My "+myAdjective+" "+myNoun+...

FreeCodeCamp.com - Basic JavaSript - Golf Code

Link to FreeCodeCamp In the game of golf each hole has a par meaning the average number of strokes a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname. Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest): Strokes Return 1 "Hole-in-one!" <= par - 2 "Eagle" par - 1 "Birdie" par "Par" par + 1 "Bogey" par + 2 "Double Bogey" >= par + 3 "Go Home!" par and strokes will always be numeric and positive. Sollution function golfScore(par, strokes) {   // Only change code below this line   if (strokes === 1){     return "Hole-in-one!";   } else if (strokes <= par - 2){     return "Eagle";   } else i...