Completes C Articles renaming up to 'Challenge-P'

Rafael J. Rodriguez
2016-05-14 12:10:12 -04:00
parent 05188f949e
commit a43d21e946
146 changed files with 1187 additions and 922 deletions
@@ -1,8 +1,10 @@
HTML elements are essentially little rectangles. Three important attributes control the space that surrounds each HTML element: `padding`, `margin`, and `border`. An element's padding controls the amount of space between the element and its border.
```css
.green-box {
background-color: green;
padding: 20px;
}
```
# Challenge Adjust the Padding of an Element
HTML elements are essentially little rectangles. Three important attributes control the space that surrounds each HTML element: `padding`, `margin`, and `border`. An element's padding controls the amount of space between the element and its border.
```css
.green-box {
background-color: green;
padding: 20px;
}
```
@@ -1,9 +1,10 @@
# Appending Variables to Strings
# Challenge Appending Variables to Strings
Just as we can build a string over multiple lines out of string literals, we can also append variables to a string using the plus equals `+=` operator.
## Example
```js
```javascript
var anAdjective = "awesome!";
var ourStr = "Free Code Camp is ";
ourStr += anAdjective;
@@ -1,23 +1,25 @@
# Apply the Default Bootstrap Button Style
Bootstrap has a button class called `btn-default`
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
</div>
</div>
# Challenge Apply the Default Bootstrap Button Style
Bootstrap has a button class called `btn-default`
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
<div class="col-xs-6">
<div class="well">
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
<button class="btn btn-default"></button>
</div>
</div>
</div>
</div>
```
+3 -3
@@ -1,11 +1,11 @@
#Challenge: Assignment with a Returned Value
# Challenge Assignment with a Returned Value
If you'll recall from our discussion of Storing Values with the Equal Operator, everything to the right of the equal sign is resolved before the value is assigned. This means we can take the return value of a function and assign it to a variable.
Assume we have pre-defined a function `sum` which adds two numbers together, then:
```js
```javascript
var ourSum = sum(5, 12);
```
will call `sum` function, which returns a value of `17` and assigns it to `ourSum` variable.
will call `sum` function, which returns a value of `17` and assigns it to `ourSum` variable.
@@ -1,4 +1,5 @@
# Assignment with Divided by Equals
# Challenge Assignment with Divided by Equals
The `/= operator` divides a variable by another number.
```javascript
@@ -1,4 +1,5 @@
# Assignment with Minus Equals
# Challenge Assignment with Minus Equals
Like the `+= operator`, `-=` subtracts a number from a variable.
```javascript
@@ -1,11 +1,12 @@
# Assignment with Plus Equals
# Challenge Assignment with Plus Equals
Everything to the right of the equals sign is evaluated first, so we can say `myVar = myVar + 5;` 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.
One such operator is the `+= operator`.
`myVar += 5;` will add 5 to myVar instead of using any of the follwoing:
```js
```javascript
myVar = 5 + myVar;
myVar = myvar + 5;
```
@@ -1,4 +1,5 @@
# Assignment with Times Equals
# Challenge Assignment with Times Equals
The `*= operator` multiplies a variable by a number.
```javascript
@@ -1,10 +1,11 @@
# Bring your JavaScript Slot Machine to Life
Let's use the jQuery selector `$(".slot")` to select all of the slots.
Once they are all selected, we can use bracket notation to access each individual slot:
```javascript
$($(".slot")[0]).html(slotOne);
$($(".slot")[1]).html(slotTwo);
$($(".slot")[2]).html(slotThree);
```
# Challenge Bring your JavaScript Slot Machine to Life
Let's use the jQuery selector `$(".slot")` to select all of the slots.
Once they are all selected, we can use bracket notation to access each individual slot:
```javascript
$($(".slot")[0]).html(slotOne);
$($(".slot")[1]).html(slotTwo);
$($(".slot")[2]).html(slotThree);
```
+16 -15
@@ -1,15 +1,16 @@
# Build JavaScript Objects
Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.
Here's a sample object:
```javascript
var cat = {
"name": "Whiskers",
"legs": 4,
"tails": 1,
"enemies": ["Water", "Dogs"]
};
```
Objects are useful for storing data in a structured way, and can represents real world objects, like a cats.
# Challenge Build JavaScript Objects
Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.
Here's a sample object:
```javascript
var cat = {
"name": "Whiskers",
"legs": 4,
"tails": 1,
"enemies": ["Water", "Dogs"]
};
```
Objects are useful for storing data in a structured way, and can represents real world objects, like a cats.
@@ -1,6 +1,7 @@
# Call out Optional Actions with Button Info
Bootstrap comes with several pre-defined colors for buttons. The `btn-info` class is used to call attention to optional actions that the user can take. Note that these buttons still need the `btn` and`btn-block` classes.
```html
<button class= "btn btn-block btn-info">Info</button>
```
# Challenge Call out Optional Actions with Button Info
Bootstrap comes with several pre-defined colors for buttons. The `btn-info` class is used to call attention to optional actions that the user can take. Note that these buttons still need the `btn` and`btn-block` classes.
```html
<button class= "btn btn-block btn-info">Info</button>
```
@@ -1,4 +1,9 @@
# Center Text with Bootstrap
Now that we're using Bootstrap, we can center our heading elements to make them look better. All we need to do is add the class text-center to our h1 and h2 elements.
Remember that you can add several classes to the same element by separating each of them with a space, like this: `<h2 class="text-red text-center">your text</h2>`.
# Challenge Center Text with Bootstrap
Now that we're using Bootstrap, we can center our heading elements to make them look better. All we need to do is add the class text-center to our h1 and h2 elements.
Remember that you can add several classes to the same element by separating each of them with a space, like this:
```html
<h2 class="text-red text-center">your text</h2>
```
+2 -2
@@ -1,8 +1,8 @@
#Challenge: Chaining If Else Statements
# Challenge Chaining If Else Statements
`if/else` statements can be chained together for complex logic. Here is `pseudocode` of multiple chained `if` / `else if` statements:
```js
```javascript
if(condition1) {
statement1
} else if (condition2) {
@@ -1,4 +1,5 @@
# Change Text Inside an Element Using jQuery
Using jQuery, you can change the text between the start and end tags of an element. You can even change HTML markup.
jQuery has a function called `.html()` that lets you add HTML tags and text within an element. Any content previously within the element will be completely replaced with the content you provide using this function.
@@ -1,4 +1,9 @@
# Change Text with Click Events
When a click event happens, we can use Ajax to update an HTML element with the following code `$(".message").html("Here is the message");`
When a click event happens, we can use Ajax to update an HTML element with the following code:
```javascript
$(".message").html("Here is the message");
```
This tells jQuery to use the class `message` as the selector and to that, change the HTML to have the string provide, we can also add custom html elements wrapped in quotation marks.
@@ -1,13 +1,14 @@
# Change the CSS of an Element Using jQuery
We can also change the CSS of an HTML element directly with jQuery.
Query has a function called `.css()` that allows you to change the CSS of an element.
```html
<script>
$(document).ready(function() {
$("#target1").css("color", "red");
});
</script>
```
# Change the CSS of an Element Using jQuery
We can also change the CSS of an HTML element directly with jQuery.
Query has a function called `.css()` that allows you to change the CSS of an element.
```html
<script>
$(document).ready(function() {
$("#target1").css("color", "red");
});
</script>
```
+9
@@ -0,0 +1,9 @@
# Challenge Change the color of a Text
CSS allows us to change many styles. To change the color of an element we use `color`.
Here's how you would set your h2 element's text color to blue:
```html
<h2 style="color: blue">CatPhotoApp</h2>
```
@@ -1,5 +1,7 @@
# Challenge Change the Font Size of an Element
Font size is controlled by the `font-size` CSS attribute, like this: `h1 { font-size: 30px; }`.
px is a unit that stands for [pixels](https://en.wikipedia.org/wiki/Pixel)
Although this Challenge requires the use of px, there are [other units of size measurements used](http://www.w3.org/Style/Examples/007/units.en.html)
Although this Challenge requires the use of px, there are [other units of size measurements used](http://www.w3.org/Style/Examples/007/units.en.html)
-3
@@ -1,3 +0,0 @@
CSS allows us to change many styles. To change the color of an element we use `color`.
Here's how you would set your h2 element's text color to blue: `<h2 style="color: blue">CatPhotoApp</h2>`.
@@ -1,3 +1,9 @@
You can set a checkbox or radio button to be checked by default using the checked attribute.
To do this, just add the word "checked" to the inside of an input element. For example, `<input type="radio" name="test-name" checked>`.
# Challenge Check Radio Buttons and Checkboxes by Default
You can set a checkbox or radio button to be checked by default using the checked attribute.
To do this, just add the word "checked" to the inside of an input element. For example,
```html
<input type="radio" name="test-name" checked>
```
@@ -1,4 +1,7 @@
# Check the Length Property of a String Variable
Data structures have properties. For example, strings have a property called `.length` that will tell you how many characters are in the string.
`lastNameLength = lastName.length;`
# Check the Length Property of a String Variable
Data structures have properties. For example, strings have a property called `.length` that will tell you how many characters are in the string.
```javascript
lastNameLength = lastName.length;
```
@@ -1,7 +1,9 @@
# Clone an Element Using jQuery
jQuery has a function called`clone()` that makes a copy of an element.
For example, if we wanted to copy target2 from our left-well to our right-well, we would use
```javascript
$("#target2").clone().appendTo("#right-well");
```
# Challenge Clone an Element Using jQuery
jQuery has a function called`clone()` that makes a copy of an element.
For example, if we wanted to copy target2 from our left-well to our right-well, we would use
```javascript
$("#target2").clone().appendTo("#right-well");
```
@@ -1,15 +1,17 @@
Commenting is a way that you can leave comments within your code without affecting the code itself.
It is also a convenient way to make code inactive without having to delete it entirely.
You can start a comment with `<!-- and end a comment with -->`.
```html
<!--
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
<p>Hello Paragraph</p>
-->
```
# Challenge Comment out HTML
Commenting is a way that you can leave comments within your code without affecting the code itself.
It is also a convenient way to make code inactive without having to delete it entirely.
You can start a comment with `<!-- and end a comment with -->`.
```html
<!--
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
<p>Hello Paragraph</p>
-->
```
@@ -1,10 +1,13 @@
# Comment your JavaScript Code
Comments are a great way to leave notes to yourself and to other people who will later need to figure out what it does. Any code in it will be ignored.
Let's take a look at the two ways you can write comments in JavaScript.
- The double-slash comment will comment out the remainder of the text on the current line:
`// This is a comment.`
- The slash-star-star-slash comment will comment out everything between the `/*` and the `*/` characters:
`/* This is also a comment */`
# Challenge Comment your JavaScript Code
Comments are a great way to leave notes to yourself and to other people who will later need to figure out what it does. Any code in it will be ignored.
Let's take a look at the two ways you can write comments in JavaScript.
- The double-slash comment will comment out the remainder of the text on the current line:
`// This is a comment.`
- The slash-star-star-slash comment will comment out everything between the `/*` and the `*/` characters:
`/* This is also a comment */`
+4 -4
@@ -1,10 +1,10 @@
##Challenge: Comparison with the Equality Operator
# Challenge Comparison with the Equality Operator
There are many `Comparison Operators` in JavaScript. All of these operators return a boolean `true` or `false` value.
The most basic operator is the equality operator `==`. The equality operator compares two values and returns `true` if they're equivalent or `false` if they are not. Note that equality is different from assignment (=), which assigns the value at the right of the operator to a variable in the left.
```js
```javascript
function testEqual(myVal) {
if (myVal == 10) {
return "Equal";
@@ -17,7 +17,7 @@ If `myVal` is equal to `10`, the function will return "Equal". If it is not, the
The equality operator will do its best to convert values for comparison, for example:
```js
```javascript
1 == 1 // true
"1" == 1 // true
1 == '1' // true
@@ -25,4 +25,4 @@ The equality operator will do its best to convert values for comparison, for exa
0 == null // false
0 == undefined // false
null == undefined // true
```
```
@@ -1,12 +1,12 @@
#Challenge: Comparison with the Greater Than Equal To Operator
# Challenge Comparison with the Greater Than Equal To Operator
The greater than equal to operator (`>=`) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns `true`. Otherwise, it returns `false`.
Like the equality operator, greater than equal to operator will convert data types while comparing.
##Examples
## Examples
```js
```javascript
6 >= 6 // true
7 >= '3' // true
2 >= 3 // false
@@ -1,12 +1,12 @@
#Challenge: Comparison with the Greater Than Operator
# Challenge Comparison with the Greater Than Operator
The greater than operator (`>`) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns `true`. Otherwise, it returns `false`.
Like the equality operator, greater than operator will convert data types of values while comparing.
##Examples
## Examples
```js
```javascript
5 > 3 // true
7 > '3' // true
2 > 3 // false
+4 -4
@@ -1,13 +1,13 @@
#Challenge: Comparison with the Inequality Operator
# Challenge Comparison with the Inequality Operator
The inequality operator (`!=`) is the opposite of the equality operator. It means "Not Equal" and returns `false` where equality would return `true` and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing.
##Examples
## Examples
```js
```javascript
1 != 2 // true
1 != "1" // false
1 != '1' // false
1 != true // false
0 != false // false
```
```
@@ -1,13 +1,13 @@
#Challenge: Comparison with the Less Than Equal To Operator
# Challenge Comparison with the Less Than Equal To Operator
The less than equal to operator (`<=`) compares the values of two numbers. If the number to the left is less than or equal the number to the right, it returns `true`. If the number on the left is greater than the number on the right, it returns `false`. Like the equality operator, `less than equal to` converts data types.
##Examples
## Examples
```js
```javascript
4 <= 5 // true
'7' <= 7 // true
5 <= 5 // true
3 <= 2 // false
'8' <= 4 // false
```
```
+3 -3
@@ -1,10 +1,10 @@
#Challenge: Comparison with the Less Than Operator
# Challenge Comparison with the Less Than Operator
The less than operator (`<`) compares the values of two numbers. If the number to the left is less than the number to the right, it returns `true`. Otherwise, it returns `false`. Like the equality operator, less than operator converts data types while comparing.
##Examples
## Examples
```js
```javascript
2 < 5 // true
'3' < 7 // true
5 < 5 // false
@@ -1,12 +1,12 @@
#Challenge: Comparison with the Strict Equality Operator
# Challenge Comparison with the Strict Equality Operator
Strict equality (`===`) is the counterpart to the equality operator (`==`). Unlike the equality operator, strict equality tests both the `type` and `value` of the compared elements.
##Examples
## Examples
```js
```javascript
3 === 3 // true
3 === '3' // false
```
*In the second example, `3` is a `Number` type and `'3'` is a `String` type.*
_In the second example, `3` is a `Number` type and `'3'` is a `String` type._
@@ -1,10 +1,10 @@
#Challenge: Comparison with the Strict Inequality Operator
# Challenge Comparison with the Strict Inequality Operator
The strict inequality operator (`!==`) is the opposite of the strict equality operator. It means "Strictly Not Equal" and returns `false` where strict equality would return `true` and vice versa. Strict inequality will not convert data types.
##Examples
## Examples
```js
```javascript
3 !== 3 // false
3 !== '3' // true
4 !== 3 // true
@@ -1,10 +1,10 @@
#Challenge: Comparisons with the Logical And Operator
# Challenge Comparisons with the Logical And Operator
Sometimes you will need to test more than one thing at a time. The `logical and` operator (`&&`) returns `true` if and only if the `operands` to the left and right of it are true.
The same effect could be achieved by nesting an if statement inside another if:
```js
```javascript
if (num > 5) {
if (num < 10) {
return "Yes";
@@ -15,9 +15,9 @@ return "No";
will only return "Yes" if `num` is between `6` and `9` (6 and 9 included). The same logic can be written as:
```js
```javascript
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
```
```
@@ -1,10 +1,10 @@
#Challenge: Comparisons with the Logical Or Operator
# Challenge Comparisons with the Logical Or Operator
The `logical or` operator (`||`) returns `true` if either of the `operands` is `true`. Otherwise, it returns `false`.
The pattern below should look familiar from prior Challenges:
```js
```javascript
if (num > 10) {
return "No";
}
@@ -16,9 +16,9 @@ return "Yes";
will return "Yes" only if `num` is between `5` and `10` (5 and 10 included). The same logic can be written as:
```js
```javascript
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
```
```
+7
@@ -0,0 +1,7 @@
# Challenge Concatenate Strings with `.concat`
`.concat()` can be used to merge the contents of two arrays into one.
```javascript
array = array.concat(otherArray);
```
-3
@@ -1,3 +0,0 @@
`.concat()` can be used to merge the contents of two arrays into one.
`array = array.concat(otherArray);`
@@ -1,8 +1,13 @@
# Concatenating Strings with Plus Operator
# Challenge Concatenating Strings with Plus Operator
In JavaScript, when the `+` operator is used with a String value, it is called the `concatenation` operator. You can build a new string out of other strings by concatenating them together.
## Example
`'My name is Alan,' + ' I concatenate.'`
```javascript
'My name is Alan,' + ' I concatenate.'
```
## Note
Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
@@ -1,12 +1,14 @@
# Concatenating Strings with the Plus Equals Operator
# Challenge Concatenating Strings with the Plus Equals Operator
We can use the `+=` operator to concatenate a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
## Note
Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
## Example
```js
```javascript
var ourStr = "I come first. ";
ourStr += "I come second.";
```
@@ -1,13 +1,15 @@
Reduce can be useful for condensing an array of numbers into one value.
```javascript
var array = [4,5,6,7,8];
var singleVal = 0;
// Only change code below this line.
var singleVal = array.reduce(function(previousVal, currentVal){
return previousVal+currentVal;
});
```
# Challenge Condense Arrays with Reduce
Reduce can be useful for condensing an array of numbers into one value.
```javascript
var array = [4,5,6,7,8];
var singleVal = 0;
// Only change code below this line.
var singleVal = array.reduce(function(previousVal, currentVal){
return previousVal+currentVal;
});
```
@@ -1,13 +1,14 @@
## Construct JavaScript Objects with Functions
Using constructors it is easy to create new objects using a blueprint or constructor. The declaration syntax is a little different but still easy to remember.
```javascript
// Let's add the properties engines and seats to the car in the same way that the property wheels has been added below. They should both be numbers.
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};
var myCar = new Car();
```
# Challenge Construct JavaScript Objects with Functions
Using constructors it is easy to create new objects using a blueprint or constructor. The declaration syntax is a little different but still easy to remember.
```javascript
// Let's add the properties engines and seats to the car in the same way that the property wheels has been added below. They should both be numbers.
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 1;
};
var myCar = new Car();
```
@@ -1,9 +1,10 @@
# Constructing Strings with Variables
# Challenge Constructing Strings with Variables
Sometimes you will need to build a string, [Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs) style. By using the concatenation operator `+`, you can insert one or more variables into a string you're building.
## Example
```js
```javascript
var ourName = "Free Code Camp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
```
+14 -6
@@ -1,31 +1,37 @@
# Details
# Challenge Convert Celsius to Fahrenheit
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
You are given a variable `celsius` representing a temperature in Celsius. Create a variable `fahrenheit` and apply the algorithm to assign it the corresponding temperature in Fahrenheit.
Remember to use [ Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
Remember to use [Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
## Useful Links
- [The Order of Operations: PEMDAS](http://www.purplemath.com/modules/orderops.htm)
- [Order of Operation: Video](https://www.khanacademy.org/math/pre-algebra/order-of-operations/order_of_operations/v/order-of-operations)
## Problem Explanation:
- Explain what is asked in an easy to understand way.
## Hint: 1
- Take a look at the code, there is an area that you're not supposed to edit, from there ask yourself, what is used there that I don't see before?
## Hint: 2
- Keep in mind the `order of operation` check the link in the _link_ section for more information.
## Spoiler Alert!
[![687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/thumb/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)
**Solution ahead!**
## Code Solution:
```js
```javascript
function convertToF(celsius) {
// Only change code below this line
var fahrenheit = (celsius * (9/5)) + 32;
@@ -42,11 +48,13 @@ function convertToF(celsius) {
convertToF(30);
```
# Code Explanation:
### Code Explanation:
- Make sure the proper order is followed with arithmetic using `()` when needed.
- Declare the `fahrenheit` variable.
# Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @Rafase282 for your help with Checkpoint: Convert Celsius to Fahrenheit`**
### Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @Rafase282 for your help with Checkpoint: Convert Celsius to Fahrenheit`**
> **NOTE:** Please add your username only if you have added any **relevant main contents** to the wiki page. (Please don't remove any existing usernames.)
@@ -1,9 +1,10 @@
# Convert JSON Data to HTML
# Challenge Convert JSON Data to HTML
Once you know how to get data from the JSON call then is time to learn how to iterate through it. We can use the .map() method to loop through our data and modify our HTML elements.
Here is a code that does that:
```js
```javascript
// calling map on the json variable and using a custom callback function.
json.map(function(val) {
@@ -1,11 +1,12 @@
# Count Backwards With a For Loop
# Challenge Count Backwards With a For Loop
A for loop can also count backwards, so long as we can define the right conditions.
In order to count backwards by twos, we'll need to change our `initialization`, `condition`, and `final-expression`.
We'll start at `i = 10` and loop while `i > 0`. We'll decrement `i` by `2` each loop with `i -= 2`.
```js
```javascript
var ourArray = [];
for(var i = 10; i > 0; i -= 2) {
+24 -16
@@ -1,18 +1,18 @@
# Checkpoint: Counting Cards
# Challenge Counting Cards
***About Blackjack***
**_About Blackjack_**
In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called [Card Counting](https://en.wikipedia.org/wiki/Card_counting "Wikipedia entry on Card Counting").
Having more high cards remaining in the deck favors the player. Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low.
| Value | Cards |
| ------ |:----------------------:|
| +1 | 2, 3, 4, 5, 6 |
| 0 | 7, 8, 9 |
| -1 | 10, 'J', 'Q', 'K','A' |
Value | Cards
----- | :-------------------:
+1 | 2, 3, 4, 5, 6
0 | 7, 8, 9
-1 | 10, 'J', 'Q', 'K','A'
***Instructions***
**_Instructions_**
You will write a card counting function. It will receive a **card** parameter and increment or decrement the global **count** variable according to the card's value (see table). The function will then return the current count and the string **"Bet"** if the count is positive, or **"Hold"** if the count is zero or negative.
@@ -21,14 +21,16 @@ You will write a card counting function. It will receive a **card** parameter an
- -3 Hold
- 5 Bet
Remember to use [ Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
Remember to use [Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
## Useful Links
- [Challenge: Selecting from many options with Switch Statements](http://www.freecodecamp.com/challenges/selecting-from-many-options-with-switch-statements)
- [Challenge: Chaining If Else Statements](http://www.freecodecamp.com/challenges/chaining-if-else-statements)
- [Challenge: Increment a Number with Javascript](http://www.freecodecamp.com/challenges/increment-a-number-with-javascript)
## Problem Explanation:
- Change the code below `// Only change code below this line` and up to `// Only change code above this line`
- Take note that you are editing the inside of the `cc` function
- Use what you've learned to check the value of each `card` parameter passed into the function
@@ -37,15 +39,19 @@ Remember to use [ Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try
- If the final count is 0 or less, return **# Bet**
## Hint: 1
- Use a `switch` (or `else if`) statement to count the value of each card.
## Hint: 2
- Add/subtract the value of each card to variable `count`. If the card is worth 0, don't do anything!
## Hint: 3
- After you've counted the cards, use an `if` statement to check the value of `count`. Also, make sure your return has a space between the number and the string.
## Spoiler Alert!
[![687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/thumb/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)
**Solution ahead!**
@@ -78,18 +84,20 @@ function cc(card) {
}
// Only change code above this line
}
```
# Code Explanation:
### Code Explanation:
- Checks the value of each card via a `switch` statement
- The variable `count`:
- Increases by 1 if the card is a 2, 3, 4, 5, or 6
- Since 7, 8, and 9 aren't worth anything, we ignore those cards in our `switch` statement.
- Decreases by 1 if the card is a 10, 'J', 'Q', 'K', or 'A'
- Checks the value of `count` and returns the appropriate response
***Example Run***
**_Example Run_**
- `cc(2);` runs
- The `switch` statement hits `case 2`, jumps down and adds 1 to the variable `count`
@@ -98,10 +106,10 @@ function cc(card) {
- After the `switch` statement, the `if` statement checks `count`, which is now 0
- This then drops down to the `else` statement, which will return **0 Hold**
***Note***
As mentioned earlier, the `switch` statement could have also been an `else if` statement
**_Note_** As mentioned earlier, the `switch` statement could have also been an `else if` statement
# Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @leftynaut for your help with Checkpoint: Counting Cards`**
### Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @leftynaut for your help with Checkpoint: Counting Cards`**
> **NOTE:** Please add your username only if you have added any **relevant main contents** to the wiki page. (Please don't remove any existing usernames.)
@@ -1,6 +1,7 @@
# Create a Block Element Bootstrap Button
Normally, your button elements are only as wide as the text that they contain. By making them block elements, your button will stretch to fill your page's entire horizontal space. Note that these buttons still need the `btn` class.
```html
<button class="btn btn-block">Like</button>
```
# Challenge Create a Block Element Bootstrap Button
Normally, your button elements are only as wide as the text that they contain. By making them block elements, your button will stretch to fill your page's entire horizontal space. Note that these buttons still need the `btn` class.
```html
<button class="btn btn-block">Like</button>
```
@@ -1,6 +1,7 @@
# Create a Bootstrap Button
Bootstrap has its own styles for button elements, which look much better than the plain HTML ones.
```html
<button type="submit" class="btn">Like</button>
```
# Challenge Create a Bootstrap Button
Bootstrap has its own styles for button elements, which look much better than the plain HTML ones.
```html
<button type="submit" class="btn">Like</button>
```
+7
@@ -0,0 +1,7 @@
# Challenge Create a Bootstrap Headline
To create a headline in boostrap you will need to add the class `.text-primary`.
```html
<h3 class="text-primary text-center"> jQuery Playground </h3>
```
+7
@@ -0,0 +1,7 @@
# Challenge Create a Bootstrap Row
Create a div element with the class row.
```html
<div class="row"></div>
```
@@ -1,14 +1,16 @@
HTML has a special element for creating unordered lists, or bullet point-style lists.
Unordered lists start with a `<ul>` element. Then they contain some number of `<li>` elements.
For example:
```html
<ul>
<li>milk</li>
<li>cheese</li>
</ul>
```
would create a bullet point-style list of "milk" and "cheese".
# Challenge Create a Bulleted Underscore List
HTML has a special element for creating unordered lists, or bullet point-style lists.
Unordered lists start with a `<ul>` element. Then they contain some number of `<li>` elements.
For example:
```html
<ul>
<li>milk</li>
<li>cheese</li>
</ul>
```
would create a bullet point-style list of "milk" and "cheese".
@@ -1,4 +1,5 @@
# Create a Class to Target with jQuery Selectors
Not every class needs to have corresponding CSS. Sometimes we create classes just for the purpose of selecting these elements more easily using jQuery.
For this we use the `target` class on the `button` elements.
# Challenge Create a Class to Target with jQuery Selectors
Not every class needs to have corresponding CSS. Sometimes we create classes just for the purpose of selecting these elements more easily using jQuery.
For this we use the `target` class on the `button` elements.
@@ -1,12 +1,13 @@
# Create a Custom Heading
Using `div` and the custom grid layout we can create our own heading.
```html
<div class="row">
<div class="col-xs-8">
<h2 class="text-primary text-center">CatPhotoApp</h2>
</div>
<div class="col-xs-4">
<a href="#"><img class="img-responsive thick-green-border" src="https://bit.ly/fcc-relaxing-cat"></a>
</div>
```
# Challenge Create a Custom Heading
Using `div` and the custom grid layout we can create our own heading.
```html
<div class="row">
<div class="col-xs-8">
<h2 class="text-primary text-center">CatPhotoApp</h2>
</div>
<div class="col-xs-4">
<a href="#"><img class="img-responsive thick-green-border" src="https://bit.ly/fcc-relaxing-cat"></a>
</div>
```
+9
@@ -0,0 +1,9 @@
# Challenge Create a Form Element
You can build web forms that actually submit data to a server using nothing more than pure HTML. You can do this by specifying an action on your `form` element.
For example:
```javascript
<form action="/url-where-you-want-to-submit-form-data"></form>
```
@@ -1,8 +1,9 @@
# Create a JavaScript Slot Machine
For this we have to generate three random numbers using the formula they give us and not the general one. `Math.floor(Math.random() * (3 - 1 + 1)) + 1;`
```javascript
slotOne = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
slotTwo = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
slotThree = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
```
# Challenge Create a JavaScript Slot Machine
For this we have to generate three random numbers using the formula they give us and not the general one. `Math.floor(Math.random() * (3 - 1 + 1)) + 1;`
```javascript
slotOne = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
slotTwo = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
slotThree = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
```
@@ -1,5 +1,8 @@
Checkboxes are a type of input.
- Each of your checkboxes should be nested within its own label element.
- All related checkbox inputs should have the same name attribute.
Here's an example of a checkbox: `<label><input type="checkbox" name="personality"> Loving</label>`.
# Challenge Create a Set of Checkboxes
Checkboxes are a type of input.
- Each of your checkboxes should be nested within its own label element.
- All related checkbox inputs should have the same name attribute.
Here's an example of a checkbox: `<label><input type="checkbox" name="personality"> Loving</label>`.
@@ -1,5 +1,11 @@
You can use radio buttons for questions where you want the user to only give you one answer.
Radio buttons are a type of input. They should all be nested in their own label element. Furthermore, all related radio buttons should have the same name attribute.
Here's an example of a radio button: `<label><input type="radio" name="indoor-outdoor"> Indoor</label>`.
# Challenge Create a Set of Radio Buttons
You can use radio buttons for questions where you want the user to only give you one answer.
Radio buttons are a type of input. They should all be nested in their own label element. Furthermore, all related radio buttons should have the same name attribute.
Here's an example of a radio button:
```javascript
<label><input type="radio" name="indoor-outdoor"> Indoor</label>
```
+11
@@ -0,0 +1,11 @@
# Challenge Create a Text Field
Text inputs are a convenient way to get input from your user.
You can create one like this:
```javascript
<input type="text">
```
Note that input elements are self-closing.
@@ -1,14 +1,16 @@
HTML has a special element for creating ordered lists, or numbered-style lists.
Ordered lists start with a `<ol>` element. Then they contain some number of `<li>` elements.
For example:
```html
<ol>
<li>hydrogen</li>
<li>helium</li>
</ol>
```
would create a numbered list of "hydrogen" and "helium".
# Challenge Create an Ordered list
HTML has a special element for creating ordered lists, or numbered-style lists.
Ordered lists start with a `<ol>` element. Then they contain some number of `<li>` elements.
For example:
```html
<ol>
<li>hydrogen</li>
<li>helium</li>
</ol>
```
would create a numbered list of "hydrogen" and "helium".
+23 -26
@@ -1,26 +1,23 @@
# Create Bootstrap Wells
Bootstrap has a class called `well` that can create a visual sense of depth for your columns.
Nest one div element with the class well within each of your `col-xs-6 div` elements.
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
</div>
</div>
<div class="col-xs-6">
<div class="well">
</div>
</div>
</div>
</div>
```
# Challenge Create Bootstrap Wells
Bootstrap has a class called `well` that can create a visual sense of depth for your columns.
Nest one div element with the class well within each of your `col-xs-6 div` elements.
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<div class="well">
</div>
</div>
<div class="col-xs-6">
<div class="well">
</div>
</div>
</div>
</div>
```
@@ -0,0 +1,7 @@
# Challenge Create Decimal Numbers with JavaScript
JavaScript number variables can have decimals.
```javascript
var myDecimal = 2.8;
```
@@ -1,2 +0,0 @@
# Create Decimal Numbers with JavaScript
JavaScript number variables can have decimals. `var myDecimal = 2.8;`
-4
@@ -1,4 +0,0 @@
# Create a Bootstrap Headline
```html
<h3 class="text-primary text-center"> jQuery Playground </h3>
```
-2
@@ -1,2 +0,0 @@
# Create a Bootstrap Row
Create a div element with the class row. `<div class="row"></div>`
-3
@@ -1,3 +0,0 @@
You can build web forms that actually submit data to a server using nothing more than pure HTML. You can do this by specifying an action on your `form` element.
For example: `<form action="/url-where-you-want-to-submit-form-data"></form>`.
-3
@@ -1,3 +0,0 @@
Text inputs are a convenient way to get input from your user.
You can create one like this: `<input type="text">`. Note that input elements are self-closing.
@@ -1,11 +1,11 @@
## Declare JavaScript Objects as Variables
This has a simple format. You declare your variable and have it equal to an object in the form `{ key: value}`
```javascript
var car = {
"wheels":4,
"engines":1,
"seats":5
};
```
# Challenge Declare JavaScript Objects as Variables
This has a simple format. You declare your variable and have it equal to an object in the form `{ key: value}`
```javascript
var car = {
"wheels":4,
"engines":1,
"seats":5
};
```
+9 -4
@@ -1,4 +1,9 @@
# Declare JavaScript Variables
When we store data in a data structure, we call it a variable. JavaScript variables are written in `camel case`. An example of camel case is: `camelCase`.
You can declare a variable this way `var myName = "Rafael";`
# Challenge Declare JavaScript Variables
When we store data in a data structure, we call it a variable. JavaScript variables are written in `camel case`. An example of camel case is: `camelCase`.
You can declare a variable this way
```javascript
var myName = "Rafael";
```
+7 -4
@@ -1,4 +1,7 @@
# Declare String Variables
A String variable. It is nothing more than a "string" of characters. JavaScript strings are always wrapped in quotes.
`var myFirstName = 'Rafael';`
# Challenge Declare String Variables
A String variable. It is nothing more than a "string" of characters. JavaScript strings are always wrapped in quotes.
```javascript
var myFirstName = 'Rafael';
```
@@ -1,10 +1,15 @@
# Decrement a Number with Javascript
# Challenge Decrement a Number with Javascript
You can easily decrement or decrease a variable by `1` with the `--` operator.
`i--;`
```javascript
i--;
```
is the equivalent of
`i = i - 1;`
```javascript
i = i - 1;
```
**Note:** The entire line becomes `i--;`, eliminating the need for the equal sign.
+3 -1
@@ -1 +1,3 @@
Deleting elements is very simple. All you have to do is remove everything from the opening to the closing of the element and it will be removed. No extra code is needed.
# Challenge Delete HTML Elements
Deleting elements is very simple. All you have to do is remove everything from the opening to the closing of the element and it will be removed. No extra code is needed.
@@ -1,26 +1,30 @@
# Delete Properties from a JavaScript Object
# Challenge Delete Properties from a JavaScript Object
We can also delete properties from objects like this:
```js
```javascript
delete ourDog.bark;
```
The **delete operator** removes a property from an object.
### Syntax
## Syntax
`delete expression` where expression should evaluate to a property reference, e.g.:
```js
```javascript
delete object.property
delete object['property']
```
### Parameters
**object** The name of an object, or an expression evaluating to an object.
## Parameters
**property** The property to delete.
**object** The name of an object, or an expression evaluating to an object.
**property** The property to delete.
## Return value
### Return value
Throws in [strict](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode) mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases.
[Read more](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete)
@@ -1,2 +1,3 @@
# Delete your jQuery Functions
To delete the functions it is just as any other piece of code that you want to remove, select it and delete with the keyboard.
# Challenge Delete your jQuery Functions
To delete the functions it is just as any other piece of code that you want to remove, select it and delete with the keyboard.
@@ -1,8 +1,13 @@
# Disable an Element Using jQuery
You can also change the non-CSS properties of HTML elements with jQuery. For example, you can disable buttons.
When you disable a button, it will become grayed-out and can no longer be clicked.
jQuery has a function called `.prop()` that allows you to adjust the properties of elements.
Here's how you would disable all buttons: `$('#button').prop('disabled', true);`
# Challenge Disable an Element Using jQuery
You can also change the non-CSS properties of HTML elements with jQuery. For example, you can disable buttons.
When you disable a button, it will become grayed-out and can no longer be clicked.
jQuery has a function called `.prop()` that allows you to adjust the properties of elements.
Here's how you would disable all buttons:
```javascript
$('#button').prop('disabled', true);
```
@@ -1,4 +1,5 @@
# Ditch Custom CSS for Bootstrap
We can clean up our code and make our Cat Photo App look more conventional by using Bootstrap's built-in styles instead of the custom styles we created earlier.
All you need to know is the built in [classes](http://getbootstrap.com/css/).
# Challenge Ditch Custom CSS for Bootstrap
We can clean up our code and make our Cat Photo App look more conventional by using Bootstrap's built-in styles instead of the custom styles we created earlier.
All you need to know is the built in [classes](http://getbootstrap.com/css/).
@@ -0,0 +1,7 @@
# Challenge Divide One Number by Another with JavaScript
JavaScript uses use the `/` symbol for division.
```javascript
var quotient = 66 / 33;
```
@@ -1,2 +0,0 @@
# Divide One Number by Another with JavaScript
JavaScript uses use the `/` symbol for division. `var quotient = 66 / 33;`
@@ -1,11 +1,12 @@
# Escape Sequences in Strings
# Challenge Escape Sequences in Strings
Quotes are not the only characters that can be escaped inside a string. Here is a table of common escape sequences:
**Code** | **Output**
-------- | ---------------
\' | single quote
\" | double quote
\\ | backslash
\ | backslash
\n | new line
\r | carriage return
\t | tab
@@ -1,8 +1,17 @@
# Escaping Literal Quotes in Strings
# Challenge Escaping Literal Quotes in Strings
In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash `\` in front of the quote.
`"Alan said, \"Peter is learning JavaScript\"."`
```javascript
"Alan said, \"Peter is learning JavaScript\"."
```
This signals JavaScript that the following quote is not the end of the string, but should instead appear inside the string.
However another option is to alternate quotation marks if possible. `'Alan said, "Peter is learning JavaScript"'` this would also work.
However another option is to alternate quotation marks if possible.
```javascript
'Alan said, "Peter is learning JavaScript"'
```
this would also work.
@@ -1,11 +1,13 @@
Web developers traditionally use **lorem ipsum** text as placeholder text. It's called lorem ipsum text because those are the first two words of a famous passage by Cicero of Ancient Rome.
**lorem ipsum** text has been used as placeholder text by typesetters since the 16th century, and this tradition continues on the web.
```html
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
<p>Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.</p>
```
# Challenge Fill in the Blank with Placeholder Text
Web developers traditionally use **lorem ipsum** text as placeholder text. It's called lorem ipsum text because those are the first two words of a famous passage by Cicero of Ancient Rome.
**lorem ipsum** text has been used as placeholder text by typesetters since the 16th century, and this tradition continues on the web.
```html
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
<p>Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.</p>
```
@@ -1,9 +1,11 @@
Filter is a useful method that can filter out values that don't match a certain criteria.
```javascript
var array = [1,2,3,4,5,6,7,8,9,10];
// Only change code below this line.
array = array.filter(function(val) {
return val <= 5;
});
```
# Challenge Filter Arrays with Filter
Filter is a useful method that can filter out values that don't match a certain criteria.
```javascript
var array = [1,2,3,4,5,6,7,8,9,10];
// Only change code below this line.
array = array.filter(function(val) {
return val <= 5;
});
```
@@ -1,6 +1,7 @@
# Find Numbers with Regular Expressions
We can use special selectors in Regular Expressions to select a particular type of value.
One such selector is the digit selector `\d` which is used to grab the numbers in a string.
It is used like this: `/\d+/g`.
# Challenge Find Numbers with Regular Expressions
We can use special selectors in Regular Expressions to select a particular type of value.
One such selector is the digit selector `\d` which is used to grab the numbers in a string.
It is used like this: `/\d+/g`.
@@ -1,4 +1,5 @@
# Find the Length of a String
# Challenge Find the Length of a String
You can find the _length_ of a String value by writing `.length` after the string variable or string literal.
`"Alan Peter".length; // 10`
@@ -1,6 +1,7 @@
# Find White Space with Regular Expressions
We can also use selectors like`\s` to find spaces in a string.
It is used like this:
`/\s+/g`
# Challenge Find White Space with Regular Expressions
We can also use selectors like`\s` to find spaces in a string.
It is used like this:
`/\s+/g`
@@ -1,4 +1,5 @@
# Finding a Remainder in Javascript
# Challenge Finding a Remainder in Javascript
The _remainder operator_ `%` gives the remainder of the division of two numbers.
## Example
@@ -11,6 +12,7 @@ Math.floor(5 / 2) = 2 (Quotient)
```
## Usage
In mathematics, a number can be checked even or odd by checking the remainder of the division of the number by 2.
```
@@ -1,2 +1,3 @@
# Generate Random Fractions with JavaScript
JavaScript has a `Math.random()` function that generates a random decimal number.
# Challenge Generate Random Fractions with JavaScript
JavaScript has a `Math.random()` function that generates a random decimal number.
@@ -1,3 +1,5 @@
# Challenge Generate Random Whole Numbers with JavaScript
It's great that we can create random decimal numbers, but it's even more useful if we lot more useful to generate a random whole number.
To achieve this we can multiply the random number by ten and use the `Math.floor()` to convert the decimal number to a whole number
@@ -1,55 +1,51 @@
# Generate Random Whole Numbers within a Range
> We can use a certain mathematical expression to get a random number between two numbers.
`Math.floor(Math.random() * (max - min + 1)) + min`
**See Also:** :scroll: [**Explanation**](Challenge-Generate-Random-Whole-Numbers-within-a-Range#explanation) | [Explanation in SO](https://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range/1527820#1527820)
## Explanation
> `Math.random()` creates a floating point number between `0` (inclusive) and `1` (exclusive)
**Q:** Create a random **whole** number between `2` (inclusive, min) and `6` (inclusive, max) i.e. create a random *whole* number between `min` (*inclusive*) and max (*inclusive*)
**A:** `Math.floor(Math.random() * (6 - 2 + 1)) + 2`
Let's write `Math.random() * max` which creates a floating point number between `0` (inclusive) and `max` (*exclusive*).
E.g. `Math.random() * 6` generates a random floating point number between `0` (inclusive) and `6` (*exclusive*).
----
But we need to generate a whole number between `0` (*inclusive*) and `max` (*inclusive*) first. So we do a trick :wink:.
What if we create a number between `0` and `max + 1`. That way, we also include the `max` number. And then apply the `Math.floor` method on the result to get a whole number between **`0`** (*inclusive*, min) and `max` (*inclusive*).
We can write it as `Math.floor(Math.random() * (max + 1))`.
E.g. `Math.floor(Math.random() * (6 + 1))` generates a random floating point number between **`0`** (*inclusive*, min) and **`6`** (*inclusive*, max).
**`0 1 2 3 4 5 6`**
> The `Math.random() * (6 + 1)` part generates a floating point number between `0` (*inclusive*) and `7` (*exclusive*).
**`0..1..2..3..4..5..6..`**
----
But that doesn't solve our given question yet :disappointed:. We need to generate a random whole number between **`min`** (*inclusive*) and `max` (*inclusive*). So we need another trick :wink:.
What if we write `Math.floor(Math.random() * ((max + 1) - min))` that will generate a whole number between `0` and `(max + 1) - min`.
E.g. `Math.floor(Math.random() * ((6 + 1) - 2))` **=** `Math.floor(Math.random() * 5)` that will generate a whole number between `0` (inclusive) and `4` (inclusive).
**`0 1 2 3 4`**
----
But still the question is unsolved :disappointed:. We need the random number between `min` (**inclusive**) and `max` (**inclusive**).
So we simply write `Math.floor(Math.random() * ((max + 1) - min)) + min` :stuck_out_tongue_winking_eye:.
E.g. `Math.floor(Math.random() * ((6 + 1) - 2)) + 2` **=** `Math.floor(Math.random() * 5) + 2`
**`2 3 4 5 6`**
**Now this answers our given question. `Math.floor(Math.random() * (max - min + 1) + min` correctly generates a random number between `min` (*inclusive*) and `max` (*inclusive*) i.e. it generates a random whole number between `2` (inclusive) and `6` (inclusive).** :smiley:
# Challenge Generate Random Whole Numbers within a Range
> We can use a certain mathematical expression to get a random number between two numbers.
`Math.floor(Math.random() * (max - min + 1)) + min`
**See Also:** :scroll: [**Explanation**](Challenge-Generate-Random-Whole-Numbers-Within-A-Range#explanation) | [Explanation in SO](https://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range/1527820#1527820)
## Explanation
> `Math.random()` creates a floating point number between `0` (inclusive) and `1` (exclusive)
**Q:** Create a random **whole** number between `2` (inclusive, min) and `6` (inclusive, max) i.e. create a random _whole_ number between `min` (_inclusive_) and max (_inclusive_)
**A:** `Math.floor(Math.random() * (6 - 2 + 1)) + 2`
Let's write `Math.random() * max` which creates a floating point number between `0` (inclusive) and `max` (_exclusive_).
E.g. `Math.random() * 6` generates a random floating point number between `0` (inclusive) and `6` (_exclusive_).
But we need to generate a whole number between `0` (_inclusive_) and `max` (_inclusive_) first. So we do a trick :wink:.
What if we create a number between `0` and `max + 1`. That way, we also include the `max` number. And then apply the `Math.floor` method on the result to get a whole number between **`0`** (_inclusive_, min) and `max` (_inclusive_).
We can write it as `Math.floor(Math.random() * (max + 1))`.
E.g. `Math.floor(Math.random() * (6 + 1))` generates a random floating point number between **`0`** (_inclusive_, min) and **`6`** (_inclusive_, max).
**`0 1 2 3 4 5 6`**
> The `Math.random() * (6 + 1)` part generates a floating point number between `0` (_inclusive_) and `7` (_exclusive_).
**`0..1..2..3..4..5..6..`**
But that doesn't solve our given question yet :disappointed:. We need to generate a random whole number between **`min`** (_inclusive_) and `max` (_inclusive_). So we need another trick :wink:.
What if we write `Math.floor(Math.random() * ((max + 1) - min))` that will generate a whole number between `0` and `(max + 1) - min`.
E.g. `Math.floor(Math.random() * ((6 + 1) - 2))` **=** `Math.floor(Math.random() * 5)` that will generate a whole number between `0` (inclusive) and `4` (inclusive).
**`0 1 2 3 4`**
But still the question is unsolved :disappointed:. We need the random number between `min` (**inclusive**) and `max` (**inclusive**).
So we simply write `Math.floor(Math.random() * ((max + 1) - min)) + min` :stuck_out_tongue_winking_eye:.
E.g. `Math.floor(Math.random() * ((6 + 1) - 2)) + 2` **=** `Math.floor(Math.random() * 5) + 2`
**`2 3 4 5 6`**
**Now this answers our given question. `Math.floor(Math.random() * (max - min + 1) + min` correctly generates a random number between `min` (_inclusive_) and `max` (_inclusive_) i.e. it generates a random whole number between `2` (inclusive) and `6` (inclusive).** :smiley:
@@ -1,4 +1,5 @@
## Get Geo-location Data
# Challenge Get Geo-location Data
Every browser has a built in navigator that can give us this information.
The navigator will get our user's current longitude and latitude.
@@ -9,7 +10,7 @@ By selecting allow you will see the text on the output phone change to your lati
Here's some code that does this:
```js
```javascript
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
@@ -1,4 +1,5 @@
# Get JSON with the jQuery getJSON Method
# Challenge Get JSON with the jQuery getJSON Method
Application Programming Interfaces - are tools that computers use to communicate with one another.
Most web APIs transfer data in a format called JSON. JSON stands for JavaScript Object Notation. JSON is nothing more than object properties and their current values, sandwiched between a `{` and a `}`.
@@ -7,7 +8,7 @@ These properties and their values are often referred to as "key-value pairs".
Here is a sample of what it looks like.
```js
```javascript
$.getJSON("/json/cats.json", function(json) {
$(".message").html(JSON.stringify(json));
@@ -1,3 +1,5 @@
You can set an element's background color with the `background-color` attribute.
For example, if you wanted an element's background color to be green, you'd use `.green-background { background-color: green; }` within your `style` element.
# Challenge Give a Background Color to a Div Element
You can set an element's background color with the `background-color` attribute.
For example, if you wanted an element's background color to be green, you'd use `.green-background { background-color: green; }` within your `style` element.
@@ -1,2 +1,3 @@
# Give Each Element a Unique ID
We will also want to be able to use jQuery to target each button by its unique id. So we add an unique id to each button.
# Challenge Give Each Element a Unique ID
We will also want to be able to use jQuery to target each button by its unique id. So we add an unique id to each button.
@@ -1,8 +1,9 @@
# Give your JavaScript Slot Machine some stylish images
We've already set up the images for you in an array called images. We can use different indexes to grab each of these.
Here's how we would set the first slot to show a different image depending on which number its random number generates:
```javascript
$($('.slot')[0]).html('<img src = "' + images[slotOne-1] + '">');
```
# Challenge Give your JavaScript Slot Machine some stylish images
We've already set up the images for you in an array called images. We can use different indexes to grab each of these.
Here's how we would set the first slot to show a different image depending on which number its random number generates:
```javascript
$($('.slot')[0]).html('<img src = "' + images[slotOne-1] + '">');
```
+2 -2
@@ -1,5 +1,5 @@
#Challenge: Global Scope and Functions
# Challenge Global Scope and Functions
In JavaScript, `scope` refers to the visibility of variables. Variables which are defined outside of a function block have `Global` scope. This means, they can be seen everywhere in your JavaScript code.
Variables which are used without the `var` keyword are automatically created in the `global` scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with `var`.
Variables which are used without the `var` keyword are automatically created in the `global` scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with `var`.
+3 -3
@@ -1,10 +1,10 @@
#Challenge: Global vs Local Scope in Functions
# Challenge Global vs Local Scope in Functions
It is possible to have both `local` and `global` variables with the same name. When you do this, the local variable takes precedence over the `global` variable.
In this example:
```js
```javascript
var someVar = "Hat";
function myOutfit() {
var someVar = "Head";
@@ -12,4 +12,4 @@ function myOutfit() {
}
```
The function `myOutfit` will return `"Head"` because the `local` version of the variable is present.
The function `myOutfit` will return `"Head"` because the `local` version of the variable is present.
+24 -15
@@ -1,51 +1,58 @@
# Details
# Challenge Gold Code
We will now use our knowledge about else if statements and comparison with equality, less and greater operators.
In the game of golf each hole has a par for the average number of strokes needed to sink the ball. Depending on how far above or below par your strokes are, there is a different nickname.
Your function will be passed a par and strokes. Return strings according to this table (based on 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!" |
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.
Remember to use [ Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
Remember to use [Read-Search-Ask](FreeCodeCamp-Get-Help) if you get stuck. Try to pair program. Write your own code.
## Useful Links
- [Challenge: Chaining If Else Statements](http://www.freecodecamp.com/challenges/chaining-if-else-statements)
- [Challenge: Comparison with the Greater Than Equal To Operator](http://www.freecodecamp.com/challenges/comparison-with-the-greater-than-equal-to-operator)
- [Challenge: Comparison with the Less Than Equal To Operator](http://www.freecodecamp.com/challenges/comparison-with-the-less-than-equal-to-operator)
## Problem Explanation:
- Change the code below `// Only change code below this line` and above `// Only change code above this line`
- Take note that you are editing the inside of the `golfScore` function.
- You will have to make the function return exactly the same string as shown shown on table, depending on the value of the parameters `par` and `strokes` that are passed in to your function.
## Hint: 1
- `+number -number` can be used to increase or decrease a parameter in your condition.
## Hint: 2
- You can chain else if statements to return different values in different scenarios.
## Hint: 3
- Control the flow of your function based on the tables order of priority - top (highest) to bottom (lowest) to return matching string values.
## Spoiler Alert!
[![687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/thumb/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)
**Solution ahead!**
## Code Solution:
```js
```javascript
function golfScore(par, strokes) {
// Only change code below this line
if (strokes == 1){
@@ -69,12 +76,14 @@ function golfScore(par, strokes) {
golfScore(5, 4);
```
# Code Explanation:
### Code Explanation:
- Comparing the `parameters` stroke and par value to return appropriated string value.
- Using else if statement for flow control
- else will return string "Go home!" to every condition where strokes are equal to par +3 or higher
# Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @osterbergmarcus for your help with Checkpoint: Golf Code`**
### Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @osterbergmarcus for your help with Checkpoint: Golf Code`**
> **NOTE:** Please add your username only if you have added any **relevant main contents** to the wiki page. (Please don't remove any existing usernames.)
@@ -1,6 +1,8 @@
You can create different levels of Heading elements by using **h1, h2, h3, h4, h5, h6** which will result on different sizes.
```html
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
```
# Challenge Headline with the h2 Element
You can create different levels of Heading elements by using **h1, h2, h3, h4, h5, h6** which will result on different sizes.
```html
<h1>Hello World</h1>
<h2>CatPhotoApp</h2>
```
@@ -1,8 +1,9 @@
# House our page within a Bootstrap Container Fluid Div
To make the content responsive, we use the `container-fluid` class.
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
</div>
```
# Challenge House our page within a Bootstrap Container Fluid Div
To make the content responsive, we use the `container-fluid` class.
```html
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
</div>
```
+9
@@ -0,0 +1,9 @@
# Challenge Import a Google Font
To import a font from Google or any other site, this is the format that you should follow:
```html
<link href="http://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css">
```
Once imported, it can then be used in the font-family property
-3
@@ -1,3 +0,0 @@
To import a font from Google or any other site, this is the format that you should follow: `<link href="http://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css">`
Once imported, it can then be used in the font-family property
@@ -1,10 +1,15 @@
# Increment a Number with Javascript
# Challenge Increment a Number with Javascript
You can easily increment or add `1` to a variable with the `++` operator.
`i++;`
```javascript
i++;
```
is the equivalent of
`i = i + 1;`
```javascript
i = i + 1;
```
**Note** The entire line becomes `i++;`, eliminating the need for the equal sign.

Some files were not shown because too many files have changed in this diff Show More