SyntaxError: unterminated string literal in this javascript - javascript

Why am I getting this error???
function Overallrating() {
var amt = this.getField("txt5).value;
if (amt > 10.5){
this.getField("Text2").value = "EXCEPTIONAL";
}else if (amt < 11.00 && amt > 8.5){
this.getField("Text2").value = "EXCEEDS";
}else if (amt < 9.00 && amt > 6.5){
this.getField("Text2").value = "IMPROVEMENT NEEDED";
}else if (amt < 7.00 && amt > 4.50){
this.getField("Text2").value = "UNSATISFACTORY";
}else if (amt < 5.00 && amt > 2.00)
}

you wrote
var amt=this.getField("txt5).value;
you need " at the end of txt5. correct to:
var amt=this.getField("txt5").value;

Related

Simplifying multiple similar If statements (JAVASCRIPT)

Im working on a code for an adobe acrobat form, I want to add the following code:
var total =6301
var warranty
if (0 < total && total <= 3300){warranty = 194.25}
else if (3300 < total && total <= 4000){warranty = 197.5}
else if (4000 < total && total <= 5000){warranty = 202.15}
else if (5000 < total && total <= 6000){warranty = 206.75}
else if (6000 < total && total <= 7000){warranty = 211.45}
else if (7000 < total && total <= 8000){warranty = 216.1}
else if (8000 < total && total <= 9000){warranty = 220.75}
else if (9000 < total && total <= 10000){warranty = 225.4}
else if (10000 < total && total <= 11000){warranty = 230.1}
else if (11000 < total && total <= 12000){warranty = 234.75}
else if (12000 < total && total <= 13000){warranty = 239.4}
else if (13000 < total && total <= 14000){warranty = 243.95}
else if (14000 < total && total <= 15000){warranty = 248.7}
else if (15000 < total && total <= 16000){warranty = 253.3}
else if (16000 < total && total <= 17000){warranty = 258}
else if (17000 < total && total <= 18000){warranty = 262.65}
else if (18000 < total && total <= 19000){warranty = 267.3}
else if (19000 < total && total <= 20000){warranty = 271.95}
else if (20000 < total && total <= 21000){warranty = 276.6}
else if (21000 < total && total <= 22000){warranty = 281.3}
else if (22000 < total && total <= 23000){warranty = 285.9}
else if (23000 < total && total <= 24000){warranty = 290.5}
else if (24000 < total && total <= 25000){warranty = 295.25}
else if (25000 < total && total <= 26000){warranty = 299.85}
else if (26000 < total && total <= 27000){warranty = 304.55}
else if (27000 < total && total <= 28000){warranty = 309.15}
else if (28000 < total && total <= 29000){warranty = 313.85}
else {warranty = 999999}
I want to have this be a lot less repetitive as I have a lot more conditions similar to the ones above.
Thanks for the help in advance!!
This approach features an array with value pairs which works for any values.
const
getValue = total => [
[3300, 194.25], [4000, 197.5], [5000, 202.15], [6000, 206.75],
[7000, 211.45], [8000, 216.1], [9000, 220.75], [10000, 225.4],
[11000, 230.1], [12000, 234.75], [13000, 239.4], [14000, 243.95],
[15000, 248.7], [16000, 253.3], [17000, 258], [18000, 262.65],
[19000, 267.3], [20000, 271.95], [21000, 276.6], [22000, 281.3],
[23000, 285.9], [24000, 290.5], [25000, 295.25], [26000, 299.85],
[27000, 304.55], [28000, 309.15], [29000, 313.85], [Infinity, 999999]
].find(([t]) => total <= t)[1];
console.log([0, 1, 3300, 3500, 3999, 4000, 4001, 5000, 10000, 100000].map(getValue));
Use switch case instead which is ideal in terms of optimisation & performance. This way the code execution directly jumps into the corresponding condition rather than going through each if else if condition.
var total = 6301;
var warranty;
switch (true) {
case (0 < total && total <= 3300): warranty = 194.25; break;
case (3300 < total && total <= 4000): warranty = 194.25; break;
...
default: warranty = 999999; break;
}

Stuck in an Infinite Loop with Multiple If Conditions inside a loop

Can you please let me know why this code is generating an infinite loop? I have tried to limit the steps between > min ) && (i <= logic but it's still sticking in the loop.
var min = -9.00;
var max = 14.00;
for (var i = min; i < max;) {
console.log(i);
if((i > min ) && (i <= -2.00)) {i += (0.25);}
if((i > -2.00 ) && (i <= 0.00)){i += (0.5);}
if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);}
if((i > 6.00 ) && (i <= max)) {i += (0.5);}
}
Yes you have an infinite loop. Because i = -9.00 and your first if is i > min which min = -9.00 its the same not greather. just change > to >=
var min = -9.00;
var max = 14.00;
for (var i = min; i < max;) {
console.log(i);
if((i >= min ) && (i <= -2.00)) {i += (0.25);}
if((i > -2.00 ) && (i <= 0.00)){i += (0.5);}
if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);}
if((i > 6.00 ) && (i <= max)) {i += (0.5);}
}

Creating an input form with javascript

I had a question about creating a form that I can input "grades" into which also would check to see if those grades were between 0 and 25 or 0 and 100 (input validation). My code has to be in javascript but I don't know where or how to start. In other words, I need to take this code and add a "form" and check for valid input. This is what I have so far:
<!doctype html>
<html>
<script>
var a1=parseFloat(prompt("Enter the grade for assignment 1: "));
var a2=parseFloat(prompt("Enter the grade for assignment 2: "));
var a3=parseFloat(prompt("Enter the grade for assignment 3: "));
var a4=parseFloat(prompt("Enter the grade for assignment 4: "));
var mid=parseFloat(prompt("Enter the grade for the mid exam: "));
var fe=parseFloat(prompt("Enter the grade for the final exam: "));
var fp=parseFloat(prompt("Enter the grade for the final project: "));
var sum;
var grade;
var error;
sum=((a1+a2+a3+a4)/4)*(4*0.25)+(mid*0.25)+(fe*0.25)+(fp*0.25);
/* if (a1 < 0 && a1 > 25) {
error = window.prompt( "Assignments are only out of 25 points, please re-enter the integer grade:")
} */
if (sum >= 94.0) {
grade = "A";
} else if (sum <= 94.0 && sum >= 90.0) {
grade = "A-";
} else if (sum <= 90.0 && sum >= 87.0) {
grade = "B+";
} else if (sum <= 86.9 && sum >= 84.0) {
grade = "B";
} else if (sum <= 83.9 && sum >= 80.0) {
grade = "B-";
} else if (sum <= 79.9 && sum >= 77.0) {
grade = "C+";
} else if (sum <= 76.9 && sum >= 74.0) {
grade = "C";
} else if (sum <= 73.9 && sum >= 70.0) {
grade = "C-";
} else if (sum <= 69.9 && sum >= 67.0) {
grade = "D+";
} else if (sum <= 66.9 && sum >= 64.0) {
grade = "D";
} else if (sum <= 63.9 && sum >= 60.0) {
grade = "D-";
} else if (sum <= 60.0) {
grade = "F";
}
document.writeln("The final percent grade is "+sum+"%. Your grade letter is: "+grade+".");
</script>
</html>
Take the values from textboxes and give a button get Results and display the result in the output div.
Here is how I have done.
In javascript, take values from the textboxes using document.getElementById("<ID>").value and store it in a variable. Follow the same step for all the input boxes. And process the values obtained from textboxes and display it when the button is clicked.
Here is a simple code snippet which does the same thing using fields.
function myFunction()
{
var a1 = parseFloat(document.getElementById("a1").value);
var a2 = parseFloat(document.getElementById("a2").value);
var a3 = parseFloat(document.getElementById("a3").value);
var a4 = parseFloat(document.getElementById("a4").value);
var mid = parseFloat(document.getElementById("mid").value);
var fe = parseFloat(document.getElementById("fe").value);
var fp = parseFloat(document.getElementById("fp").value);
var sum=((a1+a2+a3+a4)/4)*(4*0.25)+(mid*0.25)+(fe*0.25)+(fp*0.25);
var grade;
if (sum >= 94.0) {
grade = "A";
} else if (sum <= 94.0 && sum >= 90.0) {
grade = "A-";
} else if (sum <= 90.0 && sum >= 87.0) {
grade = "B+";
} else if (sum <= 86.9 && sum >= 84.0) {
grade = "B";
} else if (sum <= 83.9 && sum >= 80.0) {
grade = "B-";
} else if (sum <= 79.9 && sum >= 77.0) {
grade = "C+";
} else if (sum <= 76.9 && sum >= 74.0) {
grade = "C";
} else if (sum <= 73.9 && sum >= 70.0) {
grade = "C-";
} else if (sum <= 69.9 && sum >= 67.0) {
grade = "D+";
} else if (sum <= 66.9 && sum >= 64.0) {
grade = "D";
} else if (sum <= 63.9 && sum >= 60.0) {
grade = "D-";
} else if (sum <= 60.0) {
grade = "F";
}
document.getElementById("result").innerHTML = ("The final percent grade is "+sum+"%. Your grade letter is: "+grade+".");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
a1: <input type="text" id="a1"><br><br>
a2: <input type="text" id="a2"><br><br>
a3: <input type="text" id="a3"><br><br>
a4: <input type="text" id="a4"><br><br>
mid: <input type="text" id="mid"><br><br>
finalexam: <input type="text" id="fe"><br><br>
finalproject: <input type="text" id="fp"><br><br>
<button onclick="myFunction()">Get Results</button><br><br>
<span id="result"></span>
</body>
</html>
Thank you.

howto convert a number to a clever number with specific length and k, m, b

for example I have this number:
1,234,567.89
And I want to convert this number to a number with a specific length like these:
1M
01M
1.2M
1.23M
1.234M
1.2346M
1.23457M
1,234,568
01,234,568
1,234,567.9
1,234,567.89
Is there any javascript function, plugin or something for doing this in such a way (with K, M, B...)?
I don't understand your downvotes because this a not a duplicated question und I found NOTHING similar. So i made it by myself and it works ;)
function number(num, size) {
var s = num;
var l1 = s.toString().length;
if (l1 > size && num > 1) s = Math.floor(num);
var l2 = s.toString().length;
if (l2 > size) {
if (num >= 1e3 && num < 1e6) {
s = num / 1e3;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'k';
} else if (num >= 1e6 && num < 1e9) {
s = num / 1e6;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'M';
} else if (num >= 1e9 && num < 1e12) {
s = num / 1e9;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'G';
} else if (num >= 1e12 && num < 1e15) {
s = num / 1e12;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'T';
} else if (num >= 1e15 && num < 1e18) {
s = num / 1e18;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'P';
} else if (num >= 1e18 && num < 1e21) {
s = num / 1e18;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'E';
} else if (num >= 1e21 && num < 1e24) {
s = num / 1e21;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'Z';
} else if (num >= 1e24 && num < 1e27) {
s = num / 1e24;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'Y';
}
}
var l3 = s.toString().length;
if (l3 > size) s = '-';
var s = s.toString();
while (s.length < size) s = s.charAt(0) == '-' ? '-' + s : '0' + s;
var startZeros = /^(0)\1+/.exec(s),
startZerosCount = (startZeros != null) ? startZeros[0].length : 0,
decimalCount = (num.toString().split('.')[1] || []).length;
if (startZerosCount >= 2 && decimalCount > 0 && s.indexOf('.') < 0) {
var decimals = num.toString().split('.')[1],
movedDigits = Math.min(startZerosCount, decimalCount),
lastChar = s.substring(s.length - 1);
if (isNaN(lastChar)) {
s = s.substring(0, s.length - 1);
s = s.substring(movedDigits) + '.' + decimals.substring(0, movedDigits - 1);
s += lastChar;
} else {
s = s.substring(movedDigits) + '.' + decimals.substring(0, movedDigits - 1);
}
}
return s;
}
The output for 784432432.9999 is:
-
--
---
784M
0784M
784.4M
784.43M
784.432M
784432432
0784432432

Javascript looping error with arrays

I am trying to prompt the user for a weight smaller than 126. Then my javascript is supposed to classify the weight into a category. I have used arrays but every time I loop it writes the 3rd array, superfly class. What can I do to make it function properly?
var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];
var weight = parseInt(prompt("What is your weight?"), 10);
while (weight > 126) {
alert('Please enter a weight lighter than 126');
weight = parseInt(prompt("What is your weight?"), 10);
}
recruit();
function recruit() {
var weightClass = wArray[0];
if (0 < weight && weight < 112) {
weightClass = wArray[1];
} else if (112 < weight && weight < 115) {
weightClass = wArray[2];
} else if (weight > 115 && weight < 118) {
weightClass = wArray[3];
} else if (weight > 118 && weight < 122) {
weightClass = wArray[4];
} else if (weight > 122 && weight < 126) {
weightClass = wArray[5];
}
document.getElementById("weight").innerHTML = ('You are in ' + weightClass + ' class!');
}
Your first if condition is incorrect. You say if (112 < weight < 115). This first does 112 < weight, then takes the result of that and compares it to 115.
112 < weight evaluates to true or false; when used in numeric comparisons, true is 1 and false is 0. (Obviously) both 1 and 0 will always be less than 115, so this condition will always be true.
Also note that this script should be run onload of the page. This is because the div with the ID weight may not have loaded when the script executes and attempts to populate it. You can do this by saying:
<script type="text/javascript">
var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];
function calculateWeight() {
var weight = parseInt(prompt("What is your weight?"), 10);
while (weight > 126) {
alert('Please enter a weight lighter than 126');
weight = parseInt(prompt("What is your weight?"), 10);
}
recruit();
}
function recruit() {
var weightClass = wArray[0];
if (weight >= 112 && weight < 115) {
weightClass = wArray[1];
} else if (weight >= 115 && weight < 118) {
weightClass = wArray[2];
} else if (weight >= 118 && weight < 122) {
weightClass = wArray[3];
} else if (weight >= 122 && weight < 126) {
weightClass = wArray[4];
}
document.getElementById("weight").innerHTML = ('You are in ' + weightClass + ' class!');
}
</script>
<body onload="calculateWeight()">
<!-- include body contents here -->
</body>
the line:
if (112 < weight < 115) {
Should be
if (112 < weight && weight < 115) {

Categories

Resources