Last active
January 16, 2019 20:01
-
-
Save mariohmol/d1ffa0cc94dacaf7708b3a0c13138bc5 to your computer and use it in GitHub Desktop.
algorithms
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
class Teacher { | |
public name: string; | |
public classes: Array<StudentClass> = []; | |
public quizes: Array<Quiz> = []; | |
addClass(name) { | |
this.classes.push(new StudentClass(name)); | |
} | |
addStudentToClass(name, student) { | |
this.classes.forEach((c: StudentClass) => { | |
if (c.name == name) { | |
const found = c.students.find(s => s === student); | |
if (found) { | |
c.students = c.students.filter(s => s === student); | |
} else { | |
c.students.push(student); | |
} | |
} | |
}); | |
} | |
} | |
class StudentClass { | |
public id: string; | |
public name: string; | |
public students: Array<Student>; | |
constructor(name) { | |
this.name = name; | |
} | |
} | |
class Student { | |
public name; | |
public quizzes: Array<Quiz>; | |
} | |
class Quiz { | |
public questions: Array<Question>; | |
addQuestion(title, correct, grade, choices) { | |
this.questions.push(new Question(title, correct, grade, choices)); | |
} | |
} | |
class QuizzAnswer{ | |
public answers: Array<Question>; | |
} | |
class Question { | |
public title; | |
public correct; | |
public grade; | |
public choices; | |
constructor(title, correct, grade, choices) { | |
this.title = title; | |
this.correct = correct; | |
this.grade = grade; | |
this.choices = choices; | |
} | |
} | |
const mario = new Teacher() | |
mario.name = "Mario"; | |
const bill = new Student() | |
bill.name = "Mario"; | |
const quizz = new Quiz() | |
quizz.questions = ["Mario"]; | |
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
import React, Component from 'react'; | |
import queryAPI from 'queryAPI'; | |
import PropTypes from 'prop-types'; | |
//component should no be called | |
class ShowResultsFromAPI extends Component { | |
//constructor missing props | |
constructor(props) { | |
super(props); | |
//props should no be changed | |
this.state = { apiQueryDelay: this.props.apiQueryDelay}; | |
//this.container = null; // container is never used | |
} | |
onDisableDelay() { | |
this.setState({ | |
apiQueryDelay: 0 | |
}); | |
// using state instead of changin prop | |
// this.props.apiQueryDelay = 0; | |
} | |
click() { | |
//using state instead | |
if (this.state.apiQueryDelay) { | |
setTimeout(function() { | |
this.fetchData(); | |
}, this.state.apiQueryDelay); | |
} | |
//show run with no delay | |
else{ | |
this.fetchData(); | |
} | |
} | |
fetchData() { | |
queryAPI() | |
//show convert to json object | |
.then(res=>res.json()) | |
.then(function(response) { | |
if (response.data) { | |
this.setState({ | |
data: response.data, | |
error: false | |
}); | |
} | |
}); | |
} | |
//render show show the data from request, and getting info from state | |
render() { | |
return <div class="content-container" ref="container"> | |
{ | |
if (!!this.state.error) { | |
<p>Sorry - there was an error with your request.</p> | |
} | |
else { | |
<p>{this.state.data}</p> | |
} | |
} | |
</div> | |
<Button onClick={this.onDisableDelay.bind(this)}>Disable request delay</Button> | |
<Button onClick={this.click.bind(this)}>Request data from endpoint</Button> | |
} | |
} | |
ShowResultsFromAPI.displayName = { | |
name: "ShowResultsFromAPI" | |
}; | |
ShowResultsFromAPI.defaultProps = { | |
apiQueryDelay: 0 | |
}; | |
// fixing import | |
ShowResultsFromAPI.propTypes = { | |
apiQueryDelay: PropTypes.number | |
}; | |
//this does not exist | |
// export ContentContainer; | |
export default ShowResultsFromAPI; |
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
myinput = [[1, 2, [3]], 4]; | |
function convert(input, array = []) { | |
if (!(input instanceof Array)) { | |
array.push(input); | |
} else { | |
input.forEach(i => { | |
convert(i, array); | |
}); | |
} | |
return array; | |
} | |
const finalarray = convert(myinput); | |
console.log(finalarray) // [1,2,3,4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment