Skip to content

Instantly share code, notes, and snippets.

@yibojiang
Last active May 1, 2018 00:01
Show Gist options
  • Save yibojiang/480cb8d198e1d3fb4221cae5f1387c45 to your computer and use it in GitHub Desktop.
Save yibojiang/480cb8d198e1d3fb4221cae5f1387c45 to your computer and use it in GitHub Desktop.
final for web programming
/**
* The following 2 questions are intended to measure your progress through the semester.
* There is no "correct" answer, and I just want to see how well you can solve them.
* Good luck.
*/
/*
* 1. React Component
* Create a <Header> component that takes a single prop "type" and renders an html element with that type.
* For example:
* If the "type" is "h1", then an <h1> tag should be rendered.
* The text of the tag should be whatever is passed in between the <Header> and </Header> tags.
*/
import React, { Component } from 'react';
/**
* Define your component here
*/
class Header extends Component {
constructor(props) {
super(props);
}
render() {
const MyTag = this.props.type;
return (
<MyTag>{this.props.children}</MyTag>);
}
}
class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div className="App">
<Header type = {'h1'}>123</Header>
</div>
);
}
}
export default App;
/*
* 2. Servers and APIs
* Using this URL: https://hn.algolia.com/api/v1/search?query=react&tags=story
* Write a simple express program that will accept a request and returns the titles from the API
*/
var express = require('express');
var app = express();
// Your code goes here
var request = require('request')
app.get('/', (req, res)) => {
fetch('https://hn.algolia.com/api/v1/search?query=react&tags=story').
then(
function(response) {
response.json().then(data){
var titles = data.map((item) => item.title)
res.send(titles);
}
}
)
.catch(function(err) {
res.send(err);
});
}
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment