Skip to content

Instantly share code, notes, and snippets.

@freejoe76
Last active November 21, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freejoe76/4d7f90dc6b07acaa0110f536ca641f88 to your computer and use it in GitHub Desktop.
Save freejoe76/4d7f90dc6b07acaa0110f536ca641f88 to your computer and use it in GitHub Desktop.
One-line javascript exercises

One-line javascript exercises

Each of these can be accomplished with one line of javascript.

  1. Create a variable and assign it the value explore (a string).
  2. Create a variable that's an array of two values (strings), red and blue.
  3. Create a variable that evaluates to the boolean true. Create another var, set it to 1. Compare the two (with javascript) and tell me whether javascript thinks they equal each other.
  4. Write a function named my_first_function that logs to console the word "hey". Name the function my_first_function. That line will start with function my_first....
  5. Edit that function so instead of logging to console, it "returns" the value "hey" ("return" is the name of the command you can use in a function that sends information back to whatever called the function). Call (run) the function and make sure it does what you think it does.
  6. Take your function from before and assign it to a variable named hey_maker. That line will start with var hey_maker.... Call it from the javascript console.
  7. Write a function that, depending on what value is passed the function, returns whatever's passed the function and appends the word "hey" to the end of it.
  8. Create a variable and assign it a value of 17 (the integer, not the string). Divide it by 5 and then round the result to the nearest whole number.
  9. This is a three-parter:
    1. Create a variable that's an object and give it a key of color, and give that key a value of grey (a string).
    2. Add another key to the object, call the key 'size' and assign the key an empty array.
    3. Write a line of js that returns all the keys of that object.
  10. Create a variable and give it a value of a function that returns the value 193 (an integer), then run that function.
  11. Create a variable and set it to true (a boolean). Write an if statement that tests if that variable is true, and if it is, write to the console log the value of that variable.
  12. Create a variable, assign it the integer 3, and write a while loop that increases the value of that variable by 1 until that var equals 10.
  13. Write a for loop that loops five times, logging to console the variable you use to count each time.
  14. Write a for loop that loops 10 times, logging to console the counter variable only when the counter variable is divisible by 3.
  15. Create an array with three items, then create another variable that gets its value from whatever's the last element on that array you created.
  16. Create an array, and then reverse its contents.
  17. Create a variable with that's a string that's assigned a value of this sentence itself. Create another variable that's assembled programmatically and is an array made up of each of the words of that sentence.
  18. Using the array you created in the previous quiz, test to see if the word "assigned" is in that array, and if it is, log all the words after the word "this" to console.
  19. Write a line of javascript that when executed in the javascript console, gives the entire page you're on a 50px black border.
  20. Write some javascript that turns the text color of every <p> on the page you're on red.
  21. Find three functions available to the document object and report back with the function names & what each does.
  22. Turn this string into a javascript Date object: 'June 1 2017 00:00:00 GMT-06:00'
  23. Calculate the number of seconds since the June 1, 2017.
  24. Write a function that logs the time to the console every five seconds.
  25. Write a function that counts down from 10 to 0, once per second, logging each new number to console. When you get to zero, send the console an error message instead of a log message.
  26. Write a function that counts down from 10 to 0, once per second, appending each new number to an array that you have created. When you get to zero log the array to console.
  27. Given this array, var words = ['hi', 'this', 'is', 'an', 'array', 'of', 'some', 'words'];, loop through it and add any words that have the letter "h" in them to another, separate array. Log that array to the console when you're done.
  28. Write javascript that turns this string into a JSON object: ``. . Write some javascript that appends this script to the head element of the page you're on: http://interactive.nydailynews.com/js/jquery.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment