You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ramdan Katakpawou 52a0d2d86c added the other exercises +theorie 2 years ago
jsonpackage json package 2 years ago
node_modules/readline-sync json package 2 years ago
.gitignore Initial commit 2 years ago
README.md added the other exercises +theorie 2 years ago
exercise_1.js minor change in last console.log 2 years ago
exercise_2.js anotaties toegevoegd 2 years ago
exercise_3.js added the other exercises +theorie 2 years ago
exercise_4.js added the other exercises +theorie 2 years ago
exercise_5.js exercise 3 for loop done 2 years ago
exercise_6.js excercise while loop tweaked 2 years ago
forLoop.js added the other exercises +theorie 2 years ago
whileLoop.js added the other exercises +theorie 2 years ago

README.md

jsbootcamp

EXERCISE_1 Data and Variables Use the information given below about your space shuttle to complete the exercises: Data Value Name of the space shuttle Determination Shuttle Speed (mph) 17,500 Distance to Mars (km) 225,000,000 Distance to the Moon (km) 384,400 Miles per kilometer 0.621

  1. Declare and assign variables Declare and assign a variable for each item in the list above. Hint: When declaring and assigning your variables, remember that you will need to use that variable throughout the rest of the exercises. Make sure that you are using the correct data type!
  2. Print out the type of each variable For each variable you declared in part A, use the typeof operator to print its type to the console, one item per line.
  3. Calculate a space mission! We need to determine how many days it will take to reach Mars. Create and assign a miles to Mars variable. You can get the miles to Mars by multiplying the distance to Mars in kilometers by the miles per kilometer. Next, we need a variable to hold the hours it would take to get to Mars. To get the hours, you need to divide the miles to Mars by the shuttle’s speed. Finally, declare a variable and assign it the value of days to Mars. In order to get the days it will take to reach Mars, you need to divide the hours it will take to reach Mars by 24.
  4. Print out the results of your calculations Using variables from above, print to the screen a sentence that says: “_ will take ___ days to reach Mars.” Fill in the blanks with the shuttle name and the calculated time.
  5. Now calculate a trip to the Moon Repeat the calculations, but this time determine the number of days it would take to travel to the Moon and print to the screen a sentence that says “_ will take ___ days to reach the Moon.“.

EXERCISE_2 Booleans and conditionals Declare and initialize the following variables for our space shuttle Variable Name Value engineIndicatorLight red blinking spaceSuitsOn true shuttleCabinReady true crewStatus spaceSuitsOn && shuttleCabinReady computerStatusCode 200 shuttleSpeed 15000

  1. Write conditional expressions to satisfy the safety rules. Use the variables defined from the table above to satisfy the rules listed below.

1.1 crewStatus If the value is true, print “Crew Ready” Else print “Crew Not Ready”

1.2 computerStatusCode If the value is 200, print “Please stand by. Computer is rebooting.” Else if the value is 400, print “Success! Computer online.” Else print “ALERT: Computer offline!”

1.3 shuttleSpeed If the value is > 17500, print “ALERT: Escape velocity reached!” Else if the value is < 8000, print “ALERT: Cannot maintain orbit!” Else print “Stable speed”

  1. Monitor the shuttle’s fuel status. Implement the checks below using if / else if / else statements. Order is important when working with conditionals, and the checks below are NOT written in the correct sequence. Please read ALL of the checks before coding and then decide on the best order for the conditionals.

If fuelLevel is above 20000 AND engineTemperature is at or below 2500, print “Full tank. Engines good.” If fuelLevel is above 10000 AND engineTemperature is at or below 2500, print “Fuel level above 50%. Engines good.” If fuelLevel is above 5000 AND engineTemperature is at or below 2500, print “Fuel level above 25%. Engines good.” If fuelLevel is at or below 5000 OR engineTemperature is above 2500, print “Check fuel level. Engines running hot.” If fuelLevel is below 1000 OR engineTemperature is above 3500 OR engineIndicatorLight is red blinking, print “ENGINE FAILURE IMMINENT!” Otherwise, print “Fuel and engine status pending…” Run your code several times to make sure it prints the correct phrase for each set of conditions.

