Why does my calculation function not run properly? - javascript

I need to create a BAC calculator in JavaScript. When I run the code, the form validation works but the calculate function doesn't seem to run.
function validateForm() {
let weight = document.getElementById("weight").value;
let beer = document.getElementById("beer").value;
let wine = document.getElementById("wine").value;
let liquor = document.getElementById("liquor").value;
let hours = document.getElementById("hours").value;
if (weight <= 0){
alert("Weight must be more than zero")
return false;
}
else if (beer<0 || wine<0 || liquor<0 || hours<0){
alert("Number must be zero or higher")
return false;
}
else if(isNaN(weight)||isNaN(beer)||isNaN(wine)||isNaN(liquor)||isNaN(hours)){
alert("Answer must be a number")
return false;
}
else if (weight == null||beer == null||wine == null||liquor == null||hours == null){
alert("All fields must be filled")
return false;
}
else{
return calculate;
}
}
function calculate() {
let weight = document.getElementById("weight").value;
let beer = document.getElementById("beer").value;
let wine = document.getElementById("wine").value;
let liquor = document.getElementById("liquor").value;
let hours = document.getElementById("hours").value;
let answer = (((((beer * 0.54)+(wine*0.6)+(liquor*0.6))*7.5)/weight)-(hours*0.015)).toFixed(3)
document.getElementById("bac").value = answer;
document.getElementById("answer").append(answer);
return answer;
}
If anyone has any answers I'd be more than happy. Thank you.

In the validateForm()-method, try return calculate();

Related

how do i modify the get number function so that in the next attempt i will get new number

"use strict"
function getNumber(){
return (Math.floor(Math.random()*100+1));
}
var compNumber = getNumber();
var chances = 0;
let playAgain = false;
do {
playAgain = false;
var userNumber = prompt("Enter your number.");
userNumber = Number(userNumber);
if(userNumber>compNumber){
console.log("your number is greater.");
}
else if(userNumber<compNumber){
console.log("your number is lesser.");
}
else{
console.log(`congratulations you guessed it right and your score is ${100-chances}`);
playAgain = confirm("do you want to play again ?");
if(playAgain){
getNumber();
chances = 0;
}
}
chances++
}while(userNumber!== compNumber && chances<=100 || playAgain);
now this program giving me same random number after i confirm to play again but i wanted another random number how could i achieve this
"use strict"
function getNumber(){
return (Math.floor(Math.random()*100+1));
}
var compNumber = getNumber();
var chances = 0;
let playAgain = false;
do {
playAgain = false;
var userNumber = prompt("Enter your number.");
userNumber = Number(userNumber);
if(userNumber>compNumber){
console.log("your number is greater.");
}
else if(userNumber<compNumber){
console.log("your number is lesser.");
}
else{
console.log(`congratulations you guessed it right and your score is ${100-chances}`);
playAgain = confirm("do you want to play again ?");
if(playAgain){
compNumber = getNumber();
chances = 0;
}
}
chances++
}while(userNumber!== compNumber && chances<=100);

how I can cancel the value of the entire instruction

let mood = 'sleepy';
let tirednessLevel = 6;
if (mood === 'sleepy' || tirednessLevel > 8) {
console.log('time to sleep');
} else {
console.log('not bed time yet');
}
How I can cancel the value of the entire instruction with! ??
Need More Info about what you want but this may help you to know
let mood = 'sleepy';
let tirednessLevel = 6;
function check() {
if (mood=== 'sleepy' || tirednessLevel > 8){
return 'time to sleep';
} else{
return 'not bed time yet';
}
return '';
}
console.log(check())

Google Scripts - Using a drop-down menu to show a balance

