Javascript "variable" is not defined [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've written a script that takes grades from a user for different units and then adds them to an array, shows the grades and also an average.
When I run the script it works fine, but instead of an average, it displays 'grade' is not defined.
var subjects = ["CF:", "DaD:", "PoP:", "N&CS:", "SAD:", "APP:"];
var grades = ["", "", "", "", "", ""];
var result;
grades[0] = prompt("Please enter your marks for CF: ");
grades[1] = prompt("Please enter your marks for DaD: ");
grades[2] = prompt("Please enter your marks for PoP: ");
grades[3] = prompt("Please enter your marks for N&CS: ");
grades[4] = prompt("Please enter your marks for SAD: ");
grades[5] = prompt("Please enter your marks for APP: ");
console.log("Units and grades are: ");
console.log(subjects[0] +"\t"+ grades[0] );
console.log(subjects[1] +"\t"+ grades[1] );
console.log(subjects[2] +"\t"+ grades[2] );
console.log(subjects[3] +"\t"+ grades[3] );
console.log(subjects[4] +"\t"+ grades[4] );
console.log(subjects[5] +"\t"+ grades[5] );
result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] +
grade[5];
console.log("Average: " + "\t" + result / 6);
anyone have any ideas?
Apologies for the bad javascript, i'm very new to this.

In this code,
result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grade[5];
grade[5] must be grades[5]. You missed "s" point.

Related

"Missing ) after..." on a line, Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I am creating a "Silly Story Generator" in Javascript and after fixing a few errors that popped up I encountered "SyntaxError: missing ) after argument list"
After reading more about it I learned that it occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.
I checked my code and I cannot seem to find the mistake, string on line 38 looks okay.
Thank you.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
Your Code is Badly formatted.
At newStory.contentString.replace("insertX", "insertY", "insertZ";)
You had a semi-colon inside the the parenthesis.
You are also missing two curly braces near the end.
I suggest getting a good IDE or using the formatting features that come with the one you use.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
you have written a semicolon before the closing parentheses
newStory.contentString.replace("insertX", "insertY", "insertZ");

Javascript: Split with more than one parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there a function in Java Script that divide a string with several arguments?
var string = "This is a test text, again, this is a testing text";
For example, I can split with , by saying string.split(','); and I will have:
var string = ["This is a test text", "again", "this is a testing text"];
Now, I want to split it with several parameters, so string now would be
var string = ["test text", "testing text"]
I'm looking for a function that extract all the parts that start with test and end with text.
I'm not sure that I understood what you want, but here is a function I wrote in 2 minutes. With the following scenario
var string = "This is a test text, again, this is a testing text";
function customSplit(str_to_split, start_string, end_string) {
var res = [];
var start_index, end_index;
for (i = 0; i <= str_to_split.length; i++) {
start_index = str_to_split.toLowerCase().indexOf(start_string.toLowerCase(), i);
if (i == start_index) {
end_index = str_to_split.toLowerCase().indexOf(end_string.toLowerCase(), i);
if (end_index >= 0) {
res.push(str_to_split.substring(start_index, end_index + end_string.length));
}
}
}
return res;
}
console.log(customSplit(string, "test", "text"));
it will output ["test text", "testing text"].
Let me know if it helps you.
EDIT
Corrected a wrong behave with a particular string. Please remind that I wrote it in a couple of minutes.
Use a regex:
var str = "This is a test text, again, this is a testing text";
console.log(str.match(/test.+?text/g));

Get values of Input[type=(type)] from specific class with jQuery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this code that does not work:
kw _class = _keyword1;
var text = $("'input." + kw_class + "[type=text]'").val();
var val = $("'input." + kw_class + "[type=hidden]'").val();
Firefox console comes out with this:
`Syntax error, unrecognized expression: 'input._keyword1[type=text]'
I have tried at least three combos of this that are not working that I found from other questions.
Yes because you have ' ' inside of the selector. It should be:
var text = $("input." + kw_class + "[type=text]").val();
var val = $("input." + kw_class + "[type=hidden]").val();
you have extra '' in the selector
var text = $('input.' + kw_class + '[type=text]').val();
var val = $('input.' + kw_class + '[type=hidden]').val();

Jquery/Javascript Issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to query/javascript and having a problem with the following code to calculate a gross value and tax amount based
on the net amount the user enters. The user will enter a double amount and the gross and vat amounts are also defined as doubles.
Can anyone help? I get an error: "Uncaught SyntaxError: Unexpected number" when i try running the following code.
$('#netPayment').change(calcLowerVatRateAndGrossAmount);
/* $('#netPayment').change(function(){
calcLowerVatRateAndGrossAmount();
}); */
});
function calcVatRateAndGrossAmount(){
var netPayment = parseFloat($('#netPayment').val());
var vatAmount = 00.0;
var VatRate = 20.0;
var grossPayment = 0.00;
var totalPaymentAmount = 0.00;
if (netPayment !== '') {
vatAmount = (netPayment * VatRate) / 100;
grossPayment = (netPayment - vatAmount);
$('#vatAmount').val(parseFloat(vatAmount.data).toFixed(2));
$('#grossPayment').val(parseFloat(grossPayment.data).toFixed(2));
} else {
$('#vatAmount').val(vatAmount.amountNull);
$('#grossPayment').val(grossPayment.amountNull);
}
};
So you calculate a number here
vatAmount = (netPayment * VatRate) / 100;
And in here, you treat vatAmount as an object that has a key data
$('#vatAmount').val(parseFloat(vatAmount.data).toFixed(2));
You should just be using the variable. A simple test
console.log("variable itself: ", vatAmount);
console.log("key data: ", vatAmount.data);
So you would need to just do
$('#vatAmount').val(vatAmount.toFixed(2));
$('#grossPayment').val(grossPayment.toFixed(2));
You do the same thing with grossPayment and you reference some other property vatAmount.amountNull
$('#vatAmount').val(vatAmount.amountNull);
$('#grossPayment').val(grossPayment.amountNull);
should be
$('#vatAmount').val(""); //or any error message
$('#grossPayment').val("");

Display Images in an Array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array likes this
<script>
var myFruit = new Array("Apple", "orange", "banana", "plum", "pear");
var myindex = prompt("enter 0 to 4","");
document.write(myFruit[myindex]);
</script>
It will prompt a window and user enter a number then get result.
Now I don't want a prompt, I want to display the fruit in order like: My 1 fruit is an Aplle.
How can I write a code so that when I enter 1, then it will show: My 1 fruit is an (image) Apple. When I enter 2, it will show: My 2 fruit is an (image) orange....
I don't know how to get the index changed, then it will show both index and value simultaneously.
Please give me a hand . Thanks
------------------------------------------------------
From the help of user2782160, I have another concern below
I have 5 images put in an array likes this
<script>
var myDegree = new Array("30.gif", "60.gif", "90.gif", "120.gif", "150.gif");
.........
</script>
I need to write code somehow to have
<div id="degreeCount"> 30</div><div>//image 30.gif has to show inside this div</div>
I mean 30.gif will show next to the number 30.
Please give me a hand. Thank you!
Just concatenate the strings:
var myindex = parseInt(prompt("enter 0 to 4", ""), 10);
document.write ('My ' + myindex + ' fruit is an ' + myFruit[myindex]);
something like this i guess?
for(i = 0; i<myFruit.length; i++){
document.write("My "+i+" fruit is an <img src='URL HERE MINUS FRUIT NAME'"+myFruit[i]+".jpg>");
}

Categories