fuelLevel engineTemperature engineIndicatorLight Result Any Any red blinking ENGINE FAILURE IMMINENT! 21000 1200 NOT red blinking Full tank. Engines good. 900 Any Any ENGINE FAILURE IMMINENT! 5000 1200 NOT red blinking Check fuel level. Engines running hot. 12000 2600 NOT red blinking Check fuel level. Engines running hot. 18000 2500 NOT red blinking Fuel level above 50%. Engines good.

EXERCISE_3 Strings Part one Identify the result for each of the following statements:

‘JavaScript’[8] “Strings are sequences of characters.”[5] “Wonderful”.length “Do spaces count?”.length There’s no code snippet for this one, just try it on your own with old-fashioned pen and paper!

The length method returns how many characters are in a string. However, the method will NOT give us the length of a number. If num = 1001, num.length returns undefined rather than 4.

Use type conversion to print the length (number of digits) of an integer. Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6). Modify your code to print out the length of a decimal value EXCLUDING the period. What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases. (Hint: Consider the indexOf() or includes() string methods). Part two Remember, strings are immutable. Consider a string that represents a strand of DNA: dna = “ TCG-TAC-gaC-TAC-CGT-CAG-ACT-TAa-CcA-GTC-cAt-AGA-GCT “. There are some typos in the string that we would like to fix:

Use the trim() method to remove the leading and trailing whitespace, and then print the results. Change all of the letters in the dna string to UPPERCASE and print the result. Note that if you try console.log(dna) after applying the methods, the original, flawed string is displayed. To fix this, you need to reassign the changes back to dna. Apply these fixes to your code so that console.log(dna) prints the DNA strand in UPPERCASE with no whitespace. Let’s use string methods to do more work on the DNA strand:

Replace the sequence ‘GCT’ with ‘AGG’, and then print the altered strand. Look for the sequence ‘CAT’ with indexOf(). If found print, ‘CAT found’, otherwise print, ‘CAT NOT found’. Use slice() to print out the fifth set of 3 characters (called a codon) from the DNA strand. Use a template literal to print, “The DNA strand is ___ characters long.” Just for fun, apply methods to dna and use another template literal to print, ‘taco cat’. Part three If we want to turn the string ‘JavaScript’ into ‘JS’, we might try .remove(). Unfortunately, there is no such method in JavaScript. However, we can use our cleverness to achieve the same result.

Use string concatenation and two slice() methods to print ‘JS’ from ‘JavaScript’. Without using slice(), use method chaining to accomplish the same thing. Use bracket notation and a template literal to print, “The abbreviation for ‘JavaScript’ is ‘JS’.” Just for fun, try chaining 3 or more methods together, and then print the result. Some programming languages (like Python) include a title() method to return a string with Every Word Capitalized (e.g. ‘title case’.title() returns Title Case). JavaScript has no title() method, but that won’t stop us! Use the string methods you know to print ‘Title Case’ from the string ‘title case’.

EXERCISE_4 Arrays OK, rookie. It’s time to train you on how to modify the shuttle’s cargo manifest. The following actions will teach you how to add, remove, modify and rearrange our records for the items stored in our hold.

Create an array called practiceFile with the following entry: 273.15. Use the push method to add the following elements to the array. Add items a & b one at a time, then use a single push to add the items in part c. Print the array after each step to confirm the changes.

42 “hello” false, -4.6, “87” Congratulations, rookie. You can now add items to an array.

push, pop, shift and unshift are used to add/remove elements from the beginning/end of an array. Bracket notation can be used to modify any element within an array. Starting with the cargoHold array [‘oxygen tanks’, ‘space suits’, ‘parrot’, ‘instruction manual’, ‘meal packs’, ‘slinky’, ‘security blanket’], write statements to do the following:

Use bracket notation to replace ‘slinky’ in the array with ‘space tether’. Print the array to confirm the change. Remove the last item from the array with pop. Print the element removed and the updated array. Remove the first item from the array with shift. Print the element removed and the updated array. Unlike pop and shift, push and unshift require arguments inside the (). Add the items 1138 and ‘20 meters’ to the array - the number at the start and the string at the end. Print the updated array to confirm the changes. Use a template literal to print the final array and its length. Status check, rookie. Which array methods ADD items, and where are the new entries placed? Which methods REMOVE items, and where do the entries come from? Which methods require entries inside the ()?

