/* -- PHRASE BANK --
File containing phrase data for use by functions in main.js to create a fortune.
Exports function getPhrases(ordinalNum, topic) that returns an array of phrases.
Fortunes created have a first sentence and a second sentence.
Both sentences include a phrase based in a randomly selected topic.
First phrases are each associated with 1 topic.
Second phrases can be associated with multiple topics.
*/
/* --TOPIC SHORTHAND--
'G' short for Good Relationships
'B' short for Bad Relationships
'P' short for At Peace
'C' short for Change
*/
//---------------------------------------------------------------------------------------------
/* --FIRST PHRASES--
First sentences take the format:
"You are in a [number] [unit of time] cycle of [first phrase]."
E.g: "You are in a 2 week cycle cycle of good luck."
First phrases below, organized by topic.
*/
// Good Relationships (G):
const gFirstPhrases = ["good luck", "new relationships", "energy in your erotic eigth house"];
// Bad Relationships (B):
const bFirstPhrases = ["bad luck", "negative energy in your third house that is weakening communication",
"tumult in your eigth house is straining your relationships"];
// At Peace (P):
const pFirstPhrases = ["being grounded", "wellness permeating your body and mind", "good health"];
// Change (C):
const cFirstPhrases = ["growth", "creativity flooding your twelfth house",
"wanderlust entering your third house"];
//-------------------------------------------------------------------------------------------
/* --SECOND PHRASES--
Second sentences take the format:
"[second person directive] [second phrase]."
E.g: "You should be alone with yourself."
There are two second person directives:
- "You should"
- "Let yourself"
Second phrases below, organized into objects that contain more than one topic.
*/
var secondPhrases = [{phrase: "charge your crystals under the moon tonight", C:true, P:true},
{phrase: "be alone with yourself", B:true, C:true, P:true},
{phrase: "pay attention to unproductive habits that have crept in", B:true, C:true, P:true},
{phrase: "pursue new alliances today", G:true},
{phrase: "let your guard down", C:true, P:true},
{phrase: "trust no one", B:true},
{phrase: "burn sage to cleanse", B:true, P:true},
{phrase: "focus on communication", G:true, B:true},
{phrase: "listen to the cosmos", G:true, B:true, P:true, C:true},
{phrase: "be swept away by love", G:true},
{phrase: "grow today", G:true, C:true, P:true},
{phrase: "relax and breathe in deeply", P:true, C:true},
{phrase: "focus on the short term", G:true, B:true, P:true},
{phrase: "focus on the long term", G:true, B:true, C:true, P:true},
{phrase: "be vibrant and take up space today", G:true, C:true, P:true},
{phrase: "practice gratitude", C:true, P:true},
{phrase: "invest in yourself", C:true, P:true},
{phrase: "pursue your passions", G:true, C:true}];
//-------------------------------------------------------------------------
/* --Sort Second Phrases by Topic--
The splitByTopic is a helper funciton for getPhrases meant to parse second phrases by topic.
- secondPhrases: an array of secondPhrase objects
- topic: 'G', 'B', 'P', or 'C', representing a topic
Returns an array of second phrases as strings based on the topic argument.
*/
function splitByTopic (secondPhrases, topic) {
if (topic !== 'G' && topic !== 'B' && topic !== 'P' && topic !== 'C'){
console.log ('Error. Invalid topic argument.');
return [];
}
const topicArray = secondPhrases.reduce((accumulator, current) => {
if (current[topic]){
accumulator.push(current['phrase']);
}
return accumulator;
}, []);
return topicArray;
}
/* -- Get Phrases--
Exported function to get and return an array of phrases.
- ordinalNum: either 'first' or 'second' to represent first phrase or second phrase.
- topic: 'G', 'B', 'P', or 'C', representing a topic
Returns an array of ordinalNum phrases corresponding to topic.
*/
function getPhrases (ordinalNum, topic){
ordinalNum = ordinalNum.toLowerCase();
topic = topic.toUpperCase();
switch (ordinalNum) {
case 'first':
switch (topic) {
case 'G':
return gFirstPhrases;
case 'B':
return bFirstPhrases;
case 'C':
return cFirstPhrases;
case 'P':
return pFirstPhrases;
default:
console.log ('Error. Invalid topic argument.');
break;
}
case 'second':
return splitByTopic(secondPhrases, topic);
default:
console.log ('Error. Invalid ordinalNum argument.');
break;
}
}
//-------------------------------------------------------------------------
// --Helper Functions--
// getRand returns a random integer between 0 and num, exclusive.
const getRand = num => {
return Math.floor(Math.random()*num);
}
// randTimeSpan returns 'day', 'week' or 'month' at random.
const randTimeSpan = () => {
const num = getRand(3);
switch (num) {
case 0:
return 'day';
case 1:
return 'week';
case 2:
return 'month';
}
}
// randTopic returns a random topic to select a phrase from.
const randTopic = () => {
const topic = getRand(4);
switch (topic) {
case 0:
return 'G';
case 1:
return 'B';
case 2:
return 'P';
case 3:
return 'C';
}
}
const randPhrase = phrases => {
const i = getRand(phrases.length);
return phrases[i];
}
//-------------------------------------------------------------------------
// --Main Function--
/*
Fortunes created have a first sentence and a second sentence.
Both sentences include a phrase based in a randomly selected topic.
First phrases are each associated with 1 topic.
Second phrases can be associated with multiple topics.
*/
// getFortune returns a sentence representing a mystery fortune.
function getFortune() {
// Determine topic of fortune (good, bad, etc.)
const topic = randTopic();
/* First sentence
First sentences take the format:
"You are in a [number] [unit of time] cycle of [first phrase]."
E.g: "You are in a 2 week cycle cycle of good luck."
*/
// determine span of cycle for first sentence
const num = 1 + getRand(9);
const time = randTimeSpan();
// determine first phrase
const firstPhrases = getPhrases('first', topic);
const firstPhrase = randPhrase(firstPhrases);
// Construct first sentence:
const firstSent = `You are in a ${num} ${time} cycle of ${firstPhrase}.`;
/*Second sentence
Second sentences take the format:
"[second person directive] [second phrase]."
E.g.: "You should be alone wclearith yourself."
*/
// determine decond person directive
const directive = randPhrase(['You should', 'Let yourself']);
//determine second phrase
const secondPhrases = getPhrases('second', topic);
const secondPhrase = randPhrase(secondPhrases);
//construct second sentence
const secondSent = `${directive} ${secondPhrase}.`;
//construct full fortune
const fortune = `~~~~~~~~~~~ YOUR FOTUNE AWAITS ~~~~~~~~~~~~\n${firstSent}\n${secondSent}`;
return fortune
}
console.log(getFortune());
Hi folks! Come have your fortune told!
The project gives you a two sentence fortune. It randomizes different parts of the sentences, e.g., giving you a random cycle length, and giving you random phrases based on a topic.
I thought grouping by topics was a good way to go about it, so your fortune is truly unique but the phrases are still (somewhat) coherent.
Some of the phrases in the second sentence belong to more than one topic, so I stored the data as objects having properties of belonging to different topic groups. Not sure if that was the most efficient way to store and access data, so I’m open to feedback.
Enjoy!