I'm trying to create a calculator, I've managed to get the values from a google Sheets doc when I console.log from within the function. I'm now trying to combine those figures to get a total price. I'm getting a NAN error.
I'm new to Javascript so I could be missing something simple but I have tried and tried and I can't figure it out.
I simply want to add the hourly rate and the mileage together to get 25.00 and then convert it to a UK currency.
function showHourlyRate(qty) {
var oneFunc = qty
console.log(oneFunc) //result is 5.00
return oneFunc
};
function showMileageRate(qty) {
var twoFunc = qty
console.log(twoFunc) //result is 20.00
return twoFunc
};
function combinedTotal(){
var addTogether = showHourlyRate() showMileageRate();
alert(addTogether); // gives NAN
return addTogether;
}
UPDATE:
Excuse me if my terminology is not correct.
I've updated my question as the answers still didn't work for me which make me think I've done something early on so...
This is a google apps script.
I have a functions.gs, within this file I get the spreadsheet and sheet needed. I then get the range of columns and filter the results to match my word. Once it finds a match, get the next column which has a price. I then return that.
Here's that part: (I've only added the hourly rate one as the mileage one is the same but with the names changed)
function getHourlyRate() {
const sheet = SpreadsheetApp.openById("17iaOdOS9N09tFb38w4P9GC8furzy33sXVnQK8dIhPnI").getSheetByName("Working Hours");
const data = sheet.getRange(2, 1, sheet.getLastRow()-1, 8).getValues();
const filteredData = data.filter(r => r[0] === "Sundries");
return filteredData.length === 0 ? "No Match" : filteredData.reduce((subtotal, r) => subtotal r[1], 0).toFixed(2);
}
My next file is the index.html. I first load the function on initial load, I then get the hourly rate using the function afterSidebar Loads which then uses the showHourlyRate function.
Thats how I get my 20.00 or 5.00 figure
here's the html file:
var arrayOfArrays;
function afterSidebarLoads() {
google.script.run.withSuccessHandler(showHourlyRate).getHourlyRate();
google.script.run.withSuccessHandler(showMileageRate).getMileageRate();
document.addEventListener("DOMContentLoaded", afterSidebarLoads);
}
function showHourlyRate(qty) {
var oneFunc = hourlyRate
// var ghjJhg = oneFunc.toFixed(2)
// console.log(oneFunc) //result is 0.55
return oneFunc
};
function showMileageRate(mileageRate) {
var twoFunc = mileageRate
// var ghjJhg = oneFunc.toFixed(2)
// console.log(twoFunc) //result is 22.64
return twoFunc
};
function combinedTotal(){
}
I hope this makes more sense and thank you all for answering.
CodePudding user response:
function showHourlyRate(showHourlyRate) {
// ... do some more logic if needed
return showHourlyRate
};
function showMileageRate(showMileageRate) {
// ... do some more logic if needed
return showMileageRate
};
function combinedTotal(hourlyRate,mileageRate){
// ... do some more logic if needed
return showHourlyRate(hourlyRate) showMileageRate(mileageRate);
}
console.log(combinedTotal(5,20))
CodePudding user response:
You need to add the params to the called functions on the 3rd function after if you are getting a NaN error probably because you are using strings or other type of value from the doc, you cloud use something like this
function showHourlyRate(qty) {
return parseFloat(qty)
};
function showMileageRate(qty) {
return parseFloat(qty)
};
function combinedTotal(hourlyRate, mileageRate){
let addTogether = showHourlyRate(hourlyRate) showMileageRate(mileageRate);
console.log(addTogether)
return addTogether;
}
combinedTotal(1,"22")
CodePudding user response:
You didn't specify the inputs for your showHourlyRate and showMileageRage functions.
You put:
function combinedTotal(){
var addTogether = showHourlyRate() showMileageRate();
alert(addTogether); // gives NAN
return addTogether;
}
You can have:
function combinedTotal(hourlyRate, mileageRate){
var addTogether = showHourlyRate(hourlyRate) showMileageRate(mileageRate);
alert(addTogether);
return addTogether;
}