
# Coaching - March 30
with Founders and Coders
---
## Recap: Functions
----
### What is a function?
Some lines of code that do a predefined task. A function may take some input and return an output.
----
#### Writing a function
----
```javascript=
function addTwoNumbers(num1,num2){
return num1+num2;
}
var total = addTwoNumbers(2,3)
// total is assigned the returned value
```
---
## For loops
----
For loops will run until a condition is met, usually we set up the condition so the loop runs a fixed number of times.
----
```javascript=
let myArray = [1,2,3,4];
for (let i = 0; i < myArray.length; i++){
myArray[i]++;
};
console.log(myArray)
// What does i++ do?
// What is the output of line 7?
```
---
### While loops
----
While loops allow us to run a block of code until a condition is met.
----
```javascript=
let myNum = 0;
while (myNum < 10){
console.log(myNum);
myNum++
}
```
----
While loops depend on us ensuring the condition is negated somewhere in our code, otherwise the loop will run forever.
----
```javascript=
let myNum = 0;
while (myNum < 10){
console.log(myNum);
}
// This loop will run infinitely.
```
---
## ParseInt
----
parseInt allows us to convert a string to an integer (a whole number).
----
parseInt can accept two parameters:
1. a string
2. a radix
----
```javascript=
let myNumber = 10;
let myString = '10';
console.log(typeof myNumber);
console.log(typeof myString);
```
----
```javascript=
let myNumber = 10;
let myString = '10';
console.log(typeof myNumber);
console.log(typeof parseInt(myString, 10));
```
----
The _radix_ refers to the base of the number system we're using.
Binary uses 2; denary uses 10; and hexadecimal uses 16.
----
In general, we will be working with base 10 numbers (denary) and so we can use 10 as our radix.
---
### toString method
----
We'll cover methods in more detail in coming weeks.
----
Adding .toString() to a variable name will convert it to a string.
----
```javascript=
let myNum = 12;
console.log(typeof myNum)
myNum = myNum.toString()
// What is happening here?
console.log(typeof myNum)
```
---
### Equality operators
----
We use Triple equals (`===`) or double equals (`==`) to check if two things are equivalent.
----
Triple equals checks for strict equality - whether two things are of the same value and type.
Double equals checks if things are the same value.
----
```javascript=
console.log("1" === 1);
// logs false
console.log("1" == 1);
// logs true
```
---
### If statements
----
_If statements_ allow us to define a condition to be met. When that condition is met, the code inside will run.
----
```javascript=
if (myNumber === 2){
console.log('The value is two, and the type is number');
};
```
---
## Ternary Operator
----
We've already seen other operators:
```
=
>
===
--
&&
```
----
The ternarary operator is the conditional operator.
----
The conditional operator assigns a value to a variable based on a condition.
----

----
```javascript=
let apples = 2;
let oranges = 3;
let pineapples = 1;
function fiveADay(fruit){
let enoughFruit = (fruit > 5) ? "Yes!" : "No!";
return enoughFruit;
}
fiveADay(apples);
```
{"metaMigratedAt":"2023-06-15T21:46:04.818Z","metaMigratedFrom":"YAML","title":"Coaching - March 30","breaks":true,"slideOptions":"{\"theme\":\"white\"}","contributors":"[{\"id\":\"2967aacf-1990-431e-b963-91e79ce4a2bf\",\"add\":4699,\"del\":1339}]"}