|
| 1 | +// Practice with a single number |
| 2 | +const num = 12; |
| 3 | + |
| 4 | +// Ignore any lines that look like this |
| 5 | +process.stdout.write('1. '); |
| 6 | + |
| 7 | +/* |
| 8 | +1. Only print the following message if num is greater than 10. |
| 9 | +Try changing the value of num to make sure your code works. |
| 10 | +*/ |
| 11 | +console.log('num is greater than 10'); |
| 12 | + |
| 13 | +process.stdout.write('\n2. '); |
| 14 | +/* |
| 15 | +2. Only print the message 'num is less than 100' when appropriate. |
| 16 | +*/ |
| 17 | + |
| 18 | + |
| 19 | +process.stdout.write('\n3. '); |
| 20 | +/* |
| 21 | +3. Now do the same with 'num is a positive number'. |
| 22 | +*/ |
| 23 | + |
| 24 | + |
| 25 | +process.stdout.write('\n4. '); |
| 26 | +/* |
| 27 | +4. 'num is between 10 and 100' (exclusive) |
| 28 | +*/ |
| 29 | + |
| 30 | + |
| 31 | +process.stdout.write('\n5. '); |
| 32 | +/* |
| 33 | +5. 'num is even' |
| 34 | +*/ |
| 35 | + |
| 36 | + |
| 37 | +process.stdout.write('\n6. '); |
| 38 | +/* |
| 39 | +6. 'num is an even number between 20 and 30' (inclusive) |
| 40 | +*/ |
| 41 | + |
| 42 | + |
| 43 | +process.stdout.write('\n7. '); |
| 44 | +/* |
| 45 | +7. 'num is odd or negative' |
| 46 | +*/ |
| 47 | + |
| 48 | + |
| 49 | +// Practice comparing numbers |
| 50 | +const x = 3; |
| 51 | +const y = 5; |
| 52 | + |
| 53 | +process.stdout.write('\n8. '); |
| 54 | +process.stdout.write('The bigger number is: '); |
| 55 | +/* |
| 56 | +8. Print either x or y, whichever is bigger. |
| 57 | +*/ |
| 58 | + |
| 59 | + |
| 60 | +process.stdout.write('\n9. '); |
| 61 | +/* |
| 62 | +9. Print either the message 'x and y are equal' or 'x and y are not equal'. |
| 63 | +*/ |
| 64 | + |
| 65 | + |
| 66 | +// Let's add another number into the mix. |
| 67 | +const z = 2; |
| 68 | + |
| 69 | +process.stdout.write('\n10. '); |
| 70 | +process.stdout.write('The biggest number is: '); |
| 71 | +/* |
| 72 | +10. Print either x, y, or z, whichever is bigger. |
| 73 | +Remember to change the values of x, y, and z to make sure it works! |
| 74 | +*/ |
| 75 | + |
| 76 | + |
| 77 | +// Practice with more operators |
| 78 | +const walletInCents = 25; |
| 79 | +const priceInDollars = 1.25; |
| 80 | + |
| 81 | +process.stdout.write('\n11. '); |
| 82 | +/* |
| 83 | +11. Print 'Exact amount!' when the wallet contains the exact amount for the price. Print 'More than enough.' when there is more than enough money in the wallet. Print 'Not enough money.' when there is not enough money in the wallet. |
| 84 | +
|
| 85 | +Notice that the wallet is in cents and the price is in dollars, so you'll have to do a bit of work to compare them. |
| 86 | +*/ |
| 87 | + |
| 88 | + |
| 89 | +process.stdout.write('\n12. '); |
| 90 | +/* |
| 91 | +12. Do the same thing as in 11, but let's add in some error handling. In the case where the wallet is negative, print 'Wallet cannot be negative.' When the price is negative, print 'Price cannot be negative.' Think about the importance of the order of the conditions. |
| 92 | +*/ |
| 93 | + |
| 94 | + |
| 95 | +// Practice comparing strings |
| 96 | +let username = 'Alice'; |
| 97 | +let password = '1234'; |
| 98 | +const expectedUsername = 'Zed'; |
| 99 | +const expectedPassword = '9876'; |
| 100 | + |
| 101 | +process.stdout.write('\n13. '); |
| 102 | +/* |
| 103 | +13. To simulate authentication, we have a username, a password, and expected values for each. The login is successful if the username and password match the expected values. Otherwise, either the username is wrong or the password is wrong. Print appropriate messages for each of the 3 cases. (Don't worry about the case where the username and password are both wrong; one of the other cases will catch it.) |
| 104 | +*/ |
| 105 | + |
| 106 | + |
| 107 | +// Practice nesting conditionals |
| 108 | +const a = 1; |
| 109 | +const b = 2; |
| 110 | + |
| 111 | +process.stdout.write('\n14. '); |
| 112 | +/* |
| 113 | +14. Print the appropriate message for any of the following cases comparing a and b: |
| 114 | +
|
| 115 | +Matching positive numbers. |
| 116 | +Matching negative numbers. |
| 117 | +No match of positive numbers. |
| 118 | +No match of negative numbers. |
| 119 | +No match. |
| 120 | +*/ |
| 121 | + |
| 122 | + |
| 123 | +// Practice with more information |
| 124 | +const myUsername = 'Alice'; |
| 125 | +const myPassword = '1234'; |
| 126 | +const salary = 25000; |
| 127 | + |
| 128 | +const employeeUsername = 'Zarya'; |
| 129 | +const employeePassword = '9876'; |
| 130 | +const ceoUsername = 'Xyrho'; |
| 131 | +const ceoPassword = 'hunter2'; |
| 132 | +const juniorSalaryMinimum = 50000; |
| 133 | +const juniorSalaryMaximum = 70000; |
| 134 | +const intermediateSalaryMinimum = 75000; |
| 135 | +const intermediateSalaryMaximum = 90000; |
| 136 | +const seniorSalaryMinimum = 95000; |
| 137 | +const seniorSalaryMaximum = 120000; |
| 138 | +const ceoSalaryMinimum = 110000; |
| 139 | + |
| 140 | +process.stdout.write('\n15. '); |
| 141 | +/* |
| 142 | +15. We have another authentication, but this time with a salary as well. We want to print a personalized welcome message to users who log in. For example, if the username and password match the employee username and password and their salary is within the junior range, print 'Welcome, junior employee Zarya.' In the case where the salary is not within any of the set ranges, simply print 'Welcome, employee Zarya.' If the username and password match the CEO credentials but the salary is below the CEO's minimum salary, we have an imposter! For an invalid login, just print 'Invalid login.' (don't worry about whether it was the username or password that didn't match). |
| 143 | +
|
| 144 | +If you notice you're repeating a certain condition more than once, try nesting if statements to avoid the repeitition. |
| 145 | +*/ |
| 146 | + |
| 147 | + |
| 148 | +// Practice complex conditions |
| 149 | +const creditCard = 123; |
| 150 | + |
| 151 | +process.stdout.write('\n16. '); |
| 152 | +/* |
| 153 | +16. Print a message saying whether the credit card number is valid or not. A valid credit card in this system must have all of the following properties: |
| 154 | +
|
| 155 | +- It is exactly 5 digits and doesn't begin with any 0's |
| 156 | +- Tripling it gives an even number |
| 157 | +- It is divisible by either 5 or 7 |
| 158 | +*/ |
| 159 | + |
0 commit comments