diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 2ff3cb7..00405d8 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,14 +5,14 @@ version: 2 updates: - - package-ecosystem: "npm" # See documentation for possible values - directory: "/" # Location of package manifests + - package-ecosystem: 'npm' # See documentation for possible values + directory: '/' # Location of package manifests schedule: - interval: "weekly" + interval: 'weekly' - - package-ecosystem: "github-actions" + - package-ecosystem: 'github-actions' # Workflow files stored in the # default location of `.github/workflows` - directory: "/" + directory: '/' schedule: - interval: "weekly" + interval: 'weekly' diff --git a/.github/workflows/standard-criteria-tests.yml b/.github/workflows/standard-criteria-tests.yml index ca418bc..6023e31 100644 --- a/.github/workflows/standard-criteria-tests.yml +++ b/.github/workflows/standard-criteria-tests.yml @@ -1,7 +1,7 @@ name: Standard Criteria on: pull_request: - branches: [ main ] + branches: [main] jobs: build: runs-on: ubuntu-latest diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100644 index 6b54e0b..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -npx eslint src diff --git a/README.md b/README.md index cbc40a3..b02df44 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ ## Instructions - Read through the numbered comments in the following files to understand the requirements of this exercise. + - `src/boolean-conditions.js` - `src/numeric-conditions.js` - `src/string-conditions.js` @@ -21,7 +22,7 @@ - Write your code directly below the numbered comment. -- Add `console.log()`s to get visibility on your solution and run the file with the command `node src/.js` *etc* +- Add `console.log()`s to get visibility on your solution and run the file with the command `node src/.js` _etc_ - See `src/example.js` for some examples @@ -29,7 +30,7 @@ - To verify your solutions, run the test suite by running the `npx jasmine` command followed by the path to the corresponding test file for each `src/*.js` file above, eg: - `npx jasmine spec/boolean-conditions.spec.js` + `npx jasmine spec/boolean-conditions.spec.js` - Alternatively, run: diff --git a/spec/string-conditions.spec.js b/spec/string-conditions.spec.js index 9fe1237..6a08c2b 100644 --- a/spec/string-conditions.spec.js +++ b/spec/string-conditions.spec.js @@ -53,16 +53,18 @@ describe('answerSeven', () => { }) describe('answerEight', () => { - const seasons = { - Winter: ['December', 'January', 'February'], - Autumn: ['September', 'October', 'November'], - Summer: ['June', 'July', 'August'], - Spring: ['March', 'April', 'May'] - } + const seasons = { + Winter: ['December', 'January', 'February'], + Autumn: ['September', 'October', 'November'], + Summer: ['June', 'July', 'August'], + Spring: ['March', 'April', 'May'] + } - const expected = Object.keys(seasons).find(season => seasons[season].includes(MONTH)) + const expected = Object.keys(seasons).find((season) => + seasons[season].includes(MONTH) + ) - it(`should be '${expected}'`, () => { - expect(answerEight).toEqual(expected) - }) + it(`should be '${expected}'`, () => { + expect(answerEight).toEqual(expected) + }) }) diff --git a/spec/support/all-jasmine.json b/spec/support/all-jasmine.json index 24f0083..4d2193b 100644 --- a/spec/support/all-jasmine.json +++ b/spec/support/all-jasmine.json @@ -1,14 +1,9 @@ { - "spec_dir": "spec", - "spec_files": [ - "**/*[sS]pec.?(m)js" - ], - "helpers": [ - "helpers/**/*.?(m)js" - ], - "env": { - "stopSpecOnExpectationFailure": false, - "random": true - } + "spec_dir": "spec", + "spec_files": ["**/*[sS]pec.?(m)js"], + "helpers": ["helpers/**/*.?(m)js"], + "env": { + "stopSpecOnExpectationFailure": false, + "random": true } - \ No newline at end of file +} diff --git a/spec/support/extensions-jasmine.json b/spec/support/extensions-jasmine.json index d41afa2..aa1ae9b 100644 --- a/spec/support/extensions-jasmine.json +++ b/spec/support/extensions-jasmine.json @@ -1,14 +1,9 @@ { - "spec_dir": "spec", - "spec_files": [ - "extensions/**/*[sS]pec.?(m)js" - ], - "helpers": [ - "helpers/**/*.?(m)js" - ], - "env": { - "stopSpecOnExpectationFailure": false, - "random": true - } + "spec_dir": "spec", + "spec_files": ["extensions/**/*[sS]pec.?(m)js"], + "helpers": ["helpers/**/*.?(m)js"], + "env": { + "stopSpecOnExpectationFailure": false, + "random": true } - \ No newline at end of file +} diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index f8d3ca9..99ea7da 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -1,12 +1,7 @@ { "spec_dir": "spec", - "spec_files": [ - "**/*[sS]pec.?(m)js", - "!extensions/**" - ], - "helpers": [ - "helpers/**/*.?(m)js" - ], + "spec_files": ["**/*[sS]pec.?(m)js", "!extensions/**"], + "helpers": ["helpers/**/*.?(m)js"], "env": { "stopSpecOnExpectationFailure": false, "random": true diff --git a/src/README.md b/src/README.md index e3566ef..fdd67c2 100644 --- a/src/README.md +++ b/src/README.md @@ -15,8 +15,7 @@ But more commonly we can also store the result of a comparison: ```javascript const myBool = 5 > 3 //myBool will be set to true -myBool ->>> false +myBool >>> false ``` Rather than comparing fixed numbers like 3 and 5, we can also compare variables: @@ -24,72 +23,81 @@ Rather than comparing fixed numbers like 3 and 5, we can also compare variables: ```javascript const age = 10 const isOver18 = age > 18 //isOver18 will be set to false -isOver18 ->>> false +isOver18 >>> false ``` In the above example we check if `age` is greater than 18, but there are many different types of comparisons available: -* Is equal to `10 === 10` -* Is not equal to `10 !== 10` -* Is greater than `10 > 10` -* Is less than `10 < 10` -* Is greater or equal to `10 >= 10` -* Is less or equal to `10 <= 10` +- Is equal to `10 === 10` +- Is not equal to `10 !== 10` +- Is greater than `10 > 10` +- Is less than `10 < 10` +- Is greater or equal to `10 >= 10` +- Is less or equal to `10 <= 10` As well as comparing numbers, we can also compare strings: ```javascript -"Nicolas" === "Nicolas" ->>> true +'Nicolas' === 'Nicolas' >>> true ``` ```javascript -"Nicolas" === "nicolas" ->>> false // it’s case-sensitive +'Nicolas' === 'nicolas' >>> false // it’s case-sensitive ``` ```javascript -"Nicolas" !== "nicolas" ->>> true +'Nicolas' !== 'nicolas' >>> true ``` We are not limited to a single comparison - we can use multiple comparisons chained together with either the the logical AND operator `&&` or the logical OR operator `||` (2 pipe characters). -For a chained comparison to return true, `&&` requires that *all* combined expressions are also `true`. For example +For a chained comparison to return true, `&&` requires that _all_ combined expressions are also `true`. For example ```javascript let age = 10 -let isTeenager = age > 12 && age < 20 //false, as age is under 20 but not over 12 ->>> false +let isTeenager = + age > 12 && + age < + 20 >>> //false, as age is under 20 but not over 12 + false ``` ```javascript age = 14 -isTeenager = age > 12 && age < 20 //true, as age is under 20 and over 12 ->>> true +isTeenager = + age > 12 && + age < + 20 >>> //true, as age is under 20 and over 12 + true ``` ```javascript age = 21 -isTeenager = age > 12 && age < 20 //false, as age is over 20 even though greater than 12 ->>> false +isTeenager = + age > 12 && + age < + 20 >>> //false, as age is over 20 even though greater than 12 + false ``` -The OR operator `||` requires that *any of* combined expressions are `true` for the chained expression to return true. For example: +The OR operator `||` requires that _any of_ combined expressions are `true` for the chained expression to return true. For example: ```javascript age = 10 let withParent = true -isAllowed = age > 17 || withParent //true, withParent is true even though age>17 is false ->>> true +isAllowed = + age > 17 || + withParent >>> //true, withParent is true even though age>17 is false + true ``` ```javascript age = 10 withParent = false -isAllowed = age > 17 || withParent //false, withParent is false and age>17 is false ->>> false +isAllowed = + age > 17 || + withParent >>> //false, withParent is false and age>17 is false + false ``` ## If Statement @@ -103,11 +111,11 @@ Here's a basic example ```javascript if (true) { console.log("It's true!") -} +} if (false) { console.log("It's not true?") -} +} ``` If we run this code, only the first statement is printed. This is because an `if` statement will only run code in the following block (everything between the curly braces immediately after the `if`) when the expression in between the brackets evaluates to `true`. @@ -118,7 +126,7 @@ For example, we can provide a boolean variable: age = 20 isAllowed = age > 18 if (isAllowed) { - console.log("You can come in!") + console.log('You can come in!') } ``` @@ -126,7 +134,7 @@ But, we can also shorten this and simply provide the expression (`age > 18`) ins ```javascript if (age > 18) { - console.log("You can come in!") + console.log('You can come in!') } ``` @@ -135,14 +143,14 @@ We can also use the logical AND `&&` and logical OR `||` operators to chain expr ```javascript //Only allowed in if age greater than 18 AND less than 65 if (age > 18 && age < 65) { - console.log("You can come in!") + console.log('You can come in!') } ``` ```javascript //Only allowed in if age greater than 18 OR with a parent if (age > 18 || withParent) { - console.log("You can come in!") + console.log('You can come in!') } ``` @@ -151,9 +159,8 @@ If we want to execute some code when an `if` statement evaluates to false, we ca ```javascript //Only allowed in if age greater than 18 OR with a parent if (age > 18) { - console.log("You can come in!") -} -else { + console.log('You can come in!') +} else { console.log("Come back when you're older!") } ``` @@ -161,14 +168,15 @@ else { The `else if` statement can be used to execute alternative code as long another if condition is met: ```javascript -if (age < 18) { //If under 18 +if (age < 18) { + //If under 18 console.log("Come back when you're older!") -} -else if (age > 65) { //If over 18 and older than 65 - console.log("Enjoy your retirement!") -} -else { //If over 18 and younger than 65 - console.log("You can come in!") +} else if (age > 65) { + //If over 18 and older than 65 + console.log('Enjoy your retirement!') +} else { + //If over 18 and younger than 65 + console.log('You can come in!') } ``` @@ -178,25 +186,25 @@ If you have a lot of comparisons to do, the switch statement can sometimes be a ```javascript switch (day) { - case "Monday": - case "Tuesday": - case "Wednesday": - case "Thursday": - case "Friday": - dayType = "Weekday" - break; + case 'Monday': + case 'Tuesday': + case 'Wednesday': + case 'Thursday': + case 'Friday': + dayType = 'Weekday' + break default: - dayType = "Weekend" + dayType = 'Weekend' } ``` ## Next Work your way through the tests for this section. You can use the references below and also -the `example.js` file to see more code samples. Remember you can make use of the Node REPL +the `example.js` file to see more code samples. Remember you can make use of the Node REPL to try out and experiment with code. ## References -* [Boolean Conditional Flow Slides](https://docs.google.com/presentation/d/17YZv-apFaaFM0ICtIwZN2moSpKtS4-Mq-heCy6L7kxo/edit#slide=id.gd46f8ee6d4_0_8) -* [MDN If...Else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) +- [Boolean Conditional Flow Slides](https://docs.google.com/presentation/d/17YZv-apFaaFM0ICtIwZN2moSpKtS4-Mq-heCy6L7kxo/edit#slide=id.gd46f8ee6d4_0_8) +- [MDN If...Else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) diff --git a/src/boolean-conditions.js b/src/boolean-conditions.js index 5b73eb4..f1c6832 100644 --- a/src/boolean-conditions.js +++ b/src/boolean-conditions.js @@ -1,13 +1,17 @@ // Initialise the didPass variable with a boolean value -let didPass +let didPass = false // 1. Create a conditional statement that changes the answer variable to the string // "Well done, you passed!" if didPass is true, or "Sorry, try again!" if didPass // is false -let answer +let answer = (didPass = true) ? 'Well done, you passed!' : 'Sorry, try again!' // 2. When you're done and the test passes, changing didPass to the opposite boolean + +didPass = true + // and run the test again to make sure it still passes +answer = (didPass = true) ? 'Well done, you passed!' : 'Sorry, try again!' // Don't change the code below this line module.exports = { diff --git a/src/multiple-conditions.js b/src/multiple-conditions.js index 46c8e76..642ed6c 100644 --- a/src/multiple-conditions.js +++ b/src/multiple-conditions.js @@ -6,7 +6,11 @@ const NUM = 9 // eslint-disable-line no-unused-vars // 1. Use conditional statements to set the value of the answerOne variable // to be true if the NUM variable is more than or equal to the LOWER variable // AND is less than or equal to the UPPER variable -let answerOne +if (NUM >= LOWER && NUM <= UPPER) { + answerOne = true +} else { + answerOne = false +} // Task 2 const STR = null @@ -16,7 +20,7 @@ const STR = null // Set answerTwo to false if it's neither of those // Run the test after setting STR to 'Hello', then 'Goodbye', then any other value you like // to verify your code is correct -let answerTwo +let answerTwo = STR === 'Hello' || STR === 'Goodbye' // Task 3 const AGE = 0 @@ -35,6 +39,18 @@ const AGE = 0 // 20+ | Adult let answerThree +if (AGE == 0) { + answerThree = 'Baby' +} else if (AGE >= 1 && AGE <= 4) { + answerThree = 'Baby' +} else if (Age >= 5 && AGE <= 12) { + answerThree = 'Child' +} else if (AGE >= 13 && AGE <= 19) { + answerThree = 'Teenager' +} else { + answerThree = 'Adult' +} + // Run the test after changing the AGE value to verify you've successfully // accounted for each age range diff --git a/src/numeric-conditions.js b/src/numeric-conditions.js index 030005f..6cbf129 100644 --- a/src/numeric-conditions.js +++ b/src/numeric-conditions.js @@ -10,15 +10,37 @@ const ARRAY_TWO = ['Hello', 'Conditions', NUM_ONE] // eslint-disable-line no-unu // or true if it is empty let answerOne +if (ARRAY_ONE.length === 0) { + answerOne = true +} else { + answerOne = false +} + // 2. Use conditional statements to set answerTwo to false if ARRAY_TWO is not empty // or true if it is empty let answerTwo +if (ARRAY_TWO.length === 0) { + answerTwo = true +} else { + answerTwo = false +} // 3. Use conditional statements to set answerThree to true if NUM_ONE is more than NUM_TWO let answerThree +if (NUM_ONE > NUM_TWO) { + answerThree = true +} else { + answerThree = false +} + // 4. Use conditional statements to set answerFour to true if NUM_ONE or NUM_TWO are included in ARRAY_TWO let answerFour +if (ARRAY_TWO.indexOf(NUM_ONE) != -1 || ARRAY_TWO.indexOf(NUM_TWO) != -1) { + answerFour = true +} else { + answerFour = false +} // Don't edit the code below this line module.exports = { diff --git a/src/string-conditions.js b/src/string-conditions.js index 71d3944..d86b730 100644 --- a/src/string-conditions.js +++ b/src/string-conditions.js @@ -1,19 +1,19 @@ // 1. Use conditional statements to set answerOne to true if STR_ONE is 'Hello' const STR_ONE = 'Hello' // eslint-disable-line no-unused-vars -let answerOne +let answerOne = STR_ONE === 'Hello' // 2. Use conditional statements to set answerTwo to true if STR_TWO is not 'Hello' const STR_TWO = 'Goodbye' // eslint-disable-line no-unused-vars -let answerTwo +let answerTwo = STR_TWO !== 'Hello' // 3. Use conditional statements to set answerThree to true if STR_THREE is // longer than STR_FOUR const STR_THREE = 'Hello' // eslint-disable-line no-unused-vars const STR_FOUR = 'Good' // eslint-disable-line no-unused-vars -let answerThree +let answerThree = STR_THREE.length > STR_FOUR.length // 4. Use conditional statements to set answerFour to true // if STR_FIVE starts and ends with the same character, regardless of case @@ -21,6 +21,11 @@ let answerThree const STR_FIVE = 'Alexandra' // eslint-disable-line no-unused-vars let answerFour +if (STR_FIVE[0].toUpperCase === STR_FIVE[STR_FIVE.length - 1].toUpperCase) { + answerFour = true +} else { + answerFour = false +} // 5. Use conditional statements to set answerFive to true // if STR_SIX starts and ends with the same character, regardless of case @@ -29,17 +34,39 @@ const STR_SIX = 'Joanna' // eslint-disable-line no-unused-vars let answerFive +if (STR_SIX[0].toUpperCase() === STR_SIX[STR_SIX.length - 1].toUpperCase()) { + answerFive = true +} else { + answerFive = false +} +console.log(STR_SIX[0].toLowerCase()) +console.log(STR_SIX[STR_SIX.length - 1].toLowerCase()) // 6. Use conditional statements to set answerSix to the middle character of STR_SEVEN // if STR_SEVEN has an odd number of characters const STR_SEVEN = 'Kayla' // eslint-disable-line no-unused-vars let answerSix +let even = STR_SEVEN % 2 === 0 +console.log(even) +if (even) { + answerSix = null +} else { + answerSix = STR_SEVEN[(STR_SEVEN.length - 1) / 2] +} // 7. Use conditional statements to set answerSeven to the middle two characters of // STR_EIGHT if STR_EIGHT has an even number of characters const STR_EIGHT = 'Alex' // eslint-disable-line no-unused-vars let answerSeven +if (STR_EIGHT.length % 2 === 0) { + answerSeven = STR_EIGHT.slice( + STR_EIGHT.length / 2 - 1, + STR_EIGHT.length / 2 - 1 + 2 + ) +} else { + answerSeven = null +} // 8. Set answerEight to the appropriate season based on what MONTH is set to // @@ -57,6 +84,24 @@ const MONTH = 'January' let answerEight +if (MONTH === 'March' || MONTH === 'April' || MONTH === 'May') { + answerEight = 'Spring' +} else if (MONTH === 'June' || MONTH === 'July' || MONTH === 'August') { + answerEight = 'Summer' +} else if ( + MONTH === 'September' || + MONTH === 'October' || + MONTH === 'November' +) { + answerEight = 'Autumn' +} else if ( + MONTH === 'December' || + MONTH === 'January' || + MONTH === 'February' +) { + answerEight = 'Winter' +} + module.exports = { answerOne, answerTwo,