Skip to content

Instantly share code, notes, and snippets.

@robertbenjamin
Created July 1, 2015 20:25
Show Gist options
  • Save robertbenjamin/1c6529add1dc797df743 to your computer and use it in GitHub Desktop.
Save robertbenjamin/1c6529add1dc797df743 to your computer and use it in GitHub Desktop.
JS Basic Homework
// What is the return value of the below code sample? Provide a sentence or two of explanation.
typeof( 15 );
// It'll return "number", because it's a number not in quotes.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
typeof( "hello" );
// It'll return "string", because it's text in a set of parentheses.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
typeof( [ "dog", "cat", "horse" ] );
// It'll return "object", because in Javascript an array is just a form of an object.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
typeof( NaN );
// It'll return "number", which is a quirk of Javascript. It *is* a numerical type, so it does make *some* sense.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
"hamburger" + "s";
// It'll return "hamburgers", which is a result of string concatenation.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
"hamburgers" - "s";
// It'll return "NaN", since it's impossible to subtract two non-numerical things.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
"johnny" + 5;
// It'll return "johnny5", which is a result of Javascript's type coercion.
// What is the return value of the below code sample? Provide a sentence or two of explanation.
99 * "luftbaloons";
// It'll return "NaN", since it's impossible to multiple a number by a string.
// What will the contents of the below array be after the below code sample is executed.
var numbers = [ 2, 4, 6, 8 ];
numbers.pop();
numbers.push( 10 );
numbers.unshift( 3 );
// numbers = [ 3, 2, 4, 6, 10 ]
// What is the return value of the below code sample?
var morse = [ "dot", "pause", "dot" ];
var moreMorse = morse.join( " dash " ); // " dash " is acting as the separator
moreMorse.split( " " ); // Splits the string into separate instances again, with " " as the separator
// [ 'dot', 'dash', 'pause', 'dash', 'dot' ]
// What will the contents of the below array be after the below code sample is executed.
var bands = [];
var beatles = [ "Paul", "John", "George", "Pete" ];
var stones = [ "Brian", "Mick", "Keith", "Ronnie", "Charlie" ];
bands.push( beatles ); // Add beatles to bands
bands.unshift( stones ); // Add stones before the beatles
bands[ bands.length - 1 ].pop(); // Remove last item in the 2nd array
bands[0].shift(); // Remove first item in the first array
bands[1][3] = "Ringo"; // Add "Ringo" to given position
// [ [ 'Mick', 'Keith', 'Ronnie', 'Charlie' ],
// [ 'Paul', 'John', 'George', 'Ringo' ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment