Skip to content

Instantly share code, notes, and snippets.

@austinreuter
Created June 1, 2017 03:55
Show Gist options
  • Save austinreuter/e8656b1dd268aa84895bce5881f0540a to your computer and use it in GitHub Desktop.
Save austinreuter/e8656b1dd268aa84895bce5881f0540a to your computer and use it in GitHub Desktop.
self-assment1.js
function billTotal(subtotal) {
//input number
//output is number with tip and tax;
var tip = subtotal * 0.15;
var tax = subtotal * 0.095;
return subtotal + tip + tax;
}
function assert(actual, testName) {
if (!actual) {
console.log('FAILED ' + testName);
} else {
console.log('passed');
}
}
assert(billTotal(10) === (11.50 + 0.95), 'it calculates a bill total');
function range(start, end) {
//start and end === integers;
//return array: containing all (whole numbers) == don't have decimals
//assume that I have number;
var output = [];
start = Math.ceil(start);
if (start > end) {return 'error';}
for (var i = start; i < end; i++) {
output.push(i);
}
return output;
}
assert(JSON.stringify(range(3.3, 10)) === JSON.stringify([4, 5, 6, 7, 8, 9]), 'it creates a list of numbers from start until end');
//input of an object;
//output an object
//objective: create a function //create a new key on person.name property;
//Assume returning an altered version of the same object;
function getFullName(person) {
//access name property;
var name = person.name;
var first = name.first;
var middle = name.middle;
var last = name.last;
// return string of getFullName: 'first midI last'
//first + ' ' + midI + ' ' + last;
var fullName;
if (middle) {
fullName = [first, middle, last].join(' ');
}
if (!middle) {
fullName = [first, last].join(' ');
}
return fullName;
}
var person = {
name : {
first : "Alyssa",
middle: "P.",
last: "Hacker"
},
age : 26
}
var person1 = {
name: {
first: 'Aust',
last: 'Reut'
},
age: 26
}
var person2 = {
name: {
first: 'Austin',
Middle: undefined,
last: 'Reuter'
},
age: 26
}
assert(getFullName(person) === 'Alyssa P. Hacker', 'it created a full name string');
assert(getFullName(person1) === 'Aust Reut', 'it converted full name without middle property');
assert(getFullName(person2) === 'Austin Reuter', 'it converted full name with null middle');
//input in array of objects: Return person object with longest first + middle + last;
//to access what I want: f,m,l is under name property of person obj;
function longestName(people) {
var longest = '';
for (var i = 0; i < people.length; i++) {
var person = people[i];
var name = person.name;
var first = name.first;
var middle = name.middle;
var last = name.last;
var fullName = first + middle + last;
if (fullName.length > longest.length) {
longest = fullName;
var ret = person;
}
return ret;
}
var people = [
{name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26},
{name: {first: "Ben", last: "Bitdiddle"}, age: 34},
{name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40},
{name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45},
{name: {first: "Louis", last: "ReasonerReasonerReasonerReasoner"}, age: 21}
];
console.log(longestName(person))
//var expected = JSON.stringify(people[people.length - 1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment