Implement function array - javascript

I have a code for a loto which allows us to find and compare number in a array using loops.I need to update my program so that the work of checking the result is done
by a function called checkNumber. This function should take the customer
number and the array of winning numbers as arguments. The customer number
should be returned from a function called getCustomerNumber. The array of
winning numbers should be returned from a function called
getWinningNumbers. The display of the results should be done by a function
called displayResult(). The whole process should be kicked off by a function
called init. Thank you for your help!

x = checkNumber(12, 17, 24, 37, 38, 43);
function checkNumber() {
for (var i = 0; i < winningNumbers.length && !match ; i++)
if (winningNumbers[i] == customerNumbers) {
match = true;
}
}
}
function getWinningNumbers
function displayResult(){
var message="This week Winning numbers are:" + "\n" + "\n" + winningNumbers + "\n" + "\n" + "The customer's Number is:" + "\n" + "\n" + customerNumbers + "\n" + "\n" + "We have a match and a winner!";
alert(message)
} Else
var message="This week Winning numbers are:" + "\n" + "\n" + winningNumbers + "\n" + "\n" + "The customer's Number is:" + "\n" + "\n" + customerNumbers + "\n" + "\n" + "Sorry you are not a winner this week";
alert(message)
}
}

Related

JavaScript conditional statements if/else if/else

Hello guys! Could you please help me out? I am trying to use certain conditions but they seem to be ignored for some reasons. When I ran the code the popped-up random number given was 93 that fitted in the first declared statement (if), but, It got ignored and moved to the last statement even when true && true.
I do not understand why...
???
function loveMatching (name1, name2) {
name1 = prompt ("Enter your name!");
name2 = prompt ("Enter your crush name!");
if (matchingPercentage() >= 70 && matchingPercentage() <=100) {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. You guys are meant to be together!");
}
else if( matchingPercentage() >=30 && matchingPercentage() <70) {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. Too close to fail!");
}
else {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchingPercentage() + "%. You better look in another direction!");
}
}
function matchingPercentage() {
var n = Math.random();
var n = Math.floor(n * 100) + 1;
return n;
}
loveMatching();
you're calculating a new match % everytime you check it, multiple times in the same conditional. you need to just do it once at the start:
function loveMatching (name1, name2) {
name1 = prompt ("Enter your name!");
name2 = prompt ("Enter your crush name!");
const matchPercent = matchingPercentage(); // call one time
if (matchPercent >= 70 && matchPercent <=100) {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. You guys are meant to be together!");
}
else if( matchPercent >=30 && matchPercent <70) {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. Too close to fail!");
}
else {
document.write(" The compability between: " + name1 + " and " + name2 + " is of a " + matchPercent + "%. You better look in another direction!");
}
}

GoogleSheets script to sendEmail upon form submission, how should I handle a formatting error for a blank cell?

Simple call logger/lead sheet generator using Google Forms and a script to send out the lead upon submission. I'm getting a format error when the user does not input a date into the field. I don't want to require an appt. time/date in the form. Here is my code:
function sendEmails() {
//gets list of
var leadss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LatestLeadSheet");
var leadList = leadss.getRange(3, 1, leadss.getLastRow() - 2, leadss.getLastColumn()).getValues();
var timeZone = Session.getScriptTimeZone();
var today = Utilities.formatDate(new Date(), timeZone, "MM/dd/yyyy"); //Today's date for email subject line
var fullMessage = ""; //clears fullMessage variable
// Creates body of email. Looping through each array from LatestLeadSheet tab
for (var j = 0; j < leadList.length; j++) {
var message = "Client Name: " + leadList[j][1] + "\n" +
"Client Phone: " + leadList[j][3] + "\n" +
"Client Email: " + leadList[j][8] + "\n" +
"Interested in: " + leadList[j][5] + "\n" +
"Quoted rate of: " + leadList[j][6] + "\n" +
"Notes: " + leadList[j][7] + "\n" +
"Appointment Set for: " + Utilities.formatDate(leadList[j][10], timeZone, "HH:mm MM/dd/yyyy") + "\n"
//+"At: "+Utilities.formatDate(leadList[j][11],timeZone,"h:mm a") + "\n\n"
+
"Referral Source: " + leadList[j][2] + "\n" +
"Other Info: " + leadList[j][4] + "\n" +
"Documents were sent (Y/N): " + leadList[j][9] + "\n" +
"Call was taken at: " + Utilities.formatDate(leadList[j][0], timeZone, "HH:mm MM/dd/yyyy") + "\n" +
"Call was logged by: " + leadList[j][12] + "\n\n\n\n";
var fullMessage = fullMessage + message;
}
//Sends email to each recipient listed on SalesTeam tab
var subject = "DMS Call Sheet from: " + today;
var emailss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesTeam");
var lr = emailss.getLastRow();
for (var i = 2; i <= lr; i++) {
var currentEmail = emailss.getRange(i, 2).getValue();
MailApp.sendEmail(currentEmail, subject, "Please follow up with the following potential clients: \n\n" + fullMessage)
}
}

how do i get a new number to generate

i need to make a dice rolling program that rolls dice and says who won if it is a tie it rolls again and after each win you earn a point first to 5 wins the game whenever i run mine it uses the same numbers over and over again because it only generated the once how can i fix this and what else do i need to do after this issue to finish the program, thanks for the help!
<script>
var comp1 = Math.floor((Math.random()*6) + 1);
var comp2 = Math.floor((Math.random()*6) + 1);
var you1 = Math.floor((Math.random()*6) + 1);
var you2 = Math.floor((Math.random()*6) + 1);
var counter = 1;
var youPoints = 0;
var mePoints = 0;
while(counter < 6)
{{
alert("Let's shake some dice!")
alert("your turn to roll \n\n you shook a " + you1 + " and a " + you2 + ", so you have " + (you1 + you2));
alert("my turn to roll \n\n I shook a " + comp1 + " and a " + comp2 + ", so I have " + (comp1 + comp2));
counter++
var you = you1 + you2;
var me = comp1 + comp2;
if(you > me)
{
alert("you win " + you + " to " + me);
youPoints++
}
if (me > you)
{
alert("I win " + me + " to " + you);
mePoints++
}
}}
</script>
You're initializing your random variables (you1, you2) outside the while loop.
It's being initialized only once and hence producing the same number every time.
Bring it inside the loop, and that might fix it!
Move the code the generates the random numbers to inside of the loop because, right now, they only generate once... before the loop even starts.
Also, do yourself a favor and use a for counting loop, rather than a while, because while loops are easily misconfigured to cause infinite loops to occur.
var youPoints = 0;
var mePoints = 0;
for(var counter = 1; counter < 6; counter++){
// The code that generates the random numbers has to be in the loop
// in order for new randoms to be generated upon each loop iteration
var comp1 = Math.floor((Math.random()*6) + 1);
var comp2 = Math.floor((Math.random()*6) + 1);
var you1 = Math.floor((Math.random()*6) + 1);
var you2 = Math.floor((Math.random()*6) + 1);
alert("Let's shake some dice!")
alert("your turn to roll \n\n you shook a " + you1 + " and a " + you2 + ", so you have " + (you1 + you2));
alert("my turn to roll \n\n I shook a " + comp1 + " and a " + comp2 + ", so I have " + (comp1 + comp2));
var you = you1 + you2;
var me = comp1 + comp2;
// Don't make two separate if statements. Use one with an else if
if(you > me) {
alert("you win " + you + " to " + me);
youPoints++
} else if (me > you) {
alert("I win " + me + " to " + you);
mePoints++
}
}
Here you go, this should be a complete working example.
Note: I replaced alert() for console.log() so we can see the output here in the console and without popups, but it will work either way.
var compPoints = 0;
var youPoints = 0;
var winnerOfFive = false;
function rollTheDice() {
return Math.floor((Math.random()*6) + 1);
}
function rollAllDice() {
let you1 = rollTheDice();
let you2 = rollTheDice();
let comp1 = rollTheDice();
let comp2 = rollTheDice();
console.log("your turn to roll \n\n you shook a " + you1 + " and a " + you2 + ", so you have " + (you1 + you2));
console.log("my turn to roll \n\n I shook a " + comp1 + " and a " + comp2 + ", so I have " + (comp1 + comp2));
var you = you1 + you2;
var me = comp1 + comp2;
if(you > me) {
console.log("you win " + you + " to " + me);
youPoints++;
} else if(me > you) {
console.log("I win " + me + " to " + you);
compPoints++;
} else {
console.log("It was a tie, no one wins. Re-rolling...");
rollAllDice();
}
}
function startGame() {
while( !winnerOfFive ) {
console.log("Let's shake some dice!")
rollAllDice();
if(compPoints == 5) {
console.log("Comp is first to 5 games and wins " + compPoints + " to " + youPoints);
winnerOfFive = true;
} else if (youPoints == 5) {
console.log("You are first to 5 games and win " + youPoints + " to " + compPoints);
winnerOfFive = true;
}
}
}
// Start the game like this
startGame();

While loop and outputting math operators

I'm trying to create a while loop that outputs a list of values, adds them together, and outputs a total with an "=" sign at the end
I.e., if the User enters the numbers 5 and 12, the output would show up like this:
5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 = total
This is how my 'while' loop looks right now:
while(enteredNum1 <= enteredNum2){
total += enteredNum1;
document.write(enteredNum1 + " + ");
enteredNum1++;
}
document.write("= " + total);
I know it will always append the " + ", but can anyone point me in the right direction for having the last value show "=" instead of the "+" again?
Alternative;
var numbers = [];
while(enteredNum1 <= enteredNum2){
total += enteredNum1;
numbers.push(enteredNum1++);
}
document.write(numbers.join(" + ") + " = " + total);
Proper way:
list = [];
total = 0;
enteredNum1 = parseInt(enteredNum1); // parseInt only if enteredNum1 can be a string
while(enteredNum1 <= enteredNum2) {
total += enteredNum1;
list.push(enteredNum1);
enteredNum1++;
}
document.write(list.join(" + ") + " = " + total);
Any easy way would be an if statement checking if the loop will continue:
while(enteredNum1 <= enteredNum2) {
total += enteredNum1;
if (enteredNum1 + 1 <= enteredNum2) {
document.write(enteredNum1 + " + ");
} else {
document.write(enteredNum1);
}
enteredNum1++;
}
document.write(" = " + total);
Alternatively, saving the output as a string and taking the "+" off using substring would work as well
This should work:
total += enteredNum1;
document.write(enteredNum1);
enteredNum1++;
while(enteredNum1 <= enteredNum2){
total += enteredNum1;
document.write(" + " + enteredNum1);
enteredNum1++;
}
document.write("= " + total);

Math.Random is not so random

I am making a basic game with popup boxes.
My problem is that if you look at the goblinAttack function it will repeat till the goblin is dead but the damage is random.
For example, I hit the goblin 6 damage every time until it dies instead of it being a random number between 1 and 25 and the goblin does 4 damage on me every time it hits. Its boring. I wanted it to be random each hit but repeating the function seems to not give a random number each time.
//VARIABLES
var dmgMultiply = Math.floor(Math.random() * 10 + 1);
var dmg = 10;
var armour = 0;
var hp = 100;
//ENEMY VARIABLES
var dmgGoblin = Math.floor(Math.random() * 10 + 1);
var goblinHP = 100;
//ARRAYS
var choiceArray = ["Type 'sword' equip your sword.",
"Type 'run' if you're scared.",
"Type 'stats' to see your stats."];
var answerArray = ["sword", "run", "stats"];
var outcomeArrayOne = ["You equip your sword.",
"You run away. Game Over."];
var outcomeArrayTwo = ["Sword Equipped."]
//GAME CHOICE
function choice(a, b, c, x, y, z, aa, bb)
{
var answer = prompt(a + "\n" + b + "\n" + c);
if(answer == x)
{
alert(aa);
swordEquipped(outcomeArrayTwo[0]);
}
if(answer == y)
{
alert(bb);
}
if(answer == z)
{
displayStats();
}
}
//EQUIPPED SWORD
function swordEquipped(a)
{
dmg = 25;
dmgMultiply = Math.floor(Math.random() * 25 + 1);
alert(a + "\n" + "DMG : " + dmg);
goblinCombatStart("goblin");
}
//GOBLIN COMBAT START
function goblinCombatStart(g)
{
alert("A wild " + g + " appears!" + "\n" + "The " + g + " has 100 HP!");
goblinAttack("goblin")
}
function goblinAttack(g)
{
alert("The " + g + " swings his axe at you!" + "\n" + "The " + g + " does " + dmgGoblin + " damage to you!");
hp -= dmgGoblin;
var attack = prompt("Type 'attack' to swing your sword at the " + g + "\n" + "HP : " + hp);
if(attack == "attack")
{
alert("You swing your sword at the " + g + "\n" + "You did " + dmgMultiply + " to the " + g);
goblinHP -= dmgMultiply;
alert("The " + g + " is on " + goblinHP + "HP");
if(goblinHP < 0)
{
goblinDead();
}
if(goblinHP > 0)
{
goblinAttack("goblin");
}
}
else
{
alert("You dropped your sword an shouted " + "'" + attack + "'" + " in the goblins face. It wasn't very effective and the goblin choppped your head off");
}
}
function goblinDead()
{
alert("You have slain the puny goblin with only a few scratches!");
}
//GAME START
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
//STATISTICS
function displayStats()
{
var statCheck = confirm("HP : " + hp + "\n" +
"ARMOUR : " + armour + "\n" +
"DMG : " + dmg + "\n" +
"Press OK to continue");
if(statCheck == true)
{
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
}
}
Instead of computing the random damage value only once at the beginning of your script, compute them whenever you need them. That would probably be in your attack function.
Maybe you are misunderstand something here: Math.random doesn't return some special magical value which changes every time it's read. It returns a number and that number doesn't change.
If you want multiple random values, you have to call Math.random multiple times. If you want a different random value whenever the goblin attacks, you have to call Math.random when it attacks.
The problem here is that you're initializing your variables at the beginning as random.
That means that, on every move, it will use the same random number that was generated at the beginning.
If you want it to be random for each time, you have to call Math.random() on every move.
As it stands, dmgMultiply and dmgGoblin are computed randomly once and use the same value on each turn, so you end up getting the same amount of damage taken and dealt each time.
Declare your variables at the beginning:
var dmgMultiply;
var dmgGoblin;
Then, set them to a random number within your attack function so that it computes a random number each turn:
function goblinAttack(g)
{
dmgMultiply = Math.floor(Math.random() * 10 + 1);
dmgGoblin = Math.floor(Math.random() * 10 + 1);
...
}
Demo

Categories