Encountering problems with JSFiddle code - error-handling

I have recently written a piece of code that checks temperature over a period of time for some course work. i wrote the program using different bits of code but cannot find the issue to some of the problems i am encountering.
I was just wondering if anyone can see any obvious errors that i am missing and could help out with some information int he right direction.
Link to the JSFiddle
//Counting the average temperatures in a day
//needs debugged!!!
var temperatures = [];
var total = 0;
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
function getNumDays() {
number = prompt("How many days in the study? Enter 1 - 10");
while ((number < 1) || (number > 10) ||( isNaN(number) === true)) {
alert ("Invalid input!");
number = prompt ("Enter again, 1 - 10 >> ");}
return number;
}
function getTemps(numDays) {
total = 0;
for (i = 0; i < numDays; i++) {
next = prompt ("Enter the temperature for day " + (i+1));
next = parseint(next);
while (isNaN(next)===true) {
next = 0;
next = prompt ("Error in input! Try again >>");
next = parseInt(next);}
temperatures.push(next);
}
total = total + next;
return temperatures;
}
function calcAverage(total, numDays) {
average = total / numDays;
return average;
}
function showStatistics(city, average, numdays) {
alert ("The average daily temperature for "+ city + " is " + average.toFixed(2) + " measured over " + numDays + " days." );
}
//main program
city = getCity();
numDays = getNumDays();
temperatures = getTemps(numDays);
Average = calcAverage(total, numDays);
showStatistics(city, average, numDays);

function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
//main program
city = getCity();
Looks like you're missing a return statement.
Additionally, the line total = total + next; seems to be out of place: I imagine the total to be the total of the temperatures, not 0 + temperatureOfLastDay.

Related

Why doesn't this While loop work as intended?

Hey guys, newbie here. One question, can't understand why this while loop doesn't work even when I entered a int bigger than 9 to the variable num, the while loop should repeat itself until the expression is false, and it doesn't, no output even. Am I missing something here? Thanks in advance.
fun main () {
while(true) {
println ("\nWrite a positive number: ")
var num = readLine()!!.toInt()
var sum = 0
if (num > 9) {
while (num > 9) {
var digit = num % 10
sum = sum + digit
num = num / 10
}
println("\nDigit Sum: $sum")
} else if (num in 1..9) {
println("\nDigit Sum for the number $num is $num")
} else {
println("\nInvalid input, try again.")
}
}
}
The issue is that you are not summing the last num when it gets less or equal to 9. You can even simplify your code a bit. Try the following:
fun main() {
while(true) {
println ("\nWrite a positive number: ")
val insertedNumber = readLine()!!.toInt()
var num = insertedNumber
var sum = 0
while (num > 9) {
val digit = num % 10
sum = sum + digit
num = num / 10
}
sum = sum + num
println("\nDigit Sum for the number $insertedNumber is $sum")
}
}
You don't need to redeclare the variables every time
var sum = sum + digit
var num = num / 10
So simply remove var
sum = sum + digit
num = num / 10

apps script: increase variable with each execution

Hi i have a list of values each value representing the output of a shift of packaging. I want to calculate the average output of 8 weeks. So each time a shift passes the average output changes. My idea is to trigger a function after each shift, which calculates the output. Now theres my problem, how do i get a varible (the one representing the row of the first value) to increase after each trigger of the function? What i tried is to declare the variable before the function and increase the variable of 1 inside the function. Buf ofc the starting value doesnt change this way.. Probably there is an easy way for this i just dont know yet (programming newbie here :)).
let i = 7;
let j = 126;
function schnitt() {
var summe = 0;
var counter = 0;
i++;
j++;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
}
I have found a work around. I just put var i and j into cells and do it like this:
function schnitt() {
var i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
var j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
var summe = 0;
var counter = 0;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
i++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).setValue(i);
j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
j++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).setValue(j);
}

Cannot add row without complete selection of batch/serial numbers

ERROR: (-4014) Cannot add row without complete selection of batch/serial numbers.
The default function of DI API SaveDraftToDocument() is working fine on MS SQL Database but not SAP HANA.
I am posting the Delivery document with Serial Numbers.
SAPbobsCOM.Documents oDrafts;
oDrafts = (SAPbobsCOM.Documents)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts);
oDrafts.GetByKey(Convert.ToInt32(EditText27.Value));
var count = oDrafts.Lines.Count;
var linenum = oDrafts.Lines.LineNum;
//Validation
#region
var RsRecordCount = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
var sQryRecordCount = String.Format("Select * from \"SANTEXDBADDON\".\"#TEMPITEMDETAILS\" where \"U_DraftNo\" = '{0}'", EditText27.Value);
RsRecordCount.DoQuery(sQryRecordCount);
#endregion
if (count == RsRecordCount.RecordCount)
{
//LINES
string ItemCode = "", WhsCode = ""; double Quantity = 0; int index = 0;
for (int i = 0; i < oDrafts.Lines.Count; i++)
{
oDrafts.Lines.SetCurrentLine(index);
ItemCode = oDrafts.Lines.ItemCode;
//SERIAL NUMBERS
var RsSerial = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
string table = "\"#TEMPSERIALS\"";
var sQrySerial = String.Format(
"Select \"U_ItemCode\" , \"U_DistNumber\" from \"SANTEXDBADDON\".\"#TEMPSERIALS\" where " +
"\"U_DraftNo\" = '{0}' and \"U_ItemCode\" = '{1}'", EditText27.Value, ItemCode);
RsSerial.DoQuery(sQrySerial);
int serialindex = 1, lineindex = 0;
#region
if (RsSerial.RecordCount > 0)
{
while (!RsSerial.EoF)
{
//OSRN SERIALS
var RsSerialOSRN = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
var sQrySerialOSRN = String.Format(
"Select * from OSRN where \"DistNumber\" = '{0}' and \"ItemCode\" = '{1}'"
, RsSerial.Fields.Item("U_DistNumber").Value.ToString(), ItemCode);
RsSerialOSRN.DoQuery(sQrySerialOSRN);
oDrafts.Lines.SerialNumbers.SetCurrentLine(0);
oDrafts.Lines.SerialNumbers.BaseLineNumber = oDrafts.Lines.LineNum;
oDrafts.Lines.SerialNumbers.SystemSerialNumber =
Convert.ToInt32(RsSerialOSRN.Fields.Item("SysNumber").Value.ToString());
oDrafts.Lines.SerialNumbers.ManufacturerSerialNumber =
RsSerialOSRN.Fields.Item("DistNumber").Value.ToString();
oDrafts.Lines.SerialNumbers.InternalSerialNumber =
RsSerialOSRN.Fields.Item("DistNumber").Value.ToString();
oDrafts.Lines.SerialNumbers.Quantity = 1;
if (RsSerial.RecordCount != serialindex)
{
Application.SBO_Application.StatusBar.SetText("INTERNAL NO " + oDrafts.Lines.SerialNumbers.InternalSerialNumber, SAPbouiCOM.BoMessageTime.bmt_Long, SAPbouiCOM.BoStatusBarMessageType.smt_Success);
oDrafts.Lines.SerialNumbers.Add();
serialindex++;
lineindex++;
}
RsSerial.MoveNext();
}
}
#endregion
index++;
}
var status = oDrafts.SaveDraftToDocument();
if (status == 0)
{
oDrafts.Remove();
Application.SBO_Application.StatusBar.SetText("Delivery Posted Successfully !", SAPbouiCOM.BoMessageTime.bmt_Long, SAPbouiCOM.BoStatusBarMessageType.smt_Success);
}
else
{
int code = 0; string message = "";
oCompany.GetLastError(out code, out message);
Application.SBO_Application.SetStatusBarMessage(message, SAPbouiCOM.BoMessageTime.bmt_Medium, true);
}
}`
The error you have posted explains the problem. You are trying to deliver products but have not included all of the serial/batch numbers.
I don't think there's enough information to be sure where this problem happens, but here are some pointers:
You are reading the serial numbers from a custom table. Are the values you are reading valid? For example, could another user have added them to a different order? Could the values be for a different product?
Are you specifying the correct quantity of serial numbers? Is it possible that the item quantity on the line is more than the number of serial numbers you are adding?
Believe the error message until you can prove it's wrong. It doesn't seem like this is a HANA issue (we use HANA extensively) it's a logical problem with the data you are providing.
You may want to capture more debugging information to help you if you can't easily identify where the problem is.

I am having issues with a repeating while loop in a switch statement, the loop continues to repeat

I'm a newbie coder and for a class I was required to make a vending machine-like code. However, I used a switch statement as part of it. An issue I faced is that in those if statements I added a check. The check was to ask if the user would like to continue or not, but for some reason it repeats even after an input. I wasn't entirely sure if this question was asked before, but a lot of my friends share the issue.
import java.util.Scanner;
public class hw52 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String choice;
String answer = "";
int cr = 0;
int wb = 0;
int ch = 0;
int k = 0;
int j = 0;
boolean check = true;
double cost2;
double discount;
double change = 0;
double cost = 0;
double money = 0;
int counter = 0;
do {
answer = "";
System.out.println("Select an option in the vending machine. \n
Crunch:$1.50 \n Water bottle:$2.00 \n Chips:$1.00 \n Ketchup:$4.00 \n
Juice:$2.50");
choice = keyboard.nextLine();
switch(choice.toUpperCase()){
case "CRUNCH":
cost += 1.5;
cr ++;
counter ++;
System.out.println("Cost: " + cost);
while(check = true) {
System.out.println("Do you want to continue? Yes or no?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {
check = false;
}else if(answer.equalsIgnoreCase("Yes")) {
check = false;
}else {
System.out.println("Bad input.");
}
}
case "WATER BOTTLE":
cost += 2.00;
wb++;
counter ++;
System.out.println("Cost: " + cost);
while(check = true) {
System.out.println("Do you want to continue? Yes or no?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {
check = false;
}else if(answer.equalsIgnoreCase("Yes")) {
check = false;
}else {
System.out.println("Bad input.");
}
}
case "CHIPS":
cost += 1.00;
ch++;
counter ++;
System.out.println("Cost: " + cost);
System.out.println("Cost: " + cost);
while(check = true) {
System.out.println("Do you want to continue? Yes or no?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {
check = false;
}else if(answer.equalsIgnoreCase("Yes")) {
check = false;
}else {
System.out.println("Bad input.");
}
}
case "KETCHUP":
cost += 5.00;
k++;
counter ++;
System.out.println("Cost: " + cost);
while(check = true) {
System.out.println("Do you want to continue? Yes or no?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {
check = false;
}else if(answer.equalsIgnoreCase("Yes")) {
check = false;
}else {
System.out.println("Bad input.");
}
}
case "JUICE":
cost += 2.50;
j++;
counter ++;
System.out.println("Cost: " + cost);
while(check = true) {
System.out.println("Do you want to continue? Yes or no?");
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {
check = false;
}else if(answer.equalsIgnoreCase("Yes")) {
check = false;
}else {
System.out.println("Bad input.");
}
}
default:
System.out.println("Not a choice! Use exact names.");
answer = "yes";
}
}while(!answer.equalsIgnoreCase("no"));
counter /= 3;
for (int i = 0; i < counter;i++) {
discount = (double)(Math.random()*.1)+.1;
cost2 = cost * discount;
cost -= cost2;
}
while(money < cost) {
System.out.println("How much money would you like to use to pay?");
money = keyboard.nextDouble();
if (money < cost) {
System.out.println("That's not enough money.");
}
}
double Q = cost / .25;
double D = (cost % .25) / .10;
double N = ((cost % .25) % .10) / .05 ;
double P = (((cost % .25) % .10) %.05) /.01;
System.out.println("The items you bought are: \n Crunch Bars: " + cr +
"\n Water Bottles: " + wb + "\n Chips: " + ch + "\n Ketchup: " + k + "\n
Juice: " + j);
System.out.println("Your change is: $" + ((Q * .25) + (D * .1) + (N *
.05) + (P*.01)));
}
}
Please excuse my messy coding style. I personally like it better, but I know a lot of people find it strange.
Your code never allows the user to input "no" into the answer variable. So your code always forces the user to intput "yes" or it shows the message "Bad Input".
You have this if statement in each one of your while loops. Both if and else if check the exact same condition. I assume you were probably wanting one of them to check for the answer "no" and the check variable never gets assigned to false unless "yes" is chosen:
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) { //this is same condition as next if line.
check = false;
}else if(answer.equalsIgnoreCase("Yes")) { //this is same condition as prior if line.
check = false;
}else {
System.out.println("Bad input.");
}
Also, this is your main issue. You are ALWAYS assigning check to true in the while loop condition. you want to use the == operator.
This assigns true to the check variable in your code:
while(check = true) {
You actually want to do this:
while(check == true) {
I recommend using an IDE with a debugger that allows you to systematically step through your code and see what is happening with each line of code as it executes. It will help you find logic problems like this one.

Asking the user to enter in numbers, getting min/max/and average

Hi guys I'm have a real hard time for some reason trying to get this code worked out. My guide lines are:
Create a new Scanner object and save it into a variable name of your choice
Declare an integer variable for the current user’s input and initialize it to something that is NOT 0 (we will call it intInput in these instructions – the name is arbitrary, though)
Create a while loop, with the condition being that intInput is not equal to 0
Inside of this loop, call the nextInt() method of your Scanner object and store the value into intInput
I'm not getting any errors but its not working the way I thought it would.
and here is my code:
import java.util.Scanner;
// class name matches the file name
public class Lab5
{
// we must have a main method to run the program
`enter code here`public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
int userInput = 1;
int minVal = Integer.MIN_VALUE;
int maxVal = Integer.MAX_VALUE;
double average = 0;
int holdNum = 0;
double numSum = 0;
System.out.print ("Please enter numbers and to finish the program your last number should be 0: ");
numSum += userInput;
holdNum++;
while (userInput != 0)
{
userInput = scan.nextInt();
}
if (maxVal > Integer.MAX_VALUE)
{
maxVal = userInput;
}
if (minVal < Integer.MIN_VALUE)
{
minVal = userInput;
}
average = ( numSum ) / ( holdNum );
System.out.println( "Average = " + average );
System.out.println( "Max = " + maxVal );
System.out.println( "Minimum = " + minVal );
}
}
You are adding the user input in the sum and incrementing the number of user inputs only when the input is less than the minimum. I would guess you'll want to do it for all inputs instead.