Inequalities not displaying right output, JS - javascript

I am trying to make a simple final grade calculator and am having some issues with the output of the inequalities. It consists of 4 grades each given a double value. They are then multiplied by their respected weight and put into a series of inequalities to see which grade the participant would receive. Unfortunately, I am getting the incorrect letter value even though the double value is correct. I assume it's my inequalities, but I can't figure out whats wrong with them. Any help is greatly appreciated!
I didn't want to include all of the code because it's a lot, but they all correspond to their individual drop-down menus like this one.
<form name="GPA">
<select name="demoSelect" onchange="showData2()">
<option value="zilch">Select Quarter 1:</option>
<option id="4.0" >A</option>
<option id="3.5" >B+</option>
<option id="3.0" >B</option>
<option id="2.5" >C+</option>
<option id="2.0" >C</option>
<option id="1.5" >D</option>
<option id="0.0" >F</option>
</select>
</form>
End of Form
function showData() {
var theSelect = GPA.demoSelect;
var firstGrade = document.getElementById('firstGrade');
firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade = theSelect[theSelect.selectedIndex].id;
var finalGrade = parseFloat(grade);
var theSelect = GPA2.demoSelect2;
var secondGrade = document.getElementById('secondGrade');
secondGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade2 = theSelect[theSelect.selectedIndex].id;
var finalGrade2 = parseFloat(grade2);
var theSelect = GPA3.demoSelect3;
var secondGrade = document.getElementById('thirdGrade');
thirdGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade3 = theSelect[theSelect.selectedIndex].id;
var finalGrade3 = parseFloat(grade3);
var theSelect = GPA4.demoSelect4;
var forthGrade = document.getElementById('forthGrade');
forthGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade4 = theSelect[theSelect.selectedIndex].id;
var finalGrade4 = parseFloat(grade4);
var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));
if (FinalCalc >= 3.75) {
document.getElementById("output2").innerHTML = "A";
} else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
document.getElementById("output2").innerHTML = "B+";
} else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
document.getElementById("output2").innerHTML = "B";
} else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
document.getElementById("output2").innerHTML = "C+";
} else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
document.getElementById("output2").innerHTML = "C";
} else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
document.getElementById("output2").innerHTML = "D";
} else if (FinalCalc < 1.25) {
document.getElementById("output2").innerHTML = "F";
}
document.getElementById("output").innerHTML = FinalCalc;
}

The partial you have provided works fine:
function showData() {
var theSelect = GPA.demoSelect;
var firstGrade = document.getElementById('firstGrade');
firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade = theSelect[theSelect.selectedIndex].id;
var finalGrade = parseFloat(grade);
var FinalCalc = finalGrade * 0.2
//var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));
if (FinalCalc >= 3.75) {
document.getElementById("output2").innerHTML = "A";
} else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
document.getElementById("output2").innerHTML = "B+";
} else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
document.getElementById("output2").innerHTML = "B";
} else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
document.getElementById("output2").innerHTML = "C+";
} else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
document.getElementById("output2").innerHTML = "C";
} else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
document.getElementById("output2").innerHTML = "D";
} else if (FinalCalc < 1.25) {
document.getElementById("output2").innerHTML = "F";
}
document.getElementById("output").innerHTML = FinalCalc;
}
Check it out here: http://jsfiddle.net/wjvkg8q9/15/
Note that your function is called showData(), and the onchange event calls showData2()
Is it possible that you are editing wrong duplicated function in the rest of your code?
P.S: I have commented the other grade variables in the fiddle, but as you've said they correspond.

Related

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.

Check Persian Melli code in jQuery

How can I check Persian Melli code in jQuery
I have this code in c# language and work properly but when I converted this to jQuery didn't work.
I wrote this code but does not work :
function checkCodeMeli(obj) {
var code = obj.value;
var L = code.length;
if (L < 8 || parseInt(code, 10) == 0) {
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
return false;
}
code = ('0000' + code).substr(L + 4 - 10);
if (parseInt(code.substr(3, 6), 10) == 0) return false;
var c = parseInt(code.substr(9, 1), 10);
var s = 0;
for (var i = 0; i < 9; i++)
s += parseInt(code.substr(i, 1), 10 - 1) * (10 - i);
s = s % 11;
var t = (s < 2 && c == s) || (s >= 2 && c == (11 - s))
;
if (t == true)
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
else
$('#' + obj.id).css("background-color", "#fff").css("border", "1px solid gray");
return t;
return true;
}
Hi amir you can use this code to check Iranian national code:
function checkCodeMeli(obj) {
var input=obj.value;
if (!/^\d{10}$/.test(input))
{
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
return false;
}
var check = parseInt(input[9]);
var sum = 0;
var i;
for (i = 0; i < 9; ++i) {
sum += parseInt(input[i]) * (10 - i);
}
sum %= 11;
var isValid= (sum < 2 && check == sum) || (sum >= 2 && check + sum == 11);
if(!isValid){
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
}else{
$('#' + obj.id).css("background-color", "#38d043").css("border", "1px solid black");
}
return isValid;}
in JavaScript
console.log("1377456935 valid Code: "+checkMelliCode('1377456935'));
console.log("1377456938 Invalid Code: "+checkMelliCode('1377456938'));
function checkMelliCode(meli_code) {
if (meli_code.length == 10) {
if (meli_code == '1111111111' || meli_code == '0000000000' || meli_code == '2222222222' || meli_code == '3333333333' || meli_code == '4444444444' || meli_code == '5555555555' || meli_code == '6666666666' || meli_code == '7777777777' || meli_code == '8888888888' || meli_code == '9999999999') {
return false;
}
c = parseInt(meli_code.charAt(9));
n = parseInt(meli_code.charAt(0)) * 10 + parseInt(meli_code.charAt(1)) * 9 + parseInt(meli_code.charAt(2)) * 8 + parseInt(meli_code.charAt(3)) * 7 + parseInt(meli_code.charAt(4)) * 6 + parseInt(meli_code.charAt(5)) * 5 + parseInt(meli_code.charAt(6)) * 4 + parseInt(meli_code.charAt(7)) * 3 + parseInt(meli_code.charAt(8)) * 2;
r = n - parseInt(n / 11) * 11;
if ((r == 0 && r == c) || (r == 1 && c == 1) || (r > 1 && c == 11 - r)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
in C#
public bool IsValidNationalCode(string nationalcode)
{
if (string.IsNullOrEmpty(nationalcode)) return false;
if (!new Regex(#"\d{10}").IsMatch(nationalcode)) return false;
var array = nationalcode.ToCharArray();
var allDigitEqual = new[] { "0000000000", "1111111111", "2222222222", "3333333333", "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999" };
if (allDigitEqual.Contains(nationalcode)) return false;
var j = 10;
var sum = 0;
for (var i = 0; i < array.Length - 1; i++)
{
sum += Int32.Parse(array[i].ToString(CultureInfo.InvariantCulture)) * j;
j--;
}
var div = sum / 11;
var r = div * 11;
var diff = Math.Abs(sum - r);
if (diff <= 2)
{
return diff == Int32.Parse(array[9].ToString(CultureInfo.InvariantCulture));
}
var temp = Math.Abs(diff - 11);
return temp == Int32.Parse(array[9].ToString(CultureInfo.InvariantCulture));
}
Check this code (JavaScript)
console.log("1377456935 valid Code: "+checkMeliCode('1377456935'));
console.log("1377456938 Invalid Code: "+checkMeliCode('1377456938'));
function checkMeliCode(code) {
if (!/^\d{8,10}$/.test(code) || /^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/.test(code))
return false;
var L = code.length,
_ = 0;
for (i = 0; i < L - 1; i++)
_ += code.charAt(i) * (L - i);
_ %= 11;
return (code.charAt(L - 1) == ((_ < 2) ? _ : 11 - _))
}
This is solved my problem :
function checkCodeMeli(obj) {
var code = obj.value;
var L = code.length;
if (L < 8 || parseInt(code, 10) == 0) {
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
return false;
}
code = ('0000' + code).substr(L + 4 - 10);
if (parseInt(code.substr(3, 6), 10) == 0) return false;
var c = parseInt(code.substr(9, 1), 10);
var s = 0;
for (var i = 0; i < 9; i++)
s += parseInt(code.substr(i, 1), 10) * (10 - i);
s = s % 11;
var t = (s < 2 && c == s) || (s >= 2 && c == (11 - s));
if (t == false)
$('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
else
$('#' + obj.id).css("background-color", "#fff").css("border", "1px solid gray");
return t;
return true;
}
Try Persian-tools an awesome javascript library for this matter and also many other useful functionalities.
import { verifyIranianNationalId, getPlaceByIranNationalId } from "persian-tools2";
verifyIranianNationalId("0067749828"); // true
verifyIranianNationalId("0684159415"); // false

Change text depending on the time of day and on which button is clicked

I need to display different text 'set' depending on which button I click, and rotate messages in this 'set' depending on what time it is.
This is what I've got:
var date = new Date;
var hh = date.getHours();
var mm = date.getMinutes();
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
var t = hh+":"+mm;
var themessage;
function TimeF() {
if (check == "alt") {
if (t >= "08:00" && t <= "09:00"){
themessage = "A";
}
else if(t >= "09:05" && t <= "10:05"){
themessage = 2;
}
}
else if (check == "NoPack") {
if (t >= "08:00" && t <= "09:14"){
themessage = "NP";
}
else if(t >= "09:19" && t <= "10:33"){
themessage = 2;
}
}
else if (check == "pack") {
if (t >= "08:00" && t <= "09:14") {
themessage = "pack";
}
}
else if (n == 3) {
if (t >= "08:00" && t <= "09:14"){
themessage = "n=3";
}
else if(t >= "09:19" && t <= "10:33"){
themessage = 2;
}
}
else {
if (t >= "08:00" && t <= "09:19") {
themessage = "Else";
}
else if (t >= "09:21" && t<= "10:43"){
themessage =2;
}
}
$('.Test').append(themessage);
}
Also I have button code:
var reg = document.getElementById("Reg");
var pack = document.getElementById("Pack");
var nopack = document.getElementById("NoPack");
var alt = document.getElementById("Alt");
var sch = document.getElementById("Sch");
var check;
var update;
$(document).ready(function () {
$("#Pack").click(function () {
$("#Sch").html("pack");
check = "pack";
});
$("#Reg").click(function () {
$("#Sch").html("Reg");
check = "reg";
});
$("#NoPack").click(function () {
$("#Sch").html("NoPack");
check = "NoPack";
});
$("#Alt").click(function () {
$("#Sch").html("Alternate");
check = "alt";
});
})
The problem is - when I press any button, the code shows the text for 'esle', and not what I supposed to be assigned to that button.
P.S I have jquery refference and I am loading this function TimeF in body.
If you want, I could make a quick CodePen with this.
P.S.S n==3 in my code - is checking if it is Wednesday.

JQuery Change Event in function with for loop

<script type="text/javascript">
function total(){
var count = jQuery("[id^=frm_section_430]").length;
var Total = 0;
for(var i=0; i < count; i++){
var date = jQuery("#field_h1tvhv").val();
var splitdate = date.split('/');
var start = jQuery("#field_qgd9fa-" + i).val(); //eg "09:20 PM"
var end = jQuery("#field_701ehm-" + i).val(); //eg "10:00 PM"
//start time conversion to 24hr
var Starthours = Number(start.match(/^(\d+)/)[1]);
var Startminutes = Number(start.match(/:(\d+)/)[1]);
var StartAMPM = start.match(/\s(.*)$/)[1];
if (Startminutes == 15){
Startminutes = 25;
}else if(Startminutes == 30){
Startminutes = 50;
}else if(Startminutes == 45){
Startminutes = 75;
}
if (StartAMPM == "PM" && Starthours < 12) Starthours = Starthours + 12;
if (StartAMPM == "AM" && Starthours == 12) Starthours = Starthours - 12;
var sHoursStart = Starthours.toString();
var sMinutesStart = Startminutes.toString();
if (Starthours < 10) sHoursStart = sHoursStart ;
if (Startminutes > 10) sMinutesStart = sMinutesStart;
//End time conversion to 24hr
var Endhours = Number(end.match(/^(\d+)/)[1]);
var Endminutes = Number(end.match(/:(\d+)/)[1]);
var EndAMPM = end.match(/\s(.*)$/)[1];
if (Endminutes == 15){
Endminutes = 25;
}else if(Endminutes == 30){
Endminutes = 50;
}else if(Endminutes == 45){
Endminutes = 75;
}
if (EndAMPM == "PM" && Endhours < 12) Endhours = Endhours + 12;
if (EndAMPM == "AM" && Endhours == 12) Endhours = Endhours - 12;
var sHoursEnd = Endhours.toString();
var sMinutesEnd = Endminutes.toString();
if (Endhours < 10) sHoursEnd = sHoursEnd;
if (Endminutes > 10) sMinutesEnd = sMinutesEnd;
var hrStart = sHoursStart + "." + sMinutesStart;
var hrEnd = sHoursEnd + "." + sMinutesEnd;
if((hrEnd - hrStart) >= 0){
Total += hrEnd - hrStart;
jQuery("#field_deb1y1").val(Total.toFixed(2));
}else{
alert("Start value cannot be less than end value. Please try again!");
jQuery("#field_qgd9fa-" + i).val("06:00 AM");
jQuery("#field_701ehm-" + i).val("06:00 AM");
}
}
return Total;
}
jQuery(document).ready(function() {
jQuery("#field_qgd9fa-0").val("06:00 AM");
jQuery("#field_701ehm-0").val("06:00 AM");
//Script in original questioning
//jQuery("[id^=field_qgd9fa-], [id^=field_701ehm-]").change(function () {
//total();
//});
jQuery(document).on("change", "[id^=field_qgd9fa-], [id^=field_701ehm-]", function () {
total();
});
jQuery("#frm_checkbox_429-").hide();
jQuery("#field_fvb50h-").hide();
});
</script>
The change event only occurs on the first select box. I do notice that the function is totaling when I change the next select boxes, but only if I go back and play with the first select box. What am I doing wrong?

How to use AJAX or JSON in this code?

I am creating a website application that allows users to select a seat, if it is not already reserved, and reserve it.
I have created a very round about way of getting the seats that are previously reserved using iFrames, however that was temporarily, now I need to make it secure and "proper javascript code" using proper practices. I have no clue what AJAX (or JSON) is, nor how to add it to this code, but it needs to get the file "seatsReserved"+this.id(that is the date)+"Que.html" and compare the string of previously reserved seats to see which class to make the element. If this is horrible, or if any of the other things could work better, I am open to criticism to everything. Thank you all!
Here is the javascript code:
A little side note, all of the if statements are due to different amount of seats in each row
<script>
var i = " 0 ";
var counter = 0;
var leng=0;
document.getElementById("Show1").addEventListener("click", changeDay);
document.getElementById("Show2").addEventListener("click", changeDay);
document.getElementById("Show3").addEventListener("click", changeDay);
function changeDay() {
var iFrame = document.getElementById("seatList");
iFrame.src = "seatsReserved" + this.id + "Que.html";
document.getElementById('date').innerHTML = this.id;
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.removeEventListener("click", selectedSeat);
}
else {
document.getElementById(let +k).className = "openseat";
document.getElementById(let +k).removeEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
}
function loadChanges() {
var iFrame = document.getElementById("seatList");
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
var leng = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.addEventListener("click", selectedSeat);
seat.className = "openseat";
}
else {
document.getElementById(let +k).className = "notAvailible";
document.getElementById(let +k).addEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
i = " 0 ";
counter = 0;
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
document.getElementById("seatnums").innerHTML = counter;
}
i = document.getElementById("seatString").innerHTML;
counter = document.getElementById("seatnums").innerHTML;
function selectedSeat() {
var w = this.id;
var l = (" " + w);
var b = (" " + w + " ");
if (counter < 5) {
if (i.indexOf(b) <= 0) {
this.className = "closedseat";
i = i + b;
i = i.replace(" 0 ", " ");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter + 1;
document.getElementById("seatnums").innerHTML = counter;
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
function doNothing() {
}
var rannum = Math.random() * 1000;
document.getElementById('getConfirmation').value = rannum;
</script>

Categories

Resources