Created
June 1, 2017 03:54
-
-
Save boisbb18/291ffdca80ada9c29cc2c643c67f2965 to your computer and use it in GitHub Desktop.
Boiskhon Bakhodirov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// javascript | |
/*1 Write a function called billTotal that can be used to calculate the total to be paid at a restaurant -- | |
including tip and tax -- given the subtotal (i.e. cost of food and drinks). | |
We can assume that the tip will be 15% and tax will be 9.5%. Make sure that the tip does not include the tax!*? | |
*/ | |
function getTotal(price){ | |
var postTip = (price*0.15)+price; | |
var postTax = (postTip *0.095); | |
return postTip+postTax; | |
} | |
console.log("program 1 -> ",getTotal(20)); | |
/*2 Complete the below function called range that takes two integers as parameters, | |
start and end, and returns an array containing all the whole numbers between them starting | |
with start and up to end (you can use a any loop. The function definition should look like this: | |
function range(start, end) { | |
// YOUR CODE HERE | |
} | |
You should be able to use it like so: | |
range(0, 4); // => [0, 1, 2, 3] | |
range(2, 7); // => [2, 3, 4, 5, 6] | |
range(10, 10); // => [] | |
range(10, 2); // => [] | |
After you write your function, you can test it using the above inputs to make sure that it behaves correctly. | |
*/ | |
function range(start,end){ | |
var arr= []; | |
for(var i =start;i<end;i++){ | |
arr.push(i); | |
} | |
return arr; | |
} | |
console.log("Problem 2 ",range(2,7)); | |
/*3 Write a function called 'getFullName' that takes in an object which has this structure: | |
var person = { | |
name : { | |
first : "Alyssa", | |
middle: "P.", | |
last: "Hacker" | |
}, | |
age : 26 | |
} | |
and returns the person's full name. | |
`getFullName(person); //"Alyssa P. Hacker"` | |
Note: some person objects passed in may be missing a middle name. In such cases, | |
the function should return the first and last name separated by 1 space. | |
Example: | |
var personB = { | |
name: { | |
first: "Ben", | |
last: "Bitdiddle" | |
}, | |
age: 34 | |
} | |
getFullName(person); //"Ben Bitdiddle"*/ | |
var person = { | |
name : { | |
first : "Alyssa", | |
middle: "P.", | |
last: "Hacker" | |
}, | |
age : 26 | |
} | |
var personB = { | |
name: { | |
first: "Ben", | |
last: "Bitdiddle" | |
}, | |
age: 34 | |
} | |
console.log("Problem 3: ",getFullName(person)); | |
console.log("Problem 3: ",getFullName(personB)); | |
function getFullName(obj){ | |
return Object.values(obj.name).join(' '); | |
} | |
/*4. Given the following array of people, write a function that, when passed people as a parameter | |
, returns the person (that is, your function should return an object) with the longest name (first, middle & last). | |
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: "Reasoner"}, age: 21} | |
]; | |
function longestName(people) { | |
// TODO: Your code here | |
} | |
longestName(people); | |
// => {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26} | |
*/ | |
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: "Reasoner"}, age: 21} | |
]; | |
function longestName(array){ | |
return array.reduce(function(acc,el){ | |
var temp1=0; | |
if(Object.keys(acc)!=false){ | |
temp1 =Object.values(acc.name).join(' ').length; | |
} | |
var temp2 =Object.values(el.name).join(' ').length; | |
if(temp2>temp1){ | |
acc=el; | |
} | |
return acc; | |
},{}); | |
} | |
console.log("problem 4: ",longestName(people)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment