diff --git a/.DS_Store b/.DS_Store index 5008ddf..c275727 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a6a7f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +Week3/.DS_Store +Week3/prep-exercises/.DS_Store +.Rhistory +.DS_Store diff --git a/Week2/.DS_Store b/Week2/.DS_Store new file mode 100644 index 0000000..b4a3892 Binary files /dev/null and b/Week2/.DS_Store differ diff --git a/Week2/prep-exercises/.DS_Store b/Week2/prep-exercises/.DS_Store new file mode 100644 index 0000000..164f080 Binary files /dev/null and b/Week2/prep-exercises/.DS_Store differ diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..ed33470 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -9,6 +9,7 @@ function getCurrentState(trafficLight) { // TODO // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. + return trafficLight.possibleStates[trafficLight.stateIndex]; } function getNextStateIndex(trafficLight) { @@ -17,6 +18,7 @@ function getNextStateIndex(trafficLight) { // - if the color is green, it will turn to orange // - if the color is orange, it will turn to red // - if the color is red, it will turn to green + return (trafficLight.stateIndex+1)%trafficLight.possibleStates.length; } // This function loops for the number of seconds specified by the `secs` diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..ab91fac 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -12,6 +12,10 @@ function runExperiment(sampleSize) { // value from the previous step. Use the first element of `valueCounts` // for keeping a count how many times the value 1 is thrown, the second // element for value 2, etc. + for(let i = 0 ; i < sampleSize; i++){ + const x =Math.floor(Math.random()*(6-1+1)+1); //x is the random number + valueCounts[x-1]++; + } const results = []; @@ -24,7 +28,10 @@ function runExperiment(sampleSize) { // 2. Convert the computed percentage to a number string with a precision of // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. - + for(let i of valueCounts){ + let percentage = ((i/sampleSize)*100).toFixed(2); + results.push(percentage); + } return results; } @@ -36,6 +43,12 @@ function main() { // value of the `sampleSizes` array. // Log the results of each experiment as well as the experiment size to the // console. + + for(let sampleSize of sampleSizes){ + let experimentResutls = runExperiment(sampleSize); + console.log(experimentResutls, " ", sampleSize); + } + // The expected output could look like this: // // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 diff --git a/Week2/prep-exercises/2-experiments/test.js b/Week2/prep-exercises/2-experiments/test.js new file mode 100644 index 0000000..fc6ef4e --- /dev/null +++ b/Week2/prep-exercises/2-experiments/test.js @@ -0,0 +1,22 @@ +let x = []; +for (i=0;i<20; i++){ + let y = Math.floor(Math.random()*6+1); + x.push(y); +} + console.log("🚀 ~ x:", x) + + +// const valueCounts = [0, 0, 0, 0, 0, 0]; +// const sampleSize = 100000; + +// for(let i = 0 ; i < sampleSize; i++){ +// const x =Math.floor(Math.random()*(6-1+1)+1); //x is the random number +// valueCounts[x-1]++; +// } +// const results = []; +// for(let i of valueCounts){ +// let percentage = ((i/sampleSize)*100).toFixed(2); +// results.push(percentage); +// } +// console.log("🚀 ~ results:", results) + diff --git a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js index 72baa61..3257582 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,10 +8,11 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + return mentors.filter((mentor) => + mentor.canTeach.includes(moduleName)) + .map((mentor) => mentor.name) }; -// You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('using-apis')); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +21,8 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = possibleMentorsForModule(moduleName) + const randomNumber = Math.floor(Math.random()*possibleMentors.length) + return possibleMentors[randomNumber] }; -// You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +console.log(findMentorForModule('javascript')); diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index 44d2798..f368823 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,10 +12,18 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const activeClass = classes.find((a) => a.name === className); + const studentsOfClass = students.filter((student) => student.class.includes(className)) + const mentorOfClass = mentors.filter((mentor) => mentor.nowTeaching?.includes(activeClass.currentModule)) + const peopleOfClass = [ + ...studentsOfClass.map((student) => ({name: student.name, role: "Student"})), + ...mentorOfClass.map((mentor) => ({name: mentor.name, Role: "Mentor"})) + ] +return peopleOfClass }; + // You can uncomment out this line to try your function -// console.log(getPeopleOfClass('class34')); +// console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -30,7 +38,18 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((c) => c.active === true) + const classInfo ={} + // for (let i of activeClasses){ + // const participants = getPeopleOfClass(i.name); + // classInfo[i.name] = participants; + // } + + activeClasses.map((x)=> { + const participants = getPeopleOfClass(x.name); + classInfo[x.name] = participants + }) + return classInfo }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); + console.log(getActiveClasses()); diff --git a/test scripts.js b/test scripts.js new file mode 100644 index 0000000..559adc7 --- /dev/null +++ b/test scripts.js @@ -0,0 +1,50 @@ +const companies = [ + { + name: "Acme Corp", + ceo: { + name: "John Doe", + location: { city: "New York", country: "USA" }, + }, + }, + { + name: "Beta Ltd", + ceo: null, // No CEO for this company + }, + { + name: "Gamma Inc", + ceo: { name: "Jane Smith" }, // CEO without location information + }, + { + name: "Kojima Productions", + ceo: { + name: "Hideo Kojima", + location:{city: "Tokyo", country:"Japan"} + } + }, + ]; + + // Fix the function to safely retrieve the CEO's city or a default message + function getCeoCity(company) { + if (company.ceo) { + if (company.ceo.location) { + return company.ceo.location.city; + } else { + return "No city information available"; + } + } else { + return "No city information available"; + } + + // const city = company.ceo?.location?.city || "No city information available"; + // return city + + } + + + + console.log(getCeoCity(companies[0])); // Should output "New York" + console.log(getCeoCity(companies[1])); // Should output "No city information available" + console.log(getCeoCity(companies[2])); // Should output "No city information available" + console.log(getCeoCity(companies[3])); // Should output "No city information available" + + \ No newline at end of file