So I have used data validation to create a drop-down menu that will then update some cells to show the budget amount that is remaining from another sheet. Nothing is occurring when I try to update the field.
The first check for if a checkbox is marked true all works, so I know it is entering the first if. I just can't seem to get it to enter the second if.
function onEdit(e) {
var sheet=e.range.getSheet();
var rgA1=e.range.getA1Notation();
if (sheet.getName()=="Entry") {
console.log("Entry")
if (e.value == "TRUE") {
submit();
Today();
e.range.setValue("FALSE");
}
if (rgA1=="A8" || rgA1 == "B8") {
var entry = e.source.getSheetByName("Entry");
var summary = e.source.getSheetByName("Summary");
var day = entry.getRange("B9").getValue();
var month = entry.getRange("B10").getValue();
var total = entry.getRange("B11").getValue();
var today = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yy");
//var daysRemaining = DATEIF(today, summary.getRange("D4"), "D");cell functions not allow
//var monthsRemaining = DATEIF(today, summary.getRange("D4"), "M");cell functions not allowed
console.log("Entered 2nd if")
if (e.value == summary.getRange("B59").getValue) {
var balance = summary.getRange("E59").getValue
console.log("Entered 3rd if")
}
else if (e.value == summary.getRange("B60").getValue()) {
var balance = summary.getRange("E60").getValue()
}
else if (e.value == summary.getRange("B61").getValue()) {
var balance = summary.getRange("E61").getValue()
}
else if (e.value == summary.getRange("B62").getValue()) {
var balance = summary.getRange("E62").getValue()
}
else if (e.value == summary.getRange("B63").getValue()) {
var balance = summary.getRange("E63").getValue()
}
else if (e.value == summary.getRange("B64").getValue()) {
var balance = summary.getRange("E64").getValue()
}
else if (e.value == summary.getRange("B65").getValue()) {
var balance = summary.getRange("E65").getValue()
}
else if (e.value == summary.getRange("B66").getValue()) {
var balance = summary.getRange("E66").getValue()
}
else if (e.value == summary.getRange("B67").getValue()) {
var balance = summary.getRange("E67").getValue()
}
else if (e.value == summary.getRange("B68").getValue()) {
var balance = summary.getRange("E68").getValue()
}
else if (e.value == summary.getRange("B69").getValue()) {
var balance = summary.getRange("E69").getValue()
}
else if (e.value == summary.getRange("B70").getValue()) {
var balance = summary.getRange("E70").getValue()
}
else if (e.value == summary.getRange("B71").getValue()) {
var balance = summary.getRange("E71").getValue()
}
day.setValue(balance / 56);
month.setValue(balance / 2);
total.setValue(balance);
}
}
}
Any thanks would be hugely appreciated!
EDIT 5/8/20
Fixed using Cooper's suggestions
Also here is a copy of the sheet this script is for.
That's better but you still have some problems at the end.
function onEdit(e) {
var sheet=e.range.getSheet();
var rgA1=e.range.getA1Notation();
if (sheet.getName()=="Entry") {
console.log("Entry")
if (e.value == "TRUE") {
submit();
Today();
e.range.setValue("FALSE");
}
if (rgA1=="A8" || rgA1 == "B8") {
var entry = e.source.getSheetByName("Entry");
var summary = e.source.getSheetByName("Summary");
var dA=entry.getRange('B9:B11').getValues();
var day = dA[0][0];
var month = dA[0][1];
var total = dA[0][2];
var today = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yy");
var bvs=summary.getRange('B59:B71').getValues();
var erg=summary.getRange('E59:E71');
var evs=erg.getValues();
for(var i=0;i<bvs.length;i++) {
if(e.value==bvs[i][0]) {
var balance=evs[i][0];
break;
}
}
entry.getRange('B9').setValue(balance/56);
entry.getRange('B10').setValue(balance/2);
entry.getRange('B11').setValue(balance);
//day.setValue(balance / 56);//day is not a range so there is no setValue Method
//month.setValue(balance / 2);//day is not a range so there is no setValue Method
//total.setValue(balance);//day is not a range so there is no setValue Method
}
}
}

Am i missing (not covering) any cases in this code (JavaScript)?

function Scholarship(input){
let income = input.shift();
let avgGrade = input.shift();
let minSalary = input.shift();
let isExcellent = false;
let isSocial = false;
let socialSch = 0 ;
let excellentSch = 0 ;
if (avgGrade >= 5.50) {
isExcellent = true;
excellentSch = avgGrade * 25;
}
if (income <= minSalary && avgGrade >= 4.50) {
isSocial = true;
socialSch = minSalary * 0.35;
}
if ((isSocial == false) && (isExcellent == false)){
console.log("You cannot get a scholarship!");
} else if (excellentSch >= socialSch){
console.log(`You get a scholarship for excellent results ${Math.floor(excellentSch)} BGN`);
} else if (socialSch > excellentSch){
console.log(`You get a Social scholarship ${Math.floor(socialSch)} BGN`);
}
}
Scholarship([number, number, number]);
All input numbers(data) are positive.
There should be a mistake somewhere in my logic!
Maybe i am missing to check for some other cases.

JS can't get my true/false function to display the false output only the true output

I need to write a good that will validate if a number is in between a range of two numbers I can only get the number is valid output no matter what i input in my text box.
function validInputRange(min, max, textbox) {
if (textbox >= min && textbox <= max) {
return true;
} else {
return false;
}
}
function btnValidate_onclick() {
// assign textbox elements to variables for easier access
var numberTextbox = document.getElementById("txtNumber");
var input = parseFloat(numberTextbox.value);
var output = validInputRange(1, 49, numberTextbox.value);
if (output = true) {
var answer = "valid";
numberTextbox.style.backgroundColor = "#00ff00";
} else {
answer = "false";
numberTextbox.style.backgroundColor = "#ff0000";
}
numberTextbox.value = answer;
}
Instead of
if (output = true)
Just do
if (output)
or
if (output == true)
= is used for assignment, while == or === for comparing.

Categories

Resources