The splice method can be used to either add or remove items from an array. It can also accomplish both tasks at the same time. Review the splice documentation if you need a syntax reminder. Use splice to make the following changes to the final cargoHold array from exercise 2. Be sure to print the array after each step to confirm your updates.

Insert the string ‘keys’ at index 3 without replacing any other entries. Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index). Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’. Well done, cadet. Now let’s look at some finer details about array methods. We’ve got to keep our paperwork straight, so you need to know when your actions change the original records.

Some methods---like splice and push---alter the original array, while others do not. Use the arrays

holdCabinet1 [‘duct tape’, ‘gum’, 3.14, false, 6.022e23] and

holdCabinet2 [‘orange drink’, ‘nerf toys’, ‘camera’, 42, ‘parsnip’] to explore the following methods: concat, slice, reverse, sort. Refer back to the documentation if you need to review the proper syntax for any of these methods.

Print the result of using concat on the two arrays. Does concat alter the original arrays? Verify this by printing holdCabinet1 after using the method. Print a slice of two elements from each array. Does slice alter the original arrays? reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays? Good progress, cadet. Here are two more methods for you to examine.

The split method converts a string into an array, while the join method does the opposite.

Try it! Given the string str = ‘In space, no one can hear you code.’, see what happens when you print str.split() vs. str.split(‘e’) vs. str.split(’ ‘) vs. str.split(“). What is the purpose of the parameter inside the ()? Given the array arr = [‘B’, ‘n’, ‘n’, 5], see what happens when you print arr.join() vs. arr.join(‘a’) vs. arr.join(’ ‘) vs. arr.join(“). What is the purpose of the parameter inside the ()? Do split or join change the original string/array? The benefit, cadet, is that we can take a string with delimiters (like commas) and convert it into a modifiable array. Try it! Alphabetize these hold contents: “water,space suits,food,plasma sword,batteries”, and then combine them into a new string. Nicely done, astronaut. Now it’s time to bring you fully up to speed.

Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.

Define and initialize the following arrays, which hold the name, chemical symbol and mass for different elements: element1 = [‘hydrogen’, ‘H’, 1.008] element2 = [‘helium’, ‘He’, 4.003] element26 = [‘iron’, ‘Fe’, 55.85] Define the array table, and use push(arrayName) to add each of the element arrays to it. Print table to see its structure. Use bracket notation to examine the difference between printing table[1] and table[1][1]. Don’t just nod your head! I want to HEAR you describe this difference. Go ahead, talk to your screen. Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26. table is an example of a 2-dimensional array. The first “level” contains the element arrays, and the second level holds the name/symbol/mass values. Experiment! Create a 3-dimensional array and print out one entry from each level in the array.

EXERCISE_5 For loop Construct for loops that accomplish the following tasks:

Print the numbers 0 - 20, one number per line. Print only the ODD values from 3 - 29, one number per line. Print the EVEN numbers 12 down to -14 in descending order, one number per line. Print the numbers 50 down to 20 in descending order, but only if the numbers are multiples of 3. Initialize two variables to hold the string ‘LaunchCode’ and the array [1, 5, ‘LC101’, ‘blue’, 42], then construct for loops to accomplish the following tasks:

Print each element of the array to a new line. Print each character of the string---in reverse order---to a new line. Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:

Define an empty evens array to hold the even numbers and an odds array for the odd numbers. In the loop, determine if each number is even or odd, then put that number into evens or odds, as appropriate. Print the arrays to confirm the results. Print evens first. Example: console.log(evens);

EXERCISE_6

While loop Define three variables for the JavaScript shuttle---one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.

Construct while loops to do the following:

Prompt the user to enter the starting fuel level. The loop should continue until the user enters a positive value greater than 5000 but less than 30000. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7. Use a final loop to monitor the fuel status and the altitude of the shuttle. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers. (Hint: The loop should end when there is not enough fuel to boost the crew another 50 km, so the fuel level might not reach 0). After the loops complete, output the result with the phrase, The shuttle gained an altitude of ___ km.

If the altitude is 2000 km or higher, add “Orbit achieved!”

Otherwise add, “Failed to reach orbit.”