# CC-01: Conditionals

with Carmen Salas • 2024/10/02

[slides](https://docs.google.com/presentation/d/1MDunIx1dWAuVglV-gMck7KpA3w1XD7SuCxQ285drKmw/edit#slide=id.g287c4ecafef_0_150)

* [Truthys and Falsys](#truthys-and-falsys)
* [Type Hierarchy in JS](#type-hierarchy-in-js)
* [Conditionals Overview](#conditionals-overview)
* [Comparison Operators](#comparison-operators)
* [Code Challenge](#code-challenge)

***

## Truthys and Falsys

Truthy JS values:

* `'0'` - a string containing a single zero
* `'false'` - a string containing 'false'
* `[]` - empty array
* `{}` - empty object
* `function(){}` - an empty function

Falsy JS values:

* `false`
* `0` zero as a number and `-0` minus zero as a number
* `''`, `""` - empty strings
* `null`
* `undefined`
* `NaN`

Important tidbit:

```js
// these are the only ways for `null` and `undefined` to be truthy:
null == null
null == undefined
undefined == undefined
undefined == null
```

{% hint style="info" %}
The only way for `null` and `undefined` to be true when compared with equality operators is if it is compared to each other (or itself).
{% endhint %}

## `typeof` Hierarchy in JS:

```bash
.
├── boolean
├── number
│   ├── NaN
│   └── number
├── object
│   ├── Array
│   ├── null
│   └── object
├── string
└── undefined
```

{% hint style="info" %}
This is not from any official doc, but something I tested when working with the swe-1-3 homework!
{% endhint %}

## Conditionals Overview

```js
if () {} else if () {} else {}
```

## Comparison Operators

The strict equality operator `===` always considers operands of different types to be **different**.

* not all languages implement this operator!

```js
'1' == 1
'1' === 1
```

{% hint style="info" %}
Test this out in node!
{% endhint %}

## Code Challenge

{% hint style="info" %}
These solutions implement the refactored version, and is NOT the neatest way to write them (sorry!).
{% endhint %}

1. Write a function named `greatestOfTwo` that takes in two integer arguments and returns the **largest** of both arguments. You can assume both integers will be different values.

```js
const greatestOfTwo = (a,b) => {
  if (a == b) {
    if (a == undefined) {
      return 'No arguments were passed';
    }
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return (a > b) ? a : b;
  }
}

// tests
console.log(greatestOfTwo(14,1)); // returns 14
console.log(greatestOfTwo(-2,2)); // returns 2
console.log(greatestOfTwo()); // "No arguments were passed"
console.log(greatestOfTwo(-2)); // "Only one argument was passed"
```

2. Write a function named `areBothSame`, that takes in two arguments and returns true if both arguments are truthy in value, and returns false if not.

```js
const areBothSame = (a,b) => {
  if (a == b) {
    if (a == undefined) {
      return 'No arguments were passed';
    }
    return true;
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return false;
  }
}

// tests
console.log(areBothSame(true,1)); // returns true
console.log(areBothSame("2",2)); // returns true
console.log(areBothSame("5",2)); // returns false
console.log(areBothSame(1)); // "Only one argument was passed"
console.log(areBothSame()); // "No arguments were passed"
```

3. Write a function named `areBothEqual`, that takes in two arguments and returns true if both arguments are the same in data type and value, and returns false if they are not.

```js
const areBothEqual = (a,b) => {
  if (a === b) {
    if (a === undefined) {
      return 'No arguments were passed';
    }
    return true;
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return false;
  }
}

// test
console.log(areBothEqual("2", "2")); // returns true
console.log(areBothEqual("2", 2)); // returns false
console.log(areBothEqual(true, 1)); // returns false
console.log(areBothEqual(7, 7)); // returns true
console.log(areBothEqual(7)); // "Only one argument was passed"
console.log(areBothEqual()); // "No arguments were passed"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raffycastlee.gitbook.io/marcyannotes/codechallenge-curriculum/unit-0/20241002.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
