Last active
September 3, 2021 03:43
-
-
Save thiagodebastos/4b4041762bd2cb9cc244c5993d2934a5 to your computer and use it in GitHub Desktop.
Code session with Fhelipe
This file contains 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
var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; | |
var inputCalories = [3500, 1500, 1800, 2300, 2400, 1500, 1500]; | |
// const caloriesByDay = daysOfWeek.map(function(day, index) { | |
// return { | |
// [day]:inputCalories[index] | |
// } | |
// }) | |
const caloriesByDay = daysOfWeek.map((day, i) => ({[day]: inputCalories[i]})) | |
console.log(caloriesByDay) | |
function calculateHealthPlan(caloriesByDay) { | |
var desiredCaloriePerDay = 2200; | |
var healthyDays = [] | |
var unhealthyDays = [] | |
for(var index = 0; index < inputCalories.length; index++) { | |
// shorter version | |
// const isHealthy = inputCalories[index] <= 2000 : true : false | |
// if(isHealthy) | |
if(inputCalories[index] <= 2200) { | |
// add to healthyDays | |
healthyDays.push( | |
{ | |
[daysOfWeek[index]]: inputCalories[index] | |
} | |
) | |
// healthyDays = [...healthyDays, inputCalories[index]] | |
} | |
if(inputCalories[index] > 2200) { | |
// add to unhealthyDays | |
unhealthyDays.push( | |
{ | |
[daysOfWeek[index]]: inputCalories[index] | |
} | |
) | |
} | |
} | |
return { | |
healthyDays: healthyDays, | |
unhealthyDays: unhealthyDays | |
} | |
} |
This file contains 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
var outComeName = ['fail','pass', 'credit', 'distinction', 'high distinction']; | |
var testScores = [60, 55, 80, 44, 56, 65, 34,65, 75, 56, 75, 78, 97, 87, 46, 75, 57, 85, 43, 67]; | |
var scoresWithName = [ | |
{ | |
"name": "Arney", | |
"score": 95 | |
}, { | |
"name": "Ignace", | |
"score": 8 | |
}, { | |
"name": "Yorker", | |
"score": 90 | |
}, { | |
"name": "Waverly", | |
"score": 21 | |
}, { | |
"name": "Meggy", | |
"score": 15 | |
}, { | |
"name": "Angel", | |
"score": 17 | |
}, { | |
"name": "Gennifer", | |
"score": 87 | |
}, { | |
"name": "Maxi", | |
"score": 25 | |
}, { | |
"name": "Shawnee", | |
"score": 66 | |
}, { | |
"name": "Berne", | |
"score": 10 | |
}, { | |
"name": "Devonne", | |
"score": 9 | |
}, { | |
"name": "Devondra", | |
"score": 37 | |
}, { | |
"name": "Ilene", | |
"score": 4 | |
}, { | |
"name": "Abdel", | |
"score": 35 | |
}, { | |
"name": "Malachi", | |
"score": 11 | |
}, { | |
"name": "Jackie", | |
"score": 35 | |
}, { | |
"name": "Rozelle", | |
"score": 98 | |
}, { | |
"name": "Addy", | |
"score": 58 | |
}, { | |
"name": "Edwin", | |
"score": 95 | |
}, { | |
"name": "Lethia", | |
"score": 57 | |
}] | |
function getTestResultsOLD(testResultByGrade){ | |
var result = { | |
fail: [], | |
pass: [], | |
credit: [], | |
distinction: [], | |
highDistinction: [], | |
} | |
for(var index = 0; index < testResultByGrade.length ; index++){ | |
if (testScores[index]<= 49) { | |
result.fail = [...result.fail, testResultByGrade[index]].sort() | |
} | |
if((testResultByGrade[index]>=50) && (testResultByGrade[index]<=64)) { | |
result.pass = [...result.pass, testResultByGrade[index]].sort() | |
} | |
if((testResultByGrade[index]>=65) && (testResultByGrade[index]<=74)) { | |
result.credit = [...result.credit, testResultByGrade[index]].sort() | |
} | |
if((testResultByGrade[index]>=75) && (testResultByGrade[index]<=84)) { | |
result.distinction = [...result.distinction, testResultByGrade[index]].sort() } | |
if(testResultByGrade[index] >=85) { | |
result.highDistinction = [...result.highDistinction, testResultByGrade[index]].sort() | |
} | |
} | |
return result | |
} | |
function getGrade(score) { | |
var result = '' | |
switch(true) { | |
case score >= 85: | |
result = 'high distinction' | |
break | |
case score >= 75: | |
result = 'distinction' | |
break | |
case score >= 65: | |
result = 'credit' | |
break | |
case score >= 50: | |
result = 'pass' | |
break | |
default: | |
result = 'fail' | |
} | |
return result | |
} | |
// adds property 'grade' to student object | |
function addGradeToStudentObject(results) { | |
return results.map(result => { | |
return { | |
...result, | |
grade: getGrade(result.score) | |
} | |
}) | |
} | |
// get grade names (eg 'pass') from results | |
// and create a new array with only those grade names | |
function createGradesArray(results){ | |
// create object with grades as keys | |
// add students to each key based on their result | |
var organisedByGrades = Array.from(new Set(results.map(r => r.grade))) | |
return organisedByGrades | |
} | |
// create a new object who's key names | |
// are created from an array of strings | |
function createObjectWithKeys(arr) { | |
var newObj = {} | |
// for each string in an array, add a key | |
// named by that string in the newObj object | |
arr.map(el => newObj[el] = []) | |
return newObj | |
} | |
// given a results object, add student names to | |
// corresponding object key | |
// eg: if grade is 'pass', add student name to 'pass' | |
// property in object | |
function categoriseResults(results, grades) { | |
// grades object: | |
// {distinction: [], pass: [], etc etc} | |
var gradesObj = Object.assign({}, grades) | |
results.map(result => gradesObj[result.grade] = [...gradesObj[result.grade], result.name]) | |
return gradesObj | |
} | |
var testResultsWithNameArr = addGradeToStudentObject(scoresWithName) | |
var gradesArray = createGradesArray(testResultsWithNameArr) | |
var gradesObject = createObjectWithKeys(gradesArray) | |
var testResults = addGradeToStudentObject(scoresWithName) | |
var categorisedResults = categoriseResults(testResults, gradesObject) | |
// console.log(testResultsWithNameArr) | |
// console.log(gradesArray) | |
// console.log(gradesObject) | |
// console.log(testResults) | |
// console.log(categorisedResults) | |
// var testScores2 = [1,2,34,65,87,99999, 85]; | |
// console.log([...testScores, ...testScores2]) | |
// console.log(testResults([...testScores, ...testScores2])) | |
// | |
// mockaroo.com.au | |
// | |
// reference vs value | |
// https://www.freecodecamp.org/news/how-to-get-a-grip-on-reference-vs-value-in-javascript-cba3f86da223/ | |
var person1 = { | |
age: 10, | |
name: 'john' | |
} | |
var person2 = person1 | |
person2.age = 200 | |
console.log(person1) | |
var name1 = "john" | |
var name2 = name1 | |
name2 = 'peter' | |
console.log(name1) | |
console.log(name2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment