Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Last active October 19, 2020 21:36
Show Gist options
  • Save mmeigooni/3398ad73a3d1a35d3196d36d42940b9c to your computer and use it in GitHub Desktop.
Save mmeigooni/3398ad73a3d1a35d3196d36d42940b9c to your computer and use it in GitHub Desktop.

Exercises

  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!

  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.

  3. 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}

    HINT: It might be helpful to have a fullName function that, when given a person as a parameter, returns a person's full name.

@san-sekhon
Copy link

  1. function billTotal(preTaxandTipAmount) {
    var tax = preTaxAndTipAmount * 0.095;
    var tip = preTaxAndTipAmount * 0.15;
    return preTaxAndTipAmount + tax + tip;

}

  1. function range(start, end) {
    var list = [];
    if(start === end || start < end) {
    return list;
    }
    for (var i = start; i < end; i++) {
    list.push(i);
    }
    3.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}

@milanraai
Copy link

# Solution 1
function billTotal(total) {
var tipAmt = total * 0.15;
var taxAmt = total * 0.095;
return total + tipAmt + taxAmt;
}

# Solution 2
function range(start, end) {
var resultArr = [];
for(i = start; i< end; i++) {
resultArr.push(i);
}
return resultArr;

# Solution 3

function longestName(people) {
var list = people[0];

}
longestName(people);

Solution 3

function longestName(people) {

for (x in people) {

// console.log(people[x])

for(y in people[x]) {

 var listName = people[x].name;
 
 for (z in listName) {
   
   var fullName = listName.first + listName.middle + listName.last;
   
 }

}
}
}

@kiwiseven
Copy link

function range(start, end){
var rangeArr = [];
for(var i = start; i < end; i++){
rangeArr.push(i);
}
return rangeArr;
}

range(1, 4);

function billTotal(costOfGoods){
var tax = 0.095;
var tip = 0.15;
return (costOfGoods * tax) + (costOfGoods * tip) + costOfGoods;
}

billTotal(20);

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) {
var nameArr =
people.map(function(person){
if(!person[name].hasOwnProperty(middle)){
var name = person.name.first + person.name.last;
console.log(name);
}
});
return nameArr;
}

longestName(people);

@lunareve
Copy link

// 1
function billTotal(subtotal) {
var tip = subtotal * 0.15;
var tax = subtotal * 0.095;
return subtotal + tip + tax;
}

//2
function range(start, end) {
var array = [];
var n = end - start;
if (n > 0) {
for (i=0, i < n, i++) {
array[i] = start + i;
}
}
return array;
}

//3
function longestName(people) {
for (let i of people) {
var objName = people[i];
for ( var j in objName) {
var lenFullName = 0;
var partName = objName[j];
lenFullName += partName.length;
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment