Random Insult Generator (Randomizing Results) - javascript

I've written a random insult generator. It is fine, and works well to what I need. The problem is that when I run the program more than once, the insult is the same every time, unless I copy all the vars again. Here is my code:
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
var randomInsult = "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
What I'm trying to do is when I run randomInsult; again, I want a different result.

Use a function:
function generateRandomInsult() {
// ... all of your existing code ...
return randomInsult;
}
generateRandomInsult(); // this is what you repeat each time

As per my comment above, you need to use a function.
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var randomInsult = (function() {
var bp, a, a2, an;
var bp = bodyPart[Math.floor(Math.random() * 5)];
var a = adjective[Math.floor(Math.random() * 4)];
var a2 = adjectiveTwo[Math.floor(Math.random() * 5)];
var an = animal[Math.floor(Math.random() * 5)];
var insult = "Your" + " " + bp + " " + "is more" + " " + a + " " + a2 + " " + "than a" + " " + an + "'s" + " " + bp + ".";
alert(insult);
});
document.getElementById('test').addEventListener('click', randomInsult, false);
<button id="test">Click me</button>

You'll have to do something like this to select a random bodyPart, adjective, adjectiveTwo and animal at each call.
function randomInsult() {
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
return "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
}
In your piece of code you are generating the "randomInsult" string only once. It need to be generated at each call, with new random values.
So, simply embed your code in a function.
Call it this way:
randomInsult();

Related

How to display today and 5 days with a counter and pass variables from build report down to the build alert function?

How to make "funding breakdown" show today and the next five business days without removing the “totalfunding” of just today and adding a counter to the “fundingbreakdown” so it comes out numbered?
I am passing through my variables from build report down to the build alert function, but for some reason, it is not passing the values from my variables. Why is that happening?
I appreciate your patience as this is my very first googleappscript.
function buildreport() {
const ss = SpreadsheetApp.getActive();
let data = ss.getSheetByName('February 2023').getRange("A:M").getValues();
var PrimorNonPrim = ss.getSheetByName('February 2023').getRange("A:A").getValues();
var Regionandentity = ss.getSheetByName('February 2023').getRange("B:B").getValues();
var Currency = ss.getSheetByName('February 2023').getRange("D:D").getValues();
var Amount = ss.getSheetByName('February 2023').getRange("F:F").getValues();
var RequestDate = ss.getSheetByName('February 2023').getRange("K:K").getValues();
var BankAcctCreditDate = ss.getSheetByName('February 2023').getRange("L:L").getValues();
var PayDate = ss.getSheetByName('February 2023').getRange("M:M").getValues();
let payload = buildAlert(data);
sendAlert(payload);
}
function buildAlert(data,PrimorNonPrim,Regionandentity,Currency,Amount,RequestDate,BankAcctCreditDate,PayDate) {
let today = new Date();
let filteredData = data.filter(row => {
let requestDate = new Date(row[10]);
return requestDate.getFullYear() === today.getFullYear() &&
requestDate.getMonth() === today.getMonth() &&
requestDate.getDate() === today.getDate();
});
let totalfunding = filteredData.reduce((total, row) => total + row[5], 0);
if (filteredData.length === 0) {
let fundingBreakdown = "Nothing coming up within 5 working days";
} else {
fundingBreakdown = (PrimorNonPrim + " " +"Entity" + " " + Regionandentity + " " + "Currency" + " " + Currency + " " + "Amount" + " " + Amount + " " + "Request Date" + " " + RequestDate + " " + "Bank Account Credit Date" + " " + BankAcctCreditDate + " " + "Pay Date" + " " + PayDate)}

Creating Populated email from Javascript

I want to start off by saying I have no idea what I am doing with HTML and Javascript but I am trying to learn. What I am creating will not be hosted by any server , it is more of a HTML web form(I think that is what i would call it) for employees to fill out and create a standardized email. I have 97% of it working but need a little help with the last part. Below is the Javascript that works:
function populateEmail() {
let bl = document.getElementById("blurb").value;
let a = document.getElementById("reg").value;
let b = document.getElementById("lvl").value;
let c = document.getElementById("node").value;
let i = document.getElementById("cust").value;
let d = document.getElementById("rea").value;
let e = document.getElementById("ma").value;
let f = document.getElementById("start_dt").value.replace("T", " ");
let g = document.getElementById("poc").value;
let h = document.getElementById("appr").value;
let m_to = "DL-ListOne; DL-ListTwo; DL-ListThree"
let m_cc = "DL-ListFour; DL-ListFive;"
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
let notify = document.getElementById("notify_lvl").value;
if (notify == "Initial"){
let deap = "Activation ";
}
else if (notify == "Update"){
let deap = "Update ";
}
else{
let deap = "De-Activation ";
}
document.location.href = "mailto:" + encodeURIComponent(m_to) + "?cc=" + encodeURIComponent(m_cc)
+ " &subject=DEAP " + encodeURIComponent(b) + ": Any Region " + today
+ " "
+ "&body="
+ "%0D%0A%0D%0A"
+ encodeURIComponent(bl) + "%0D%0A%0D%0A"
+ "Name: " + encodeURIComponent(a) + "%0D%0A"
+ "Activation – (" + encodeURIComponent(b)
+ "Current Impact = " + encodeURIComponent(c) + " modules, " + encodeURIComponent(i) + " customers) %0D%0A"
+ "Reason: " + encodeURIComponent(d) + "%0D%0A"
+ "Event Geographical Area: " + encodeURIComponent(e) + "%0D%0A"
+ "Event Start: " + encodeURIComponent(f) + "%0D%0A"
+ "POC: " + encodeURIComponent(g) + "%0D%0A"
+ "Approved by: " + encodeURIComponent(h) + "%0D%0A"
}
Now when I try and add the Variable deap to the subject line it stops creating the email. Here are the different ways I have tried to add it.
+ " &subject=DEAP " + deap + encodeURIComponent(b) + ": NE Region " + today
+ " &subject=DEAP " + encodeURIComponent(deap) + encodeURIComponent(b) + ": NE Region " + today
then I thought that maybe I had to add some text or space in there to have it take affect so I tried adding + " " + after the variable deap
I am trying to keep the post to a minimum, if you need my ugly looking HTML I will be happy to post it
but I am still trying to figure out how to load div from Javascript so my code isn't DRY.
Thank you in advance for your time
It's because you're declaring deap inside your if statements:
if ('notify' == "Initial") {
let deap = "Activation ";
} else if ('notify' == "Update") {
let deap = "Update ";
} else {
let deap = "De-Activation ";
}
console.log(deap);
If you declare it outside and reassign it inside your if blocks, it should work:
let deap;
if ('notify' == "Initial") {
deap = "Activation ";
} else if ('notify' == "Update") {
deap = "Update ";
} else {
deap = "De-Activation ";
}
console.log(deap)

troubleshoot percent function in javascript

https://jsfiddle.net/kelliwilli/6bgrt7bL/128/
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100;
alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent "%!");
}
Seems to just be a simple syntax error, which is not what I thought you were getting at with the question or lack of...
Anyways alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent + "%!"); should fix it. You simply forgot to concat the last variable.
Look into template literals, they're super handy for throwing together strings with many variables, along with the fact I find them to be awesome syntax wise.
Template literal form:
alert(`There has been ${drawPercent}% of draws. I won ${AliPercent}%, and ${p1} won ${p1Percent}%!});
https://jsfiddle.net/kelliwilli/6bgrt7bL/224/
//percent funtion
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100);
alert("There has been " + drawPercent + "% of draws. Ali won " + AliPercent + "%, and " + p1 + " won " + p1Percent + "% of the games! Thanks for playing!!!");
}
There was some code out of sequence, calling a function before it was defined, and an "if" loop that should have been a "while".
Simple fixes. :)
This fiddle link works!

Generate random equations with random numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to generate random equations in JavaScriptp and then output them into an HTML tag.
This is the code:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
function getRandomNumber(results) {
var rollDie = getRandomizer( 1, 10 );
for ( var i = 0; i < 3; i++ ) {
results += rollDie() + "";
}
getRandomNumber.result = results;
}
function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) {
var num_7,
num_6,
num_5,
num_4,
num_3,
num_2,
num_1
getRandomNumber(num1).result = num_7;
getRandomNumber(num2).result = num_6;
getRandomNumber(num3).result = num_5;
getRandomNumber(num4).result = num_4;
getRandomNumber(num5).result = num_3;
getRandomNumber(num6).result = num_2;
getRandomNumber(num7).result = num_1;
var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x",
equation2 = "" + num_1 + " x " + num_2 + " = y",
equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3,
equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x",
equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7,
equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7,
equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5
var values = [
// there is an easier way to do this, too lazy
"" + equation1,
"" + equation2,
"" + equation3,
"" + equation4,
"" + equation5,
"" + equation6,
"" + equation7
]
var i = 0;
var e;
if (i > values.length) {
i = 0;
}
var randomEquation = values[i];
i++;
e = values[i];
this.output = randomEquation;
this.e = e;
}
function getEquation() {
var bl1,
bl2,
bl3,
bl4,
bl5,
bl6,
bl7,
equationOutput;
var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e;
getEquation.equation = eq;
}
function change() {
var final = getEquation().equation;
document.getElementById("generate").innerHTML = final;
}
</script>
</body>
</html>
But it dosen't work. Any help?
P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.
This code is a complete mess. I dont know where it comes from, but definitely not Javascript.
Try the following instead:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
function getRandomNumber() {
var results="";
for ( var i = 0; i < 3; i++ ) {
results += getRandomizer( 1, 10 );
}
return results;
}
function getRandomEquation() {
var num_7 = getRandomNumber(),
num_6 = getRandomNumber(),
num_5 = getRandomNumber(),
num_4 = getRandomNumber(),
num_3 = getRandomNumber(),
num_2 = getRandomNumber(),
num_1 = getRandomNumber();
var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
equation2 = num_1+" x "+num_2+" = y",
equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5
var randomEquation = [
equation1,
equation2,
equation3,
equation4,
equation5,
equation6,
equation7
]
return randomEquation.join("<br>");
}
function change() {
document.getElementById("generate").innerHTML = getRandomEquation();
}
</script>
</body>
</html>
Well, you could do this:
var type = (Math.floor(Math.random(4));
var ret = 0;
var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))];
if (type == 0) ret = n[0] + n[1]
else if (type == 1) ret = Math.abs(n[0] - n[1])
else if (type == 2) ret = n[0] * n[1];
else if (type == 3) ret = n[0] / n[1]
else ret = n[0] / 5 % n[1]
// do something with ret
It's fully expandable, just edit n and the if statements

I have an error and for some reason the numbers are being changed and not the names?

This is the question that I need help on.
I need to have the names change randomly. I also need the names to be the same for both selection.
If you notice, it is possible to have the two random names be the same. I made some error and I need to fix this so that the first and second names are unique. I wanted to pass the first name generated to the getoption function of the second name so that second name cannot be this name. This is what i am having trouble with can someone please help me?
So far I have the code to make the numbers change but I'm not sure how to randomize the names
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney + ". Mary buys a " + marystuff + " for $" + maryitem + " and John buys a " + +" for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);
Try this, it works for me:
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoptions(s, ch) {
var a = s.split(ch);
a.sort( function() { return 0.5 - Math.random() } );
return a;
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var stuff = "notebook,pencil,ruler,pen,eraser,binder,backpack";
var options = getoptions(stuff, ',');
var marystuff = options[0];
var johnstuff = options[1];
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney +
". Mary buys a " + marystuff + " for $" + maryitem +
" and John buys a " + johnstuff + " for $" + johnitem +
". They have $" + totalleft + ".";
document.write(str);
You can add the following to make sure the item names are not the same:
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
You are also missing johnstuff from your str at the end.
Try running the code below to see that it works:
var person1, person2;
function getPeople() {
var people = ['Mary', 'John', 'Misa', 'Steve', 'Amy', 'David'];
person1 = people[Math.floor(Math.random() * people.length)];
person2 = people[Math.floor(Math.random() * people.length)];
while (person1 === person2) {
person2 = people[Math.floor(Math.random() * people.length)];
}
}
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random());
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
getPeople();
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = person1 + " had $" + marymoney + " and " + person2 + " had $" + johnmoney + ". " + person1 + " buys a " + marystuff + " for $" + maryitem + " and " + person2 + " buys a " + johnstuff + " for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);

Categories