Practice Exercises
Try to solve each of the following using JavaScript. If you need to print something,
use console.log(), which will print the argument(s) you give it.
-
Create a variable
labeland assign it the value"senecacollege". Create another variabletldand assign it"ca". Create a third variabledomainNamethat combineslabelandtldto produce the value"senecacollege.ca". -
Create a variable
isSenecaand assign it a boolean value (trueorfalse) depending on whether or notdomainNameis equal to"senecacollege.ca". HINT: use===and don't writetrueorfalsedirectly. -
Create a variable
isNotSenecaand assign it the inverse boolean value ofisSeneca. HINT: ifisSenecaistrue,isNotSenecashould befalse. -
Create four variables
byte1,byte2,byte3,byte4, and assign each of these a value in the range0-255. -
Convert
byte1to aStringusing.toString(), andconsole.log()the result. What happens if you usetoString(2)ortoString(16)instead? -
Create a variable
ipAddressand assign it the value of combining your fourbyteNvariables together, separated by".". For example:"192.168.2.1". -
Create a variable
ipIntand assign it the integer value of bit-shifting (<<) and adding yourbyteNvariables. HINT: youripIntwill contain 32 bits, the first byte needs to be shifted 24 bit positions (<< 24) so it occupies 32-25, the second shifted 16, the third 8. -
Create a variable
ipBinarythat contains the binary representation of theipIntvalue. HINT: use.toString(2)to display the number with1and0only. -
Create a variable
statusCode, and assign it the value for the "I'm a teapot" HTTP status code. HINT: see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status -
Write an
Ifstatement that checks to see if yourstatusCodeis a4xxclient error. HINT: use the<,>,>=, and/or<=operators to test the value -
Write a
switchstatement that checks yourstatusCodefor all possible1xxinformation responses. In each case, you shouldconsole.log()the response text associated with the status code, or"unknown information response"if the status code is not known. -
Write a function
is2xx(status)which takes a status codestatus(e.g.,200) and returnstrueif the status code is a valid 2xx code. -
Create a variable
studentNameand assign your name. Create another variablestudentAgeand assign it your age. Useconsole.log()to print out a sentence that includes both variables, like"Alice is 20 years old.". -
Create a variable
isEvenand assign it a boolean value (trueorfalse) depending on whether a given numbernumis even or not. HINT: use the modulus operator%. -
Create a variable
isOddand assign it the inverse boolean value ofisEven. HINT: ifisEvenistrue,isOddshould befalse. -
Create a variable
radiusand assign it a value of10. Calculate the area of a circle with this radius and assign the result to a variablearea. HINT: useMath.PIand the formulaarea = πr^2. -
Create a variable
temperatureInCelsiusand assign it a value. Convert this temperature to Fahrenheit and assign the result to a variabletemperatureInFahrenheit. HINT: use the formulaF = C * 9/5 + 32. -
Create a variable
heightInFeetand assign it a value. Convert this height to meters and assign the result to a variableheightInMeters. HINT: use the conversion factor1 foot = 0.3048 meters. -
Create a variable
secondsand assign it a value. Convert this time to minutes and seconds (e.g., 90 seconds becomes 1 minute and 30 seconds) and assign the result to two variablesminutesandremainingSeconds. -
Create a variable
scoreand assign it a value. Write anifstatement that checks if the score is an A (90-100), B (80-89), C (70-79), D (60-69), or F (below 60) and assigns the result to a variablegrade. -
Write a
switchstatement that checks the value of a variabledayandconsole.log()s whether it is a weekday or weekend. HINT:daycan be a value from 1 (Monday) to 7 (Sunday). -
Write a function
isPositive(num)which takes a numbernumand returnstrueif the number is positive andfalseotherwise. -
Write a function
isLeapYear(year)which takes a yearyearand returnstrueif the year is a leap year andfalseotherwise. HINT: a leap year is divisible by 4, but not by 100, unless it is also divisible by 400. -
Write a function
getDayOfWeek(day)which takes a numberday(from 1 to 7) and returns the day of the week as a string (e.g., "Monday"). -
Write a function
getFullName(firstName, lastName)which takes two stringsfirstNameandlastNameand returns the full name as a single string. -
Write a function
getCircleArea(radius)which takes a numberradiusand returns the area of a circle with that radius. -
Write a function
getHypotenuse(a, b)which takes two numbersaandb(the lengths of the two sides of a right triangle) and returns the length of the hypotenuse. HINT: use the Pythagorean theorem andMath.sqrt()to calculate the square root.
After you try writing these yourself, take a look at a possible solution.