Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JavaScript coding challenge 1
- /* BMI: mass / height^2 = mass / (height * height)
- 1.store mark and john's mass and height in variables.
- 2. calculate both their bmis.
- 3. create a boolean variable containing information about whether Mark has a higher BMI than John
- 4. Print a string to the console containing the variable from Step 3. (Something like "Is Mark's BMI higher than
- John's? true").
- */
- var markMass; var markHeight; var johnMass; var johnHeight; var markBMI; var johnBMI;
- markMass = prompt('What is Mark\'s Mass?' );
- markHeight = prompt('What is Mark\'s Height?' );
- johnMass = prompt('What is John\'s Mass?' );
- johnHeight = prompt('What is John\'s Height?' );
- markBMI = markMass / (markHeight * markHeight);
- johnBMI = johnMass / (johnHeight * johnHeight);
- var markBiggerBMI = markBMI > johnBMI;
- console.log(markBMI, johnBMI);
- if(markBMI > johnBMI){
- console.log('Mark\'s BMI is bigger than John\'s');
- }
- else {
- console.log('John\'s BMI is bigger than Mark\'s');
- }
- //CODING CHALLENGE #2
- /*
- John and Mike both play basketball in different teams. In the latest 3 games, John's team scored 89,
- 120 and 103 points, while Mike's team scored 116, 94, and 123 points.
- 1. Calculate the average score for each team.
- 2. Decide which teams wins in average (highest average score), and print the winner to the console.
- Also include the average score in the output.
- 3. Then change the scores to show different winners.
- Don't forget to take into acount there might be a draw (the same average score).
- 4. EXTRA: Mary also plays basketball, and her team scored 97, 134, and 105 points. Like before, log the
- average winner to the console. HINT: you will need the && operator to take the decision.
- 5. Like before, change the scores to generate different winners, keeping in mind there might be draws.
- */
- var teamJohnAvg = (89 + 102 + 103) / 3;
- var teamMikeAvg = (116 + 94 + 123) / 3;
- var teamMaryAvg = (97 + 134 + 105) / 3;
- console.log('John\'s team Average: ' + teamJohnAvg + ' \n' +
- 'Mike\'s team Average: ' + teamMikeAvg + ' \n' +
- 'Mary\'s team Average: ' + teamMaryAvg
- );
- if (teamJohnAvg > teamMikeAvg && teamJohnAvg > teamMaryAvg) { console.log('John\'s team is winning'); }
- else if (teamMikeAvg > teamJohnAvg && teamMikeAvg > teamMaryAvg) { console.log('Mike\'s team is winning'); }
- else if ( teamMaryAvg > teamJohnAvg && teamMaryAvg > teamMikeAvg) { console.log('Mary\'s team is winning'); }
- else if (teamJohnAvg === teamMikeAvg && teamJohnAvg > teamMaryAvg) { console.log('John\'s team and Mike\'s team are tied for winning'); }
- else if (teamJohnAvg === teamMaryAvg && teamJohnAvg > teamMikeAvg ) { console.log('John\'s team and Mary\'s team are tied for winning'); }
- else if (teamMikeAvg === teamMaryAvg && teamMaryAvg > teamJohnAvg ) { console.log('Mike\'s team and Mary\'s team are tied for winning'); }
- else { console.log('They are all tied for winning \n\n'); }
- console.log('John\'s Team Score: ' + teamJohnAvg + '\n' +
- 'Mike\'s Team Score: ' + teamMikeAvg + '\n' +
- 'Mary\'s Team Score: ' + teamMaryAvg
- );
- /*
- CODING CHALLENGE 3
- John and his family went on a holiday and went to 3 different restaurants. The bills were $124, $48, and $268.
- To tip the waiter a fair amount, John created a simple tip calculator (as a function). He likes to tip 20% of
- the bill when the bill is less than $50, 15% when the bill is between 50-200 dollars, and 10% if more than 200 dollars.
- In the end, John would like to have 2 arrays:
- 1. Containing all three tips (one for each bill)
- 2. Containing all three final paid amounts (bill + tip).
- (NOTE: To calculate 20% of a value, simply multiply if with 20/100 = 0.2)
- */
- var billsBeforeTip = [124, 48, 268]; var tipAmounts = []; var finalAmounts = [];
- function tipCalculator(billAmt)
- {
- var percentage;
- if ( billAmt <= 50) {
- percentage = .2; }
- else if (50 < billAmt <= 200) {
- percentage = .15;
- }
- else {
- percentage = .1;
- }
- return percentage * billAmt
- }
- for (i = 0; i< billsBeforeTip.length; i++)
- {
- tipAmounts[i] = tipCalculator(billsBeforeTip[i]);
- finalAmounts[i] = billsBeforeTip[i] + tipAmounts[i];
- console.log([i] + '. total tip: ' + tipAmounts[i]);
- }
- console.log('\n\nBills before tips: ' + billsBeforeTip.toString());
- console.log('Final Amounts: ' + finalAmounts.toString());
- /*
- Coding Challenge #4
- remember Mark and John BMI Challenge. Implement the same functionality with objects and methods.
- 1. For each of them, create an object with properties for their full name, mass, and Height
- 2. Then, add a method to each object to calculate the BMI. Save the BMI to the object and also return
- it from the method.
- 3. In the end, log to the console who has the highest BMI, together with the full name and the respective BMI.
- Don't forget they might have the same BMI.
- Remember: BMI = mass / heightˆ2 = mass / (height * height). (mass in kg and height in meter).
- */
- var mark = {
- name: 'Mark Cage', // key: value
- mass: '78',
- height: '1.75',
- calcBMI: function(){
- this.bmi = this.mass / Math.pow(this.height,2);
- }
- }
- mark.calcBMI();
- console.log(mark);
- var john = {
- name: 'John Smith', // key: value
- mass: '82',
- height: '1.65',
- calcBMI: function(){
- this.bmi = this.mass / Math.pow(this.height,2);
- }
- }
- john.calcBMI();
- console.log(john);
- if(mark.bmi > john.bmi){
- console.log(mark.name + ' has a higher BMI');
- }
- else if (mark.bmi < john.bmi){
- console.log(john.name + ' has a higher BMI');
- } else {
- console.log(mark.name + ' and ' + john.name + ' has the same BMI');
- }
- //coding Challenge #5
- /*
- tip tipCalculator V2.0
- John and his family went to 5 different restaurants.
- The bills were $124, $48, $268, $180, and $42.
- John likes to tip 20% of the bill when the bill is less than $50, 15% when the bill is between $50, and $200, and 10% if the bill is more than $200.
- Implement a tip calculator using objects and loops:
- 1. Create an object with an array for the bill values
- 2. add a method to calculate the tips.
- 3. this method should include a loop to iterate over all the paid bills and do the tip calculations
- 4. As an output, create (1) a new array containing all tips, and (2) an array containing final paid amounts (bill + tip). HINT: start with 2 empty arrays as PROPERTIESand then fill them up in the loop.
- */
- var john = {
- fullName: 'John Smith',
- bills: [128, 48, 268, 180, 42],
- calcTips: function(){
- this.tipAmounts = []; this.finalAmounts = [];
- for (var i = 0; i < this.bills.length; i++) {
- var percentage;
- var bill = this.bills[i];
- if (bill < 50) {
- percentage = .2;
- }
- else if (50 <= bill <= 200 ) {
- percentage = .15;
- }
- else {
- percentage = .1;
- }
- this.tipAmounts[i] = bill * percentage;
- this.finalAmounts[i] = bill + (bill * percentage);
- }
- }
- }
- john.calcTips();
- console.log(john);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement