How to choose a random string out of an array? - node.js

I am trying to choose a random word from an array of strings but cannot figure out how.
I have researched on this and here is what I have so far:
roast.js
const roastList = [
'Apples',
'Bananas',
'Pears',
];
const roast = roastList[Math.floor(Math.random() * roastList.length)];
module.exports = {
roast
};
index.js:
case 'roast':
if (!message.mentions.users.size) {
return message.reply('you need to tag a user in order to roast them!');
}
message.channel.send(`Hey ${taggedUser}, ${roast}`);
break;
I copied the code so the fruits are placeholders. I hope that I can get different fruits each time I output roast.

The problem here is that the module code will only be run one time, that means that only one "roast" will be chosen the first time the module is loaded. You want to wrap up the "roast" selection logic in a function that can be called every time you want to "roast" someone.
Consider making roast.js export a function:
const roastList = [
'Apples',
'Bananas',
'Pears'
];
module.exports = function () {
return roastList[Math.floor(Math.random() * roastList.length)];
};
Then call that function in your template:
const roast = require('./roast.js');
...
message.channel.send(`Hey ${taggedUser}, ${roast()}`);

Works for me. With the export you have defined in your example the import can be done like the following, for example.
const roast = require('./roast').roast;
If you simply export the string instead of an object containing the string, you can also do this as follows:
module.exports = roast;
...
const roast = require('./roast');
See also my repl.it for demonstration
EDIT: I have just noticed by the comments you have posted that you are probably looking for a solution where you can get a new random roast each time when a roaster is required in the chat protocol. To do this my suggestion is to export a function which returns a random roast string. See example below, I have also extended the repl.it demo.
roast3.js
const roastList = [
'Apples',
'Bananas',
'Pears',
];
function roast() {
return roastList[Math.floor(Math.random() * roastList.length)]
}
module.exports = {
roast
};
index.js
const roast3 = require('./roast3').roast;
for (x=0; x<10; x++) {
console.log(roast3())
}

const roastList = [
'Apples',
'Bananas',
'Pears',
];
const roast = roastList[Math.floor(Math.random() * roastList.length)];
console.log(roast);
This actually works.
But why do you exports it ?

Related

Best way to organize firebase writes on update trigger

There may be more than one correct answer to this question, but here's my issue: I have a user document in firebase with many fields that can be updated and which interact in different ways. If one field is updated, it may require a change to another field on the backend. Is it better to have a whole bunch of if statements each with their own write action if the condition is met or, or do single write at the end of the function for all the fields that might change. If a field does not change, I would have to write its original value back to itself. That seems clunky, but so does the other option. Am I missing a third option? What is the best practice here?
Here's an example of what I'm talking about. Updating fields one at a time is what I have now, which looks like this:
export const userUpdate = functions.firestore
.document("users/{userID}")
.onUpdate(async (change) => {
const beforeData = change.before.data();
const afterData = change.after.data();
// user levels up and gets more HP
if(beforeData.userLevel != afterData.userLevel){
const newMaxHP = 15 + 5 * afterData.userLevel;
change.after.ref.update({
maxHp: newMaxHP
})
}
//update user rating
if (beforeData.numberOfRatings != afterData.numberOfRatings) {
const newRating = placerRating(beforeData.userRating, beforeData.numberOfRatings, afterData.latestRating);
change.after.ref.update({
userRating: newRating
})
}
//replenish user funds from zero
if (afterData.money == 0){
change.after.ref.update({
money: 20
})
}
If I did it all in a single write, the if statements would assign a value to a variable, but not update the firestore document. Each if statement would include an else statement assigning the variable to the field's original value. There would be a single write at the end like this:
change.after.ref.update({
maxHp: newMaxHP,
userRating: newRating,
money: 20
})
I hope that helps.
[edit to add follow-up question about updating a map value]
#Dharmaraj's answer works great, but I'm struggling to apply it when updating a map value. BTW - I'm using Typescript.
Before using #Dharmaraj's solution, I was doing this:
admin.firestore().collection("users").doc(lastPlayerAttacker).update({
"equipped.weapon.usesLeft": admin.firestore.FieldValue.increment(-1)
});
Using the update object, I'm trying it like this, but I get the error "Object is of type 'unknown'"
const lastPlayerUpdates:{[key:string]:unknown} = {};
lastPlayerUpdates.equipped.weapon.usesLeft = admin.firestore.FieldValue.increment(-1);
admin.firestore().collection("users").doc(lastPlayerAttacker).update(lastPlayerUpdates);
Any advice on how to fix it?
Every time you call update(), you are being charged for 1 write operation. It'll be best to accumulate all updated fields in an object and then update the document only once as it'll be more efficient too. Try refactoring the code as shown below:
export const userUpdate = functions.firestore
.document("users/{userID}")
.onUpdate(async (change) => {
const beforeData = change.before.data();
const afterData = change.after.data();
const updatedData = {};
// user levels up and gets more HP
if (beforeData.userLevel != afterData.userLevel) {
const newMaxHP = 15 + 5 * afterData.userLevel;
updatedData.maxHp = newMaxHP;
}
//update user rating
if (beforeData.numberOfRatings != afterData.numberOfRatings) {
const newRating = placerRating(beforeData.userRating, beforeData.numberOfRatings, afterData.latestRating);
updatedData.userRating = newRating;
}
//replenish user funds from zero
if (afterData.money == 0) {
updatedData.money = 20;
}
await change.after.ref.update(updatedData);
console.log("Data updated");
return null;
})

Adding data to JSON object with data from variables

So my goal is to have an object variable that will be empty at the start but as the code starts running it would get filled up with data from other varibales. When it gets filled up it should look like this:
var fruits = [banana, apple, ...];
var colors = [yellow, green, ...];
var calories = [300, 250, ...]
//the JSON object
{
banana :
{
"color" : "yellow",
"calories" : 300
},
apple :
{
"color" : "green",
"calories" : 250
},
...
}
As you can see all of the data is supposed to be pulled from other variables and this is where I bump into problems. I've tried the following:
var object.fruits[0] = {colors : calories};
//also tried this
var object.fruits[0] = "{""'" + (colors[0]) + "'":+calories[0]+"}";
//and tried many other things...
I've been failing to counter this for at least an hour now and what makes it worse is that some data is supposed to come from other JSON objects. Any idea how to make it work? Also note that having them in an object array is not a option as the list will be HUGE and therefore the time efficiency will be very poor.
Maybe try something like this
res = {}
fruits.map((key, index) => {
res[key] = {
'color': colors[index],
'calories': calories[index]
}
})
You can do like this but yeah put validations to make sure all three arrays are of equal length.
Whenever you want to add a property to an Object where the property value is a value of another variable it is better to use the bracket notation to add the properties to the object [] as used below.
One more thing better use let and const in place of var.
Finally you can use JSON.stringify() to convert into JSON String from the Javascript Object.
'use strict';
const fruits = ['banana', 'apple'];
const colors = ['yellow', 'green'];
const calories = [300, 250];
const fruits_object = {};
for (let i = 0; i < fruits.length; i++) {
fruits_object[fruits[i]] = {};
fruits_object[fruits[i]]['color'] = colors[i];
fruits_object[fruits[i]]['calories'] = calories[i];
}
console.log(JSON.stringify(fruits_object));
Just do it normally like so:
color: colors[0]
and then call JSON.stringify on the entire object like so
JSON.stringify({color: colors[0]})

concatenate content using loop over array

I have a Bixby capsule in progress that lets users access both free and premium content "packs". Each pack is a file stored in a content/ directory. I want to loop over these files and read them into a variable called entitled_content.
I started from the facts capsule which uses a utility function to search a local file called content.js.
const CONTENT = []
const literature = require("../content/literature")
const enhanced = require("../content/enhanced")
const roosevelt = require("../content/roosevelt")
const ambition = require("../content/ambition")
const chaucer = require ("../content/chaucer")
//const GET_REMOTE = require('./lib/getRemoteContent.js')
var console = require('console')
console.log(roosevelt)
console.log(ambition)
console.log(chaucer)
const entitlements = ["roosevelt", "ambition", "chaucer"]
var entitled_content = []
entitlements.forEach(function (item) {
entitled_content = entitled_content.concat(item)
console.log(item); })
console.log(entitled_content)
What it does is this:
[ { tags: [ 'roosevelt' ],
text: 'Happiness is not a goal; it is a by-product. --Eleanor Roosevelt',
image: { url: 'images/' } } ]
[ { tags: [ 'ambition' ],
text: 'Ambition is but avarice on stilts, and masked. --Walter Savage Landor' } ]
[ { tags: [ 'literature' ],
text: 'A man was reading The Canterbury Tales one Saturday morning, when his wife asked What have you got there? Replied he, Just my cup and Chaucer.' },
{ tags: [ 'literature' ],
text: 'For years a secret shame destroyed my peace-- I\'d not read Eliot, Auden or MacNiece. But now I think a thought that brings me hope: Neither had Chaucer, Shakespeare, Milton, Pope. Source: Justin Richardson.' } ]
roosevelt
ambition
chaucer
[ 'roosevelt', 'ambition', 'chaucer' ]
What I want it to do is to assemble these three files roosevelt, ambition and chaucer into a single array variable entitled_content that will then be searched by the utility function. What's wrong is that this line entitled_content = entitled_content.concat(item) isn't doing what I want it to do, which is to get the entire contents of the file named "item".
Because you wrapped your variable names in quotation marks the program reads them as strings.
Change it from
const entitlements = ["roosevelt", "ambition", "chaucer"]
to
const entitlements = [roosevelt, ambition, chaucer]

get all values from dictionary

I'm using linqjs in my website and I'm trying to get all values of a dictionary populated with toDictionary() library extension.
Here is my code:
var imagesDictionary = Enumerable.from(data)
.select(function (x) {
var images = Enumerable.from(x.ImagesSections)
.selectMany(function (y) {
return Enumerable.from(y.Images)
.select(function (z) {
return z.Thumb;
});
})
.toArray();
return { Title: x.Title, Images: images };
})
.toDictionary("$.Title", "$.Images");
var imagesToPreload = imagesDictionary.toEnumerable()
.selectMany("$.Value");
I would that imagesToPreload become an array of all images contained in the dictionary but I can't understand how to do that and this:
var imagesToPreload = imagesDictionary.toEnumerable()
.selectMany("$.Value");
seems the way than everyone used to obtain that.
Could someone help me?
Since it appears you're using the linqjs 3 beta version, the format of the entries have changed. The properties are in lowercase now.
var imagesDictionary = Enumerable.from(data)
.toDictionary("$.Title",
"Enumerable.from($.ImagesSections).selectMany('$.Images', '$$.Thumb').toArray()"
);
var imagesToPreload = imagesDictionary.toEnumerable()
.selectMany("$.value") // lowercase 'value'
.toArray();

Sharing & modifying a variable between multiple files node.js

main.js
var count = 1;
// psuedocode
// if (words typed begins with #add)
require('./add.js');
// if (words typed begins with #remove)
require('./remove.js');
// if (words typed begins with #total)
require('./total.js');
module.exports.count = count;
total.js
var count = require('./main.js').count;
console.log(count);
add.js
var count = require('./main.js').count;
count += 10;
console.log(count);
remove.js
var count = require('./main.js').count;
count -= 10;
console.log(count);
console.log
1
11
-9
Background:
I have an application (irc bot), and I want to add a feature that peeps can do #add 1 or #remove 1. I have a main.js that then requires different files depending on the triggers that are said. So add would trigger the add.js file, and that would then require('main.js') and add 10 (10 for simplification, it'll actually parse the number and use that number) to it. The problem I'm having is when someone goes about and does #remove. It require('main.js') and subtracts 10 from 1 resulting in -9. And doing #total would output 1.
I've done a fairly good search for module.exports and I haven't come across an example like the one i listed above. The docs don't include any examples close to what I'm wanting to do; and these questions 1, 2 I understand--but aren't of any usefulness to me--as I understand what's being said there.
Question:
I'd like to have both #add and #remove manipulate the same variable ( count ), and for #total to return the total of count with the #add and #removes taken into account. Am I using module.exports incorrectly; or is there a common way that variables are shared, with one file being able to modify the contents of the module.exports and returning the results to the main.js file?
Your problem is that when you do var count = require('./main.js').count;, you get a copy of that number, not a reference. Changing count does not change the "source".
However, you should have the files export functions. Requiring a file will only run it the first time, but after that it's cached and does not re-run. see docs
Suggestion #1:
// main.js
var count = 1;
var add = require('./add.js');
count = add(count);
// add.js
module.exports = function add(count) {
return count+10;
}
#2:
var count = 1;
var add = function() {
count += 10;
}
add();
#3: Personally i would create a counter module (this is a single instance, but you can easily make it a "class"):
// main.js
var counter = require('./counter.js');
counter.add();
console.log(counter.count);
// counter.js
var Counter = module.exports = {
count: 1,
add: function() {
Counter.count += 10;
},
remove: function() {
Counter.count += 10;
}
}
Not sure if this new or not but you can indeed share variables between files as such:
main.js
exports.main = {
facebook: null
};
counter.js
var jamie = require('./main');
console.info(jamie); //{facebook: null}
jamie.main.facebook = false;
console.info(jamie); //{facebook: false}
anothercheck.js
var jamie = require('./main');
console.info(jamie); //{facebook: null} //values aren't updated when importing from the same file.
jamie.main.facebook = true;
console.info(jamie); //{facebook: true}
Now you can share between files.
I know I'm a little bit late to answer this questions, just 7yrs!
You can simply use a global variable:
global.myVar = 'my-val';
console.log(myVar); // returns 'my-val'
// from here on it's accessable to all modules by just the variable name
using-global-variables-in-node-js
I have same problem like you,.. Sometimes I'd like to sharing variables between multiple files because I love modular style eg. separating controller, function, models in different folders/files on my node.js script so I can easy manage the code.
I don't know if this is the best solution but I hope will suit your needs.
models/data.js
// exports empty array
module.exports = [];
controllers/somecontroller.js
var myVar = require('../models/data');
myVar.push({name: 'Alex', age: 20});
console.log(myVar);
// [{ name: 'Alex', age: 20 }]
controllers/anotherController.js
var myVar = require('../models/data');
console.log(myVar);
// This array has value set from somecontroller.js before...
// [{ name: 'Alex', age: 20 }]
// Put new value to array
myVar.push({name: 'John', age: 17});
console.log(myVar);
// Value will be added to an array
// [{ name: 'Alex', age: 20 }, { name: 'John', age: 17}]
There is no way you can share a reference between different files. You shouldn't be.
I have a main.js that then requires different files depending on the triggers that are said
I don't think that's a good idea. All require statements you'll ever need must be at the top of the file.
I also see that You're requiring main.js in total.js and total.js in main.js. The require() function imports the module.exports of the file and assigns it to the namespace you provide. Your code shouldn't be split into files this way. You extract code into separate files only when they're modules by themselves. And if you do, you wouldn't be importing 2 files on each other.
It is also good to note that in javascript, when you assign something to a namespace, It gets copied (cloned) if it's a primitive. If it's an object, both namespaces then refer to the same object
var num = 5;
var prim = num;
prim++; // prim is 6, but num is still 5.
var num = {five:5};
var ob = num;
ob.five = 6;
console.log(num.five) //also 6.
A little hack that works but isn't recommended is using the process variable. You can apply different properties to it and essentially use them like you would the window object in browser-based JS. This little hack will provide a reference to the variable. It can be changed and manipulated and the change will carry over to all files that are required.
But do note that it is not recommended as overriding the process variable could have some unexpected effects and is subject to loss of information should another process interfere.
file1.js:
const s1 = require('./file2');
process.num = 2;
console.log("file1", process.num);
s1.changeNum();
console.log("file1", process.num);
file2.js:
module.exports.changeNum = () => {
process.num = 3;
console.log("file2", process.num);
};
output:
file1 2
file2 3
file1 3
alternatively, to all other answers
getters & setters
var _variableThing = 1223
module.exports = {
get variableThing(){
return _variableThing
},
set variableThing(val){
_variableThing = val
}
}
won't work with direct imports though

Resources