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 3 years ago.
Improve this question
I am trying to define an array inside of a loop so that it defines an item in the array as it increments. I am also trying to set the array item to another array item. However, I've tried multiple formats and I keep getting Unexpected Token [
var filename = [];
var i = 0;
for (span.innerHTML = ""; i < files.length; i++) {
span.innerHTML += files[i].name + " <input type='text' name='" + type + i + "' placeholder='Display Name' onchange='aupdate(undefined)'>" + "<br>";
var filename[i] = files[i].name;
}
You don't need to declare a new variable - and variables names cannot contain [ or ] (only the alphabet, numbers, _ and $):
var filename = [];
var i = 0;
for (span.innerHTML = ""; i < files.length; i++) {
span.innerHTML += files[i].name + " <input type='text' name='" + type + i + "' placeholder='Display Name' onchange='aupdate(undefined)'>" + "<br>";
filename[i] = files[i].name;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to
Create json object
Declare the products array in a json object
Create five json object in array
Each json object contains following keys
p_id
p_name
p_cost
However, I'm not getting the output result.
var data = {
"products": [{
"p_id": 111,
"p_name": "p_one",
"p_cost": 100
}];
};
var results = "";
for (var i = 0, i < data.products.length) {
var obj = data.products[i];
var _pid = obj.p_id;
var _pname = obj.p_name;
var _cost = obj.p_cost;
results = _pid + "." + _pname + "." + _pcost + "<br>";
}
console.log(results);
JavaScript 101, check your console for errors, there are a few:
extra ; inside json
undefined _pcost which should be _cost
unfinished/incorrect for loop: ; instead of , inside for loops in JavaScript, + the iteration counter missing
var data = {
"products": [{
"p_id": 111,
"p_name": "p_one",
"p_cost": 100
}]
};
var results = "";
for (var i = 0; i < data.products.length; i++) {
var obj = data.products[i];
var _pid = obj.p_id;
var _pname = obj.p_name;
var _cost = obj.p_cost;
results = _pid + "." + _pname + "." + _cost + "<br>";
}
console.log(results)
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 7 years ago.
Improve this question
Code for Length of string to be displayed in front of actual string.
Example
length: 11, "This string"
Thank you.
var str = 'Hello World';
console.log('length: ' + str.length + ', "' + str + '"');
Evaluates to:
length: 11, "Hello World"
Solution :
var str = "This string";
var n = str.length;
var displaystring = 'length: '+ n + ', "' + str + '"'
//display the above string however you want.
//example below to show in a control name demo
document.getElementById("demo").innerHTML = displaystring;
I agree with #pointy that its not clear what you really need done but this might give you some idea
var str1 = "This string";
var str2 = str1.length;
var result = str2.concat(str1);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
cut the string from last '/' to '.html'
I have a string like that
"/Views/GS/stockView.html"
My need the name "stockView"
How can I cut the name from a string?
Thanks.
a = "/Views/GS/stockView.html";
a.split('/').pop().split(".")[0];
Demo
use indexOf() and lastIndexOf() method, like
var str = "/Views/GS/stockView.html";
var slashPos = str.lastIndexOf('/');
var dotPos = str.indexOf('.', slashPos + 1);
var result = str.substring(slashPos + 1, dotPos);
Try using RegExp:
var view = function(str) {
return str.match(/\/(\w*)\./)[1];//finds a word between `/` and `.`
};
console.log(view("/Views/GS/stockView.html"));
console.log(view("/Views/fs/inventView.html"));
console.log(view("/Views/fs/p1/showView.jsp"));
console.log(view("/Views/fs/p2/showView123.aspx"));
Open console
Try this
var msg = "/Views/GS/stockView.html";
var startIndex = -1;
var endIndex=-1;
var length = msg.length;
for (var i = length - 1; i >= 0; i--) {
if (msg[i] == '/'){
startIndex=i+1;
break;
}
if(msg[i]==".")
endIndex=i;
}
console.log(msg.substr(startIndex,endIndex-startIndex));
Or try this
var msg = "/Views/GS/stockView.html";
var split=msg.split("/");
split=split[split.length-1].split(".");
console.log(split[0]);
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 a small script that should detect duplicate elements in an array of fields on a form.
function dupes() {
var unique = [];
//Loop through array of fields to get entered values
for (i = 0; i <= 9; i++) {
unique[i] = Number(document.getElementById('proj' + i).value);
}
unique.sort();
//Now compare the array values. If there are any duplicates, throw an error
for (i = 1; i <= 9; i++) {
if (unique[i] == unique[i - 1]) {
document.getElementById('errormsg').innerHTML = 'duplicated values!';
return false;
}
}
}
There are ten of these "proj" fields (proj0 - proj9), and I have an onClick event assigned to call this function. If there are any duplicate values, the span 'errormsg' is supposed to display an error, but it's not working. What might I be missing?
//Check for duplicate project numbers
function errorCheck() {
var unique = [];
//Loop through array of fields to get entered values
for (i = 0; i <= 9; i++) {
var currentValue = Number(document.getElementById('projNo' + i).value);
if(unique.indexOf(currentValue)!=-1)
{
document.getElementById('projError').innerHTML = 'duplicated values!';
return false;
}
unique[i]=currentValue;
}
return true;
}
FiddleDEMO
It first checks whether a value is already in the array by using unique.indexOf(currentValue). This function returns the index of the searched element and returns -1 if it is not found.
If it was not found, it adds it to the array and goes to the next one.
Edit:
If you want to reset the error message when you submit again and there are no more duplicates, don't forget to reset it before return true; like so:
document.getElementById('projError').innerHTML = 'no duplicates ;)';
This will detect the duplicate values :
var arr= [];
//Loop through array of fields to get entered values
for (i = 0; i <= 9; i++) {
arr.push(Number(document.getElementById('proj' + i).value));
}
function dupes(arr) { // pass the array to find dupes
var i, len=arr.length, unique= [], obj={};
for (i=0;i<len;i++) {
obj[arr[i]]=0;
}
for (i in obj) {
unique.push(i);
}
if(unique.length != arr.length) {
document.getElementById('errormsg').innerHTML = 'duplicated values!';
}
}
dupes(arr);
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
When I click the roll button, nothing happens. Is there a way to show the results using document.getElementByID("results").innerHTML, or is this not recommended?
HTML
<p>How many dice?
<br/>
<input type="text" id="numDice" />
</p>
<p>How many sides per die?
<br/>
<input type="text" id="numSides" />
</p>
<button onclick="diceRoll()" id="roll"/>Roll!</button>
<p id="results"></p>
JavaScript
var numDice = document.getElementByID(numDice).innerHTML;
var numSides = document.getElementByID(numSides).innerHTML;
function diceRoll() {
var results = "";
for (var i = 0; i < numDice; i++) {
results += (Math.random() * numSides) + 1;
}
document.getElementByID("results").innerHTML = results;
}
CodePen
Typo, it's not
getElementByID
but
getElementById
The case is important, and the arguments passed are strings, so they should be quoted
function diceRoll() {
var numDice = document.getElementById('numDice').value;
var numSides = document.getElementById('numSides').value;
var results = "";
for (var i = 0; i < numDice; i++) {
results += (Math.round(results + (Math.random() * numSides) + 1)).toString();
}
document.getElementById('results').innerHTML = results;
}
FIDDLE
You are calling document.getElementByID(numDice).innerHTML before the DOM is ready. The element does not exist, so this will throw an error.
You want to get the values each time diceRoll() is called, so that you get the values the user entered. numDice will not automatically update when the value changes.
P.S. It's getElementById, and you want to use .value() for <input>s.
function diceRoll() {
var results = "";
var numDice = parseInt(document.getElementById('numDice').value, 10);
var numSides = parseInt(document.getElementById('numSides').value, 10);
for (var i = 0; i < numDice; i++) {
results += (Math.random() * numSides) + 1;
}
document.getElementById("results").innerHTML = results;
}