Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework assignment #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion js/refactoring/momsSpagetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,58 @@ function momsSpagetti(lyrics) {
return loseYourself;
}

// REFACTORED VERSION HERE //
// REFACTORED VERSION HERE //
//
// An array of promises is created, with each element
// consisting of a section of song lyrics.
// After all lyric sections are retrieved, the complete
// song lyrics are returned.
//
// If these lyrics were being accessed over the Internet, for example,
// a promise-based solution would allow the lyrics to be accessed
// independently of each other, at the same time.
// The array of promises ensures that
// all the lyrics are displayed at the same time and in order,
// even if some are initially returned sooner than others.
//
// In the original code, the refrainRepeat property was unused,
// so in this version it is used.
//
// Additionally, ES6 recommends the use of let rather than var
// for declaring variables.

function getLyricElement(lyricElement) {
return new Promise(function(resolve) {
resolve(lyricElement);
});
}

function assembleLyrics() {
let promises = [];
let finalLyrics = lyrics.intro;
promises.push(getLyricElement(lyrics.intro));

for(i = 0; i < lyrics.choruses.length; i += 1) {
promises.push(getLyricElement(lyrics.choruses[i].chorus));
for(j = 0; j < lyrics.refrainRepeat; j += 1) {
promises.push(getLyricElement(lyrics.refrain));
}
}
promises.push(getLyricElement(lyrics.ending));

Promise.all(promises)
.then(function(results) {
for(i = 0; i < results.length; i += 1) {
finalLyrics += results[i];
}
console.log(finalLyrics);
return finalLyrics;
})
.catch(function(error) {
console.error("There was an error \
retrieving the lyrics", error.message);
});

}

assembleLyrics();