Skip to content

Instantly share code, notes, and snippets.

@binhtran04
Last active April 8, 2020 16:18
Show Gist options
  • Save binhtran04/ed4db533ba8f2d0d36db3e17d138242c to your computer and use it in GitHub Desktop.
Save binhtran04/ed4db533ba8f2d0d36db3e17d138242c to your computer and use it in GitHub Desktop.
// Type JavaScript here and click "Run Code" or press Ctrl + s
console.log('Hello, world!');
// CHALLENGE 1
function sumFunc(arr) {
// YOUR CODE HERE
let sum = 0
for (let i = 0; i < arr.length; i++) {
sum += arr[i]
}
return sum
}
// Uncomment the lines below to test your work
const array = [1, 2, 3, 4];
console.log(sumFunc(array)); // -> should log 10
function returnIterator(arr) {
// YOUR CODE HERE
let i = 0;
function iterator() {
const element = arr[i]
i++
return element
}
return iterator
}
// Uncomment the lines below to test your work
const array2 = ['a', 'b', 'c', 'd'];
const myIterator = returnIterator(array2);
console.log(myIterator()); // -> should log 'a'
console.log(myIterator()); // -> should log 'b'
console.log(myIterator()); // -> should log 'c'
console.log(myIterator()); // -> should log 'd'
// CHALLENGE 2
function nextIterator(arr) {
// YOUR CODE HERE
let i = 0;
function next() {
const element = arr[i]
i++
return element
}
return {
next
}
}
// Uncomment the lines below to test your work
const array3 = [1, 2, 3];
const iteratorWithNext = nextIterator(array3);
console.log(iteratorWithNext.next()); // -> should log 1
console.log(iteratorWithNext.next()); // -> should log 2
console.log(iteratorWithNext.next()); // -> should log 3
// CHALLENGE 3
function sumArray(arr) {
// YOUR CODE HERE
// use your nextIterator function
const iterator = nextIterator(arr)
let sum = 0
let next = iterator.next()
while (next !== undefined) {
sum += next
next = iterator.next()
}
return sum
}
// Uncomment the lines below to test your work
const array4 = [1, 2, 3, 4];
console.log(sumArray(array4)); // -> should log 10
// CHALLENGE 4
function setIterator(set) {
// YOUR CODE HERE
let i = 0
const arrFromSet = Array.from(set)
function next() {
const element = arrFromSet[i]
i ++
return element
}
return {
next
}
}
// Uncomment the lines below to test your work
const mySet = new Set('hey');
const iterateSet = setIterator(mySet);
console.log(iterateSet.next()); // -> should log 'h'
console.log(iterateSet.next()); // -> should log 'e'
console.log(iterateSet.next()); // -> should log 'y'
// CHALLENGE 5
function indexIterator(arr) {
// YOUR CODE HERE
let i = 0;
function next() {
const element = arr[i]
const index = i
i++
return [index, element]
}
return {
next
}
}
// Uncomment the lines below to test your work
const array5 = ['a', 'b', 'c', 'd'];
const iteratorWithIndex = indexIterator(array5);
console.log(iteratorWithIndex.next()); // -> should log [0, 'a']
console.log(iteratorWithIndex.next()); // -> should log [1, 'b']
console.log(iteratorWithIndex.next()); // -> should log [2, 'c']
// CHALLENGE 6
function Words(string) {
this.str = string;
}
Words.prototype[Symbol.iterator] = function() {
// YOUR CODE HERE
let i = 0
const wordArr = this.str.match(/\w+/g)
function next() {
const element = wordArr[i];
if (i < wordArr.length) {
i++;
return {value: element, done: false}
}
return {done: true}
};
return {
next
}
}
// Uncomment the lines below to test your work
const helloWorld = new Words('Hello World');
for (let word of helloWorld) { console.log(word); } // -> should log 'Hello' and 'World'
// CHALLENGE 7
function valueAndPrevIndex(array){
let i = 0
function sentence() {
const element = array[i]
const index = i
i++
if (index === 0) return 'it is the first'
return `${element} was found after index ${index-1}`
}
return {sentence}
}
const returnedSentence = valueAndPrevIndex([4,5,6])
console.log(returnedSentence.sentence());
console.log(returnedSentence.sentence());
console.log(returnedSentence.sentence());
//CHALLENGE 8
function* createConversation(string) {
let outputValue;
if (string === 'english') {
outputValue = 'gibberish'
} else {
outputValue = 'hello there'
}
yield outputValue;
/*window.setInterval(function () {
yield outputValue;
}, 3000)
*/
}
console.log(createConversation('english').next());
//CHALLENGE 9
function waitForVerb(noun) {
const verb = 'walk';
return `${noun} ${verb}`
}
async function f(noun) {
const sentence = waitForVerb(noun)
setTimeout(() => {
console.log(sentence)
}, 300)
}
f("dog");
// Challenge 1
function sayHello() {
setTimeout(() => {
console.log('Hello!')
}, 1000)
}
// Uncomment the line below when ready
sayHello(); // should log "Hello" after 1000ms
// Challenge 2
var promise = new Promise(function (resolve, reject) {
// ADD CODE HERE
setTimeout(() => {
resolve('Resolved!')
}, 1000)
});
// Should print out "Resolved!"
// ADD CODE HERE
promise.then(value => console.log(value))
// Challenge 3
promise = new Promise(function(resolve, reject) {
// ADD CODE HERE
reject('Rejected!')
})
// Should print out "Reject!"
// ADD CODE HERE
promise.catch(value => console.log(value))
// Challenge 4
promise = new Promise(function (resolve, reject) {
// ADD CODE HERE
resolve('Promise has been resolved!')
});
// Uncomment the lines below when ready
promise.then(() => console.log('Promise has been resolved!'));
console.log("I'm not the promise!");
// Challenge 5
function delay(){
return new Promise((resolve) => {
return setTimeout(() => {
resolve()
}, 1000)
})
}
// Uncomment the code below to test
// This code should log "Hello" after 1000ms
delay().then(sayHello);
// Challenge 6
//
// ADD CODE BELOW
var secondPromise = new Promise(resolve => resolve('Second!'))
var firstPromise = new Promise(resolve => resolve(secondPromise))
firstPromise
.then(value => value)
.then(value => console.log(value))
// Challenge 7
const fakePeople = [
{ name: 'Rudolph', hasPets: false, currentTemp: 98.6 },
{ name: 'Zebulon', hasPets: true, currentTemp: 22.6 },
{ name: 'Harold', hasPets: true, currentTemp: 98.3 },
]
const fakeAPICall = (i) => {
const returnTime = Math.floor(Math.random() * 1000);
return new Promise((resolve, reject) => {
if (i >= 0 && i < fakePeople.length) {
setTimeout(() => resolve(fakePeople[i]), returnTime);
} else {
reject({ message: "index out of range" });
}
});
};
function getAllData() {
// CODE GOES HERE
Promise.all(
[0, 1, 2].map(item => fakeAPICall(item))
)
.then(values => console.log(values))
}
getAllData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment