From c143911d68f25bf0bab2240a7dc30f91d2369e7b Mon Sep 17 00:00:00 2001 From: James Canterbury Date: Mon, 22 Apr 2024 16:59:37 +0100 Subject: [PATCH 1/2] Exercises completed --- src/boolean-conditions.js | 14 +++++++++++-- src/multiple-conditions.js | 27 ++++++++++++++++++++---- src/numeric-conditions.js | 20 ++++++++++++++---- src/string-conditions.js | 42 ++++++++++++++++++++++++++++++-------- 4 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/boolean-conditions.js b/src/boolean-conditions.js index 5b73eb4..9916cc1 100644 --- a/src/boolean-conditions.js +++ b/src/boolean-conditions.js @@ -1,13 +1,23 @@ // Initialise the didPass variable with a boolean value -let didPass +let didPass = true // 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 +if (didPass === true) { + answer = 'Well done, you passed!' +} else { + answer = 'Sorry, try again!' +} // 2. When you're done and the test passes, changing didPass to the opposite boolean // and run the test again to make sure it still passes +didPass = false +if (didPass === true) { + answer = 'Well done, you passed!' +} else { + answer = '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..105060a 100644 --- a/src/multiple-conditions.js +++ b/src/multiple-conditions.js @@ -6,17 +6,25 @@ 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 +} // Task 2 -const STR = null +const STR = 'Javascript' // 2. Use conditional statements to set the answerTwo variable below to true // if the STR variable is 'Hello' or 'Goodbye' // 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 + +if (STR === 'Hello' || STR === 'Goodbye') { + answerTwo = true +} else { + answerTwo = false +} // Task 3 const AGE = 0 @@ -33,7 +41,18 @@ const AGE = 0 // 5-12 | Child // 13-19 | Teenager // 20+ | Adult -let answerThree + +if (AGE === 0) { + answerThree = 'Baby' +} else if (AGE >= 1 && AGE <= 4) { + answerThree = 'Toddler' +} 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..4c4aa3f 100644 --- a/src/numeric-conditions.js +++ b/src/numeric-conditions.js @@ -8,17 +8,29 @@ const ARRAY_TWO = ['Hello', 'Conditions', NUM_ONE] // eslint-disable-line no-unu // 1. Use conditional statements to set answerOne to false if ARRAY_ONE is not empty // or true if it is empty -let answerOne +if (ARRAY_ONE.length > 0) { + answerOne = false +} else { + answerOne = true +} // 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 = false +} else { + answerTwo = true +} // 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 +} // 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.includes(NUM_ONE) || ARRAY_TWO.includes(NUM_TWO)) { + answerFour = true +} // Don't edit the code below this line module.exports = { diff --git a/src/string-conditions.js b/src/string-conditions.js index 71d3944..de729ce 100644 --- a/src/string-conditions.js +++ b/src/string-conditions.js @@ -1,45 +1,63 @@ // 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 +if (STR_ONE === 'Hello') { + answerOne = true +} // 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 +if (STR_TWO !== 'Hello') { + answerTwo = true +} // 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 +if (STR_THREE.length > STR_FOUR.length) { + answerThree = true +} // 4. Use conditional statements to set answerFour to true // if STR_FIVE starts and ends with the same character, regardless of case const STR_FIVE = 'Alexandra' // eslint-disable-line no-unused-vars -let answerFour +let strFiveLow = STR_FIVE.toLowerCase() +if (strFiveLow[0] === strFiveLow.charAt(strFiveLow.length-1)) { + answerFour = true +} // 5. Use conditional statements to set answerFive to true // if STR_SIX starts and ends with the same character, regardless of case const STR_SIX = 'Joanna' // eslint-disable-line no-unused-vars -let answerFive +let strSixLow = STR_SIX.toLowerCase() +if (strSixLow [0] === strSixLow.charAt(strSixLow.length-1)) { + answerFive = true +} else { + answerFive = false +} // 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 +if (STR_SEVEN.length%2 === 1) { + answerSix = STR_SEVEN.charAt(STR_SEVEN.length / 2 - 0.5) +} // 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.charAt(STR_EIGHT.length/2-1) + STR_EIGHT.charAt(STR_EIGHT.length/2) +} // 8. Set answerEight to the appropriate season based on what MONTH is set to // @@ -55,7 +73,15 @@ let answerSeven // Run the test after changing the value of MONTH to check you've covered every month correctly 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 { + answerEight = 'Winter' +} module.exports = { answerOne, From e8b498bd51765fae544d7b46c8f1b6ece14b261c Mon Sep 17 00:00:00 2001 From: James Canterbury Date: Mon, 22 Apr 2024 17:01:28 +0100 Subject: [PATCH 2/2] Exercises completed --- src/boolean-conditions.js | 2 ++ src/multiple-conditions.js | 3 +++ src/numeric-conditions.js | 12 ++++++++++-- src/string-conditions.js | 32 +++++++++++++++++++++++++------- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/boolean-conditions.js b/src/boolean-conditions.js index 9916cc1..a51e623 100644 --- a/src/boolean-conditions.js +++ b/src/boolean-conditions.js @@ -4,6 +4,8 @@ let didPass = true // 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 + if (didPass === true) { answer = 'Well done, you passed!' } else { diff --git a/src/multiple-conditions.js b/src/multiple-conditions.js index 105060a..62fc81b 100644 --- a/src/multiple-conditions.js +++ b/src/multiple-conditions.js @@ -6,6 +6,7 @@ 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 @@ -19,6 +20,7 @@ const STR = 'Javascript' // 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 if (STR === 'Hello' || STR === 'Goodbye') { answerTwo = true @@ -41,6 +43,7 @@ const AGE = 0 // 5-12 | Child // 13-19 | Teenager // 20+ | Adult +let answerThree if (AGE === 0) { answerThree = 'Baby' diff --git a/src/numeric-conditions.js b/src/numeric-conditions.js index 4c4aa3f..72c0a1d 100644 --- a/src/numeric-conditions.js +++ b/src/numeric-conditions.js @@ -8,6 +8,8 @@ const ARRAY_TWO = ['Hello', 'Conditions', NUM_ONE] // eslint-disable-line no-unu // 1. Use conditional statements to set answerOne to false if ARRAY_ONE is not empty // or true if it is empty +let answerOne + if (ARRAY_ONE.length > 0) { answerOne = false } else { @@ -16,18 +18,24 @@ if (ARRAY_ONE.length > 0) { // 2. Use conditional statements to set answerTwo to false if ARRAY_TWO is not empty // or true if it is empty -if (ARRAY_TWO.length > 0) { +let answerTwo + +if (ARRAY_TWO.length > 0) { answerTwo = false } else { answerTwo = true } // 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 + answerThree = true } // 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.includes(NUM_ONE) || ARRAY_TWO.includes(NUM_TWO)) { answerFour = true } diff --git a/src/string-conditions.js b/src/string-conditions.js index de729ce..b868763 100644 --- a/src/string-conditions.js +++ b/src/string-conditions.js @@ -1,6 +1,8 @@ // 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 + if (STR_ONE === 'Hello') { answerOne = true } @@ -8,6 +10,8 @@ if (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 + if (STR_TWO !== 'Hello') { answerTwo = true } @@ -17,6 +21,8 @@ if (STR_TWO !== 'Hello') { const STR_THREE = 'Hello' // eslint-disable-line no-unused-vars const STR_FOUR = 'Good' // eslint-disable-line no-unused-vars +let answerThree + if (STR_THREE.length > STR_FOUR.length) { answerThree = true } @@ -26,8 +32,10 @@ if (STR_THREE.length > STR_FOUR.length) { const STR_FIVE = 'Alexandra' // eslint-disable-line no-unused-vars -let strFiveLow = STR_FIVE.toLowerCase() -if (strFiveLow[0] === strFiveLow.charAt(strFiveLow.length-1)) { +let answerFour + +const strFiveLow = STR_FIVE.toLowerCase() +if (strFiveLow[0] === strFiveLow.charAt(strFiveLow.length - 1)) { answerFour = true } @@ -36,8 +44,10 @@ if (strFiveLow[0] === strFiveLow.charAt(strFiveLow.length-1)) { const STR_SIX = 'Joanna' // eslint-disable-line no-unused-vars -let strSixLow = STR_SIX.toLowerCase() -if (strSixLow [0] === strSixLow.charAt(strSixLow.length-1)) { +let answerFive + +const strSixLow = STR_SIX.toLowerCase() +if (strSixLow[0] === strSixLow.charAt(strSixLow.length - 1)) { answerFive = true } else { answerFive = false @@ -47,7 +57,9 @@ if (strSixLow [0] === strSixLow.charAt(strSixLow.length-1)) { // if STR_SEVEN has an odd number of characters const STR_SEVEN = 'Kayla' // eslint-disable-line no-unused-vars -if (STR_SEVEN.length%2 === 1) { +let answerSix + +if (STR_SEVEN.length % 2 === 1) { answerSix = STR_SEVEN.charAt(STR_SEVEN.length / 2 - 0.5) } @@ -55,8 +67,12 @@ if (STR_SEVEN.length%2 === 1) { // STR_EIGHT if STR_EIGHT has an even number of characters const STR_EIGHT = 'Alex' // eslint-disable-line no-unused-vars -if (STR_EIGHT.length%2 === 0) { - answerSeven = STR_EIGHT.charAt(STR_EIGHT.length/2-1) + STR_EIGHT.charAt(STR_EIGHT.length/2) +let answerSeven + +if (STR_EIGHT.length % 2 === 0) { + answerSeven = + STR_EIGHT.charAt(STR_EIGHT.length / 2 - 1) + + STR_EIGHT.charAt(STR_EIGHT.length / 2) } // 8. Set answerEight to the appropriate season based on what MONTH is set to @@ -73,6 +89,8 @@ if (STR_EIGHT.length%2 === 0) { // Run the test after changing the value of MONTH to check you've covered every month correctly const MONTH = 'January' +let answerEight + if (MONTH === 'March' || MONTH === 'April' || MONTH === 'May') { answerEight = 'Spring' } else if (MONTH === 'June' || MONTH === 'July' || MONTH === 'August') {