jdubya Posted April 30, 2017 Share Posted April 30, 2017 Hi, Im trying to Validate the numbers a user inputs, but im having trouble getting it to do both intergers and floats in the same validation, currently i can do the numbers between 3-7 but i want to add the decimal number 1.5 to this list, below is my code can anyone help? var input_gradeValue = parseFloat(prompt("Please Enter your grade for: " + input_coursecode)); while((input_gradeValue < 3) || (input_gradeValue > 7) || (input_gradeValue == "1.5")) { alert("The grade you entered is not valid, Please Try Again!"); var input_gradeValue = parseFloat(prompt("Please Enter your grade for: " + input_coursecode)); } gradeValueArray.push(input_gradeValue); Link to comment Share on other sites More sharing options...
dsonesuk Posted April 30, 2017 Share Posted April 30, 2017 Don't quite understand your request? (input_gradeValue < 3 || input_gradeValue > 7) on its own will check int and float values, if you wish to check if 1.5 is entered then try ((input_gradeValue < 3 || input_gradeValue > 7) && input_gradeValue !== "1.5") Link to comment Share on other sites More sharing options...
jdubya Posted April 30, 2017 Author Share Posted April 30, 2017 (edited) Sorry for the confusing, so if the user inputs a grade value between 3-7 its a valid number and then the code moves on, but i now want to only add in the decimal number 1.5 to the valid number list. I just added in your code change but it still came up invalid. Any help would be great @dsonesuk Edited April 30, 2017 by jdubya Link to comment Share on other sites More sharing options...
dsonesuk Posted April 30, 2017 Share Posted April 30, 2017 Broken down to simplest form gradeValueArray = []; input_coursecode = "#223456" var input_gradeValue = prompt("Please Enter your grade for: " + input_coursecode); while ((input_gradeValue < 3 || input_gradeValue > 7) && input_gradeValue !== "1.5") { alert("The grade you entered is not valid, Please Try Again!"); input_gradeValue = prompt("Please Enter your grade for: " + input_coursecode); } gradeValueArray.push(input_gradeValue); alert("YAHOO it passed, number of values in array: "+gradeValueArray.length); Link to comment Share on other sites More sharing options...
jdubya Posted April 30, 2017 Author Share Posted April 30, 2017 aHHHH You are a life saver thank you so much! and with your table help earlier too. Really appreiate it @dsonesuk Link to comment Share on other sites More sharing options...
jdubya Posted April 30, 2017 Author Share Posted April 30, 2017 Hi @dsonesuk when i added your correction to my code it threw out all of my entrys, i started to get undefined on the course codes at the end and my GPA calculator at the end started to show all sorted of random numbers below is the whole code to give you a better understand any thoughts? <script> var coursecode =["ACC1101","ACC3118","ACC5202","ACC5213","ACC5216","ACC5502","ACC8000","ACC8003","ACC8105","AGR2302","AGR3303","AGR3304","AGR3305","AGR4305","ANT1001","ANT2005","ANT2008","ANT3006","BCA3000","BCA4000","BCA8003","BIO1101","BIO1103","BIO1104","BIO1203","BIO1810","BIO2106","BIO2107","BIO2108","BIO2118","BIO2119","BIO3101","BIO3102","BIO3107","CDS1000","CHE1110","CIS1000","CIS2000","CIS2002","CIS3001","CIS3002","CIS3003","CIS3008","CIS5100","CIS5200","CIS5302","CIS5308","CIS8000","CIS8004","CIS8008","CIS8009","CIS8100","CIS8501","CIV1500","CIV2605","CIV2701","CIV3505","CIV3506","CIV3906","CIV4508","CIV5705","CLI1110","CLI3301","CMS1000","CMS1010","CMS1100","CMS2017","CMS2019","CSC1401","CSC1402","CSC2402","CSC2408","CSC3400","CSC3403","CSC3407","CSC3412","CSC3420","CSC3600","CSC8004","CSC8407","CSC8416","CSC8419","CSC8422","CSC8500","CSC8503","CSC8507","CSC8512","CSC8600","CWR1001","CWR2001","DCA9000","DCA9001","DCA9500","DCA9501","EAP7307","EAP7308","ECO1000","EDC1100","EDC1400","EDC2100","EDC2400","EDC3100","EDC4000","EDE2101","EDE3103","EDG3000","EDG5000","EDH1150","EDH2151","EDH2152","EDH3155","EDM8004","EDP2111","EDP4130","EDP4140","EDP4200","EDR8000","EDR8060","EDR8061","EDS2401","EDS3450","EDS4250","EDS4401","EDX1170","EDX1250","EDX2190","EDX2260","EDX3270","EDX3280","ELE1301","ELE1502","ELE2303","ELE2601","ELE2702","ELE2912","ELE3105","ELE3305","ELE3803","ELE3804","ELE3807","ELE3914","ELE4605","ELE4607","ELE5001","ENG1002","ENG1003","ENG1004","ENG1100","ENG1901","ENG2002","ENG3003","ENG3111","ENG4903","ENG5001","ENG8001","ENG8101","ENG8104","ENG8208","ENG8411","ENG8412","ENL1000","ENL2007","ENL3000","ENM1500","ENM1600","ENM2600","ENS7605","ENV2103","ENV2201","ENV3104","ENV4204","FIN1101","FIN2106","FIN2302","FIN3106","FIN3109","FIN5003","FIN8201","FIN1000","FIN1002","FIN1003","FIN2004","FIN2005","FIN2006","FIN3002","GIS1402","GIS3407","HEA8201","HIS1000","HIS2001","HIS8004","HMT2000","ACC1101","ACC3118","ACC5202","ACC5213","ACC5216","ACC5502","ACC8000","ACC8003","ACC8105","AGR2302","AGR3303","AGR3304","AGR3305","AGR4305","ANT1001","ANT2005","ANT2008","ANT3006","BCA3000","BCA4000","BCA8003","BIO1101","BIO1103","BIO1104","BIO1203","BIO1810","BIO2106","BIO2107","BIO2108","BIO2118","BIO2119","BIO3101","BIO3102","BIO3107","CDS1000","CHE1110","CIS1000","CIS2000","CIS2002","CIS3001","CIS3002","CIS3003","CIS3008","CIS5100","CIS5200","CIS5302","CIS5308","CIS8000","CIS8004","CIS8008","CIS8009","CIS8100","CIS8501","CIV1500","CIV2605","CIV2701","CIV3505","CIV3506","CIV3906","CIV4508","CIV5705","CLI1110","CLI3301","CMS1000","CMS1010","CMS1100","CMS2017","CMS2019","CSC1401","CSC1402","CSC2402","CSC2408","CSC3400","CSC3403","CSC3407","CSC3412","CSC3420","CSC3600","CSC8004","CSC8407","CSC8416","CSC8419","CSC8422","CSC8500","CSC8503","CSC8507","CSC8512","CSC8600","CWR1001","CWR2001","DCA9000","DCA9001","DCA9500","DCA9501","EAP7307","EAP7308","ECO1000","EDC1100","EDC1400","EDC2100","EDC2400","EDC3100","EDC4000","EDE2101","EDE3103","EDG3000","EDG5000","EDH1150","EDH2151","EDH2152","EDH3155","EDM8004","EDP2111","EDP4130","EDP4140","EDP4200","EDR8000","EDR8060","EDR8061","EDS2401","EDS3450","EDS4250","EDS4401","EDX1170","EDX1250","EDX2190","EDX2260","EDX3270","EDX3280","ELE1301","ELE1502","ELE2303","ELE2601","ELE2702","ELE2912","ELE3105","ELE3305","ELE3803","ELE3804","ELE3807","ELE3914","ELE4605","ELE4607","ELE5001","ENG1002","ENG1003","ENG1004","ENG1100","ENG1901","ENG2002","ENG3003","ENG3111","ENG4903","ENG5001","ENG8001","ENG8101","ENG8104","ENG8208","ENG8411","ENG8412","ENL1000","ENL2007","ENL3000","ENM1500","ENM1600","ENM2600","ENS7605","ENV2103","ENV2201","ENV3104","ENV4204","FIN1101","FIN2106","FIN2302","FIN3106","FIN3109","FIN5003","FIN8201","FIN1000","FIN1002","FIN1003","FIN2004","FIN2005","FIN2006","FIN3002","GIS1402","GIS3407","HEA8201","HIS1000","HIS2001","HIS8004","HMT2000","HMT4005","INR1000","INR2000","INR3000","INR8010","ISE1000","ISE1001","ISE2001","ISE2002","ISE3000","JRN1010","JRN2000","JRN2001","JRN3003","LAC1001","LAC2001","LAW1101","LAW1111","LAW1112","LAW1113","LAW1114","LAW2211","LAW2212","LAW2213","LAW3130","LAW3311","LAW3312","LAW3467","LAW3469","LAW3471","LAW3473","LAW5230","LAW8001","LAW8715","MAT1000","MAT1101","MAT1102","MAT2409","MAT3201","MAT8180","MBA8000","MEC1201","MEC2101","MEC2202","MEC2402","MEC2405","MEC2901","MEC2902","MEC3102","MEC3203","MEC3302","MEC4103","MGT1000","MGT1001","MGT2001","MGT2204","MGT3001","MGT3005","MGT5000","MGT8002","MKT1001","MKT1002","MKT2013","MKT2015","MSC8001","MSC8002","MUI1001","MUI1007","MUI1008","MUI2002","MUI2003","MUI2006","MUI2020","MUI3005","MUI3010","MUI4002","NSC2500","NUR1100","NUR1102","NUR2100","NUR2200","NUR2300","NUR2499","NUR2800","NUR3010","POL1000","POL2000","PRL1002","PRL2002","PRL2003","PSY1010","PSY1020","PSY2010","PSY2020","PSY2100","PSY3010","PSY3030","PSY3060","REN1201","REN2200","RSH8000","RSH8001","SCI1001","SCI4405","SES1299","SES2101","SES2102","SES2103","SES2104","SES3101","SES3102","SES3204","SES8003","SES8005","SES8008","SES8299","SOC1000","SPE3005","SPE3009","STA2300","STA2301","STA3300","STA8005","SVY1102","SVY1901","SVY2105","SVY2106","SVY2301","SVY2302","SVY2902","SVY3202","SVY4309","THE1001","THE1021","THE1022","THE2003","THE2006","THE2009","THE2011","THE3011","URP1001","URP2001","URP4002","VSA1002","VSA1004","VSA2000","VSA2002","VSA2004","VSA3004","VSA3021","HMT4005","INR1000","INR2000","INR3000","INR8010","ISE1000","ISE1001","ISE2001","ISE2002","ISE3000","JRN1010","JRN2000","JRN2001","JRN3003","LAC1001","LAC2001","LAW1101","LAW1111","LAW1112","LAW1113","LAW1114","LAW2211","LAW2212","LAW2213","LAW3130","LAW3311","LAW3312","LAW3467","LAW3469","LAW3471","LAW3473","LAW5230","LAW8001","LAW8715","MAT1000","MAT1101","MAT1102","MAT2409","MAT3201","MAT8180","MBA8000","MEC1201","MEC2101","MEC2202","MEC2402","MEC2405","MEC2901","MEC2902","MEC3102","MEC3203","MEC3302","MEC4103","MGT1000","MGT1001","MGT2001","MGT2204","MGT3001","MGT3005","MGT5000","MGT8002","MKT1001","MKT1002","MKT2013","MKT2015","MSC8001","MSC8002","MUI1001","MUI1007","MUI1008","MUI2002","MUI2003","MUI2006","MUI2020","MUI3005","MUI3010","MUI4002","NSC2500","NUR1100","NUR1102","NUR2100","NUR2200","NUR2300","NUR2499","NUR2800","NUR3010","POL1000","POL2000","PRL1002","PRL2002","PRL2003","PSY1010","PSY1020","PSY2010","PSY2020","PSY2100","PSY3010","PSY3030","PSY3060","REN1201","REN2200","RSH8000","RSH8001","SCI1001","SCI4405","SES1299","SES2101","SES2102","SES2103","SES2104","SES3101","SES3102","SES3204","SES8003","SES8005","SES8008","SES8299","SOC1000","SPE3005","SPE3009","STA2300","STA2301","STA3300","STA8005","SVY1102","SVY1901","SVY2105","SVY2106","SVY2301","SVY2302","SVY2902","SVY3202","SVY4309","THE1001","THE1021","THE1022","THE2003","THE2006","THE2009","THE2011","THE3011","URP1001","URP2001","URP4002","VSA1002","VSA1004","VSA2000","VSA2002","VSA2004","VSA3004","VSA3021"]; var courseCodeArray = []; var gradeValueArray = []; //CONFIRMATION FOR USER TO USE THE CALCULATOR var askUsertoUseCalculator = confirm("Would you Like to Calculate Your GPA?"); //THE CODE WILL END AND DISPLAY STATISTICS WINDOW WITH INPUTS DISPLAYING NILL if (askUsertoUseCalculator == false) { document.writeln('<table border="1">'); document.writeln('<tr>'); document.writeln('<td>' + "No Course Added" +'</td>'); document.writeln('<td>' + "No Grade Added" +'</td>'); document.writeln('</tr>'); document.writeln('</table>'); document.writeln("<h1>Statistics</h1>"); document.write("Number of courses completed: " + "0" + "</br>"); document.write("GPA : " + "0") } // BELOW CODE WILL RUN THE CALCULATOR PROGRAM IF THE USER SAYS THEYD LIKE TO CONTINUE else { //COURSE CODE INPUT AND VALIDATION var input_coursecode = prompt("Please enter your Course Code?"); var a = coursecode.indexOf(input_coursecode); while(a == -1) { alert("The code you entered is not a valid Course Code, Please Try Again"); var input_coursecode = prompt("Please enter your Course Code?"); var a = coursecode.indexOf(input_coursecode); } courseCodeArray.push(input_coursecode); //GRADE VALUE INPUT AND VALIDATION var input_gradeValue = parseInt(prompt("Please Enter your grade for: " + input_coursecode)); while (input_gradeValue < 3 || input_gradeValue > 7) { alert("The grade you entered is not valid, Please Try Again!"); var input_gradeValue = parseInt(prompt("Please Enter your grade for: " + input_coursecode)); } gradeValueArray.push(input_gradeValue); var askUser = confirm("Would you like to add another Course?"); while(askUser == true) { var input_coursecode = prompt("Please enter your Course Code?"); var a = coursecode.indexOf(input_coursecode); while(a == -1) { alert("The Code you entered is not a valid Course Code, Please Try Again"); var input_coursecode = prompt("Please enter your Course Code?"); var a = coursecode.indexOf(input_coursecode); } courseCodeArray.push(input_coursecode); var input_gradeValue = parseInt(prompt("Please Enter your grade for: " + input_coursecode)); while (input_gradeValue < 3 || input_gradeValue > 7) { alert("The grade you entered is not valid, Please Try Again!"); var input_gradeValue = parseInt(prompt("Please Enter your grade for: " + input_coursecode)); } gradeValueArray.push(input_gradeValue); var askUser = confirm("Would You Like to add Another Course?"); } var lengthofCourseCode = courseCodeArray.length; //BELOW WILL LIST THE INPUTS OF FROM THE USER IN A TABLE ABOVE STATISTICS for (var i = 0; i < courseCodeArray.length; i++) { document.writeln('<table border="1">'); document.writeln('<tr>'); document.writeln('<td>' +courseCodeArray +'</td>'); document.writeln('<td>' +gradeValueArray +'</td>'); document.writeln('</tr>'); document.writeln('</table>'); } document.writeln("<h2>Statistics</h2>") //CALCULATIONS FOR STATISTICS OF THE USERS INPUTS document.write("Number of courses completed: " + lengthofCourseCode + "</br>"); //HIGHEST GRADE STAT CALCULATION document.write("Course with the Highest Grade: " +courseCodeArray[gradeValueArray.indexOf(Math.max.apply(null,gradeValueArray))] + " with a Grade of " + Math.max.apply(null,gradeValueArray) + "</br>"); //LOWEST GRADE STAT CALCULATION document.write("Course with the Lowest Grade: " +courseCodeArray[gradeValueArray.indexOf(Math.min.apply(null,gradeValueArray))] + " with a Grade of " + Math.min.apply(null,gradeValueArray) + "</br>"); //GPA CALCULATOR var sumofGradeValue = 0; for(var i=0, n=gradeValueArray.length; i < n; i++) { sumofGradeValue += gradeValueArray; } var actualGPA = sumofGradeValue/lengthofCourseCode; var rounded = Math.round( actualGPA * 10 ) / 10; document.write("GPA : " + rounded + "</br>"); } </script> Link to comment Share on other sites More sharing options...
dsonesuk Posted April 30, 2017 Share Posted April 30, 2017 (edited) You are listing all values in arrays, +courseCodeArray + not singular item in array +courseCodeArray[i] + that what increment of variable i does Edited April 30, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now