Codecademy Javascript Course Answers

Spoiler Alert!

Codecademy Lesson 3 Exercise 3 Answer
function sad(value) {
// If a value is falsy, return the string ‘:(‘.
// Otherwise, don’t return anything.

if (!value) {
return (‘:(‘);
}
else {
return “”;
}
}
Codecademy Lesson 3 Exercise 5 Answer
// Here’s an example function that checks if a function
// is undefined or is the empty string.
function isUndefinedOrEmptyString(value) {
if (value === undefined) {
return true;
}
if (value === ”) {
return true;
}
// We only reach this point if the value is neither undefined
// nor ” since we returned true in those cases.
return false;
}

// This is what you should implement.
// Return true if the value is null or undefined.
// Return false otherwise.
function isUnset(value) {
if(value === null || value === undefined) {
return true;
} else {
return false;
}
}
Codecademy Lesson 3 Exercise 6 Answer
// Here’s a function that checks if a function is undefined.
function isUndefinedOrEmptyString(value) {
// Take a minute to make sure this logic makes sense.
return value === undefined || value === ”;
}

// This is what you should implement.
// Return true if the value is null or undefined.
// Return false otherwise.
function isUnset(value) {
return value === undefined || value === null;

}
Codecademy Lesson 4 Exercise 1 Answer
// Check if a variable is unset. This time, use else if.
// As before, return true if it unset and false if it is not
// unset. Recall that an “unset” value is undefined or null.
function isUnset(value) {
// Change the empty conditions and blocks.
if ( value === undefined) {
// block
return true;

} else if (value === null) {
// block
return true;
}
else {
return false;
}

}
Codecademy Lesson 4 Exercise 2 Answer
function UnconventionalDefaults(params, defaultParams) {
if (params === undefined) {
params = defaultParams;
}

// function would do work here

return params;
}

// Modify this function to set params to defaultParams if params
// is falsy
function moreConventionalDefaults(params, defaultParams) {
// do a more conventional check here (check if params is falsy,
// and not just undefined

if (params === null || params === undefined)

params = defaultParams;

return params;
}
Codecademy Lesson 4 Exercise 4 Answer
// This should be similar to the function you implemented
// earlier.
function moreConventionalDefaults(param, defaultParams) {
if (!params) {
params = defaultParams;
}
return params;
}

function conventionalDefaults(params, defaultParams) {
// Use || to implement a better default mechanism.

if (params === null || params === undefined) {
params = defaultParams;
}

return params;
}
Codecademy Lesson 4 Exercise 5 Answer
// This function should increment the start value by 1
// the number of times specified.
function increment(start, timesToIncrement) {
// Add the appropriate code here. You must update
// the condition in the while loop. You may want to
// create a new variable to keep track of how many
// times you have incremeneted the variable.

while(start <= timesToIncrement) {
start++;
}
return start;
}
Codecademy Lesson 4 Exercise 6 Answer
// This function should increment the start value by 1
// the number of times specified.
function increment(start, timesToIncrement) {
// Add the appropriate code here, this time using a
// for loop. This time, you must also write in the loop
// body.
for(start = 0; start < timesToIncrement; start++) {
start++;
}
return start;
}
Codecademy Lesson 4 Exercise 7 Answer
// This function should increment the start value by 1
// the number of times specified.
function increment(start, timesToIncrement) {
// Add the appropriate code here, this time using a
// do/while loop. This time, you must also write in the loop
// body.
do {

start++;

} while(start <= timesToIncrement);

return start;
}
Codecademy Lesson 4 Exercise 8 Answer
// Example switch statement. typeof returns the type
// of the value as a string. For example, the type of
// a string is ‘string’, the type of a number is
// ‘number’, and the type of an object is ‘object’.
switch (typeof ‘foo’) {
case ‘string':
// some code

// If you don’t return in the block, you should
// always have a break afterwards.
break;
default:
//block
}

// Write a function that uses switch statements on the
// type of value. If it is a string, return ‘str’. If it
// is a number, return ‘num’. If it is an object, return
// ‘obj’. If it is anything else, return ‘other’.
function detectType(value) {

switch (typeof value) {
case “string”:
return “str”;
break;

case ‘number':
return “num”;
break;

case ‘object':
return “obj”;
break;

default:
return ‘other';
break;
}

}
Codecademy Lesson 5 Exercise 3 Answer
// This function tries to set foo to be the
// value specified.
var foo;
function setFoo(val) {
// foo is declared in a function. It is not
// accessible outside of the function.
foo = val;
}

setFoo(10);

// Now that we are outside the function, foo is
// not defined and the program will crash! Fix this
// by moving the declaration of foo outside of the
// function. Make sure that setFoo will still update
// the value of foo.
alert(foo);
Codecademy Lesson 5 Exercise 4 Answer
// If value is falsy and not a string, return
// undefined. Otherwise, return ‘good input!’
// Note that this means that the empty string (”)
// should return ‘good input!’.
function detectGoodInput(value) {
if (!value && value !== “”) {
return undefined;
}
else return ‘good input!';
}
Codecademy Lesson 6 Exercise 3 Answer
// You can check the length of a string literal.
‘foo’.length; // 3.

// Or of a variable
var bar = ‘a string';
bar.length; // 8

var baz = ‘baarbarbar!';
var bazLength = baz.length;

// Now, try storing the length of baz in bazLength.

 

Codecademy Lesson 6 Exercise 4 Answer

// Here’s replace with a string. It only replaces
// the first instance of foo, and returns ‘barfoo’.
‘foofoo’.replace(‘foo’, ‘bar’);

var str = ‘Hello, world';

// Now, use replace() to update the variable str
// by replacing ‘world’ with ‘Ryan’.

str = str.replace(‘world’,’Ryan’);
Codecademy Lesson 6 Exercise 5 Answer
// Sample function that returns true if the substr is
// entirely contained in the second half of str and
// false if it is not in the string or starts in the
// first half of the string.
//
// So, inSecondHalf(‘foobar’, ‘bar’) returns true, but
// inSecondHalf(‘foobar’, ‘foo’) returns false.
function inSecondHalf(str, substr) {
var halfwayPoint = str.length / 2;
if (str.indexOf(substr) < halfwayPoint) {
return false;
} else {
return true;
}
}

// Modify the following function according to the
// comments inside.

function contains(str, substr) {
// Return true if substr is a substring of str
// and false if substr is not a substring of str

if (str.indexOf(substr) === -1) {
return false;
} else {
return true;
}
}
Codecademy Lesson 7 Exercise 3 Answer
var first = 0.1;
var second = 0.2;
var sum = (first + second) * 100;
sum = Math.round (sum) / 100;

// A common way to deal with the problem is to add
// the operands, “scale” the result by multiplying
// by 100, calling Math.round on the result, and
// then dividing by 100. digits truncated. Try doing
// this now. Store the result in sum.
Codecademy Lesson 8 Exercise 1 Answer
// This syntax is called object literal notation.
var person = {
// Note that there are no quotes around ‘name’. This
// is because words to the left of the colon are
// treated as string literals since object keys must
// be a string. You could add quotes if you want,
// but they are normally omitted.
name: ‘Ryan’, // note the comma
weight: 150 // no comma after the last key-value pair
}; // Don’t forget the semicolon.

// Make a variable called otherPerson with the name
// ‘Zach’ and a weight of 800.

var otherPerson = {
name: ‘Zach’,
weight: 800
};
Codecademy Lesson 8 Exercise 2 Answer
// This uses the object literal syntax we learned before
// to create an empty object.
var literal = {};

var constructed = new Object();

// Make a new object called otherConstructed using the
// ‘new’ keyword.

var otherConstructed = new Object();
Codecademy Lesson 8 Exercise 3 Answer
// Initialize an empty object as before.
var person = {};

// Option one: dot notation. ‘name’ is treated as
// a string literal.
person.name = ‘Ryan';

// Option two: bracket notation. Whatever is in brackets
// is treated as a variable and is coerced into string.
// So, weight must be in quotes.
person[‘weight’] = 350;

// Add an ‘age’ property with the value 30 to the person
// using dot notation.
person.age = 30;
// Add an ‘height’ property with the value 70 to the person
// using bracket notation.
person[‘height’] = 70;
Codecademy Lesson 8 Exercise 4 Answer
// Create person with the object literal
// syntax this time.
var person = {
name: ‘Ryan’,
weight: 350,
age: 30,
height: 70
};

// The syntax for accessing properties is the same as
// the syntax for setting them.
var retrievedName = person.name;
var retrievedWeight = person[‘weight’];

var retrievedAge = person.age;
var retrievedHeight = person[‘height’];

// Set retrievedAge using dot notation.

// Set retrievedHeight using bracket notation.
Codecademy Lesson 8 Exercise 5 Answer
// Initialize the same object as before.
var person = {
name: ‘Ryan’,
weight: 350,
age: 30,
height: 70
};

// Both examples below have a typo.

// Trying to access a property of an object that doesn’t
// exist returns undefined (it does not throw an error).
// So, undefinedProperty is equal to the primitive
// undefined.
var retrievedHeight = person.height;

// But this code throws an exception.
var anotherPerson = person;

// Fix the typos above so that the code works as expected.
Codecademy Lesson 8 Exercise 6 Answer
// trimName accepts a person as an argument. Person
// will always be an object.
// Return undefined if name is not defined. Otherwise,
// return a trimmed name.
function trimName(person) {
// If you do not set this variable to a value, it will be
// undefined.
var trimmedName;
// Do a check here to see if a person’s name is defined.
if (person.name !== undefined) {
trimmedName = person.name.trim();
}

return trimmedName;
}
Codecademy Lesson 8 Exercise 7 Answer
// Recall that && evaluates operands from left to right. If it
// encounters a false operand, it does not continue evaluating
// operands. Thus, foo && foo.bar will not access foo.bar if foo
// is undefined.
function exampleFunction(foo) {
if (foo && foo.bar) {
// Do things here.
}
}

// Implement the function as before.
//
// But now it’s possible that person is undefined.
// You must verify that person is not undefined and
// that person.name is undefined.
function trimName(person) {
var trimmedName;
// Add a condition here.
if (person&&person.name) {
trimmedName = person.name.trim();
}
return trimmedName;
}
Codecademy Lesson 8 Exercise 8 Answer
// We’re trying to use WEIGHT_KEY to avoid having to
// remember that the key that stores the weight information
// is called ‘weight’.
var WEIGHT_KEY = ‘weight';

var person = {
name: ‘Ryan’,
// But this sets person[‘WEIGHT_KEY’] as 350.
weight: 350
};

var weight = person[‘weight’];

Follow me

Kewyn

Director at Apptomization
Kewyn is the Founder of Apptomization. He is passionate about problem solving and helping startups and small businesses thrive. He is also an Infusionsoft Certified Consultant. Kewyn currently resides in California
Follow me

Latest posts by Kewyn (see all)

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>