Store values inside array jquery [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 3 years ago.
Improve this question
I have multiple inputs, for example:
<input id=xxx_1>
<input id=xxx_2>
I want the values from input to be stored inside an array. At the moment what I have is this but it doesn't work. I have tried to Google it and I can't find any solution. I can't understand why this does not work.
var array = [];
for (var i = 1; i >= 14; i++) {
array[i] = $(this).find("#xxx_" + i + "").val();
}
What I what is to have an array like this:
[ value, value, value,... ]
Thanks for the help

You could make use of attributs starts with selector to select all elements who id starts with xxx, then you can iterate over these elements using .each()
var array = [];
$( "[id^='xxx']" ).each(function() {
array.push(this.value);
});
console.log(array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="xxx_1" value="123">
<input id="xxx_2" value="456">

Your comparison operator in your for loop is the wrong way round, and so your code never enters it.
You probably want to look for when i is less than or equal to 14, using <= .
var array = [];
for (var i = 1; i <= 14; i++) {
array[i] = $(this).find("#xxx_" + i + "").val();
}

Related

My for loop runs only once while trying to reverse an array in 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 last year.
Improve this question
var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);
I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.
As #Barmar pointed out the problem is due to a logic error.
/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
for(let i = 0 ; i < size ; ++i)
list.push(window.prompt());
}
/* Print a list container */
function print(list){
for(let i = 0 ; i < list.length ; ++i)
console.log(list[i]);
}
var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);

JavaScript For loop ignored [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
Im trying to make a little program in js that overturns the text you typed in. The code runs fine, but the for loop gets completely ignored, it doesnt do even one block of code from inside of it.
document.getElementById("StartButtonText").addEventListener("click",function(){
var text_value = document.getElementById('TextValue').value;
console.log(text_value);
var text_length = text_value.length;
console.log(text_length);
var final_text;
var order;
for (order = 1; order >= text_length; order ++) {
final_text[order] = text_value[text_length - order + 1];
console.log('for loop log ',order);
final_text = final_text + final_text[poradie];
}
console.log('after loop log ',order);
document.getElementById("TextTurningResult").innerHTML = final_text;
console.log(final_text);
});
Any ideas why it doesnt run?
Your loop will run as long as order is greater or equals than text_length.
I guess you wanted to write:
for (order = 0; order < text_length; order ++) { ... }
You should change the condition in your for loop to
for(order = 1; order <= text_length; order++){
...
}
In your case, the condition is false at the first iteration only.

splice does not remove element from array [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 6 years ago.
Improve this question
Edit
Well, this is indeed embarrassing. Thank you all for noticing the typo, I would have deleted this question, but I can't (because it has answers). Let this be a monument.
I'm trying to write an app which, given an object and an array, removes the object from the array (if it exists), or adds it to the array (if it does not exist).
Initial array:
var permissions = [{
id: 3,
name: "Perm3"
}, {
id: 1,
name: "Perm1"
}];
Add\remove function:
triggerPermission = function(perm) {
var idx = permissions.indexOfById(perm);
if (idx === -1) {
permissions.push(perm);
} else {
permissions.slice(idx, 1); //should have been SPLICE...
}
};
helper function:
Array.prototype.indexOfById = function(obj) {
var idx = -1;
for (var i = 0; i < this.length; i++) {
if (this[i].id === obj.id) {
idx = i;
break;
}
}
return idx;
};
As can be seen on the plunkr, I'm trying to use this in an AngularJS app, with checkboxes as triggers. Some permissions are getting returned by splice (but the permissions array is not being changed), and some are not even getting returned. What am I missing here?
Your code uses slice, not splice.
slice does not modify the array, it returns a modified copy.
splice is the method you are after, which modifies the array in-place.
Exactly what meagar said. Slice will take a part of the array but leave the array untouched. Splice will actually "splice" something off.

Why does my function return undefined when trying to return the max number from an array? [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 6 years ago.
Improve this question
My function returns undefined rather than 3, I'm aware I can use:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
However, I want to first try and see if I can do so without utilizing a native function since I'm trying to improve my skills as a dev.
var my_max = function(arr){
var max=arr[0];
for (var i = 0; i < arr.length; i++){
if (arr[i] > max) {
max=arr[i];
}
}
return max;
};
console.log(my_max(1, 2, 3));
There's no array there. You're just passing three numbers to the function. You should instead pass an array of three numbers:
console.log(my_max([1, 2, 3]));
Alternatively, you can use rest parameters in your function, like that:
var my_max = function(...arr){
// ...
}
Then you can call it without using an array:
console.log(my_max(1, 2, 3));

How do I rearrange an array with similar items so that similar items will never neighbor each other? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So for example if I have this array:
var input = [1,1,2,4,6,7,7,1];
I want the output to be something like:
[1,2,1,4,6,7,1,7]
The order of the new array does not matter, as long as similar items will never (or at least as seldom as possible) neighbor each other.
I can use plain JavaScript as well as underscore.js.
Try the following:
var input = [1,1,2,4,6,7,7,1];
input.sort()
var output = [];
var len = input.length;
for (var i = 0; i < Math.floor((len / 2)); i++) {
output.push(input[i]);
output.push(input[len - i - 1]);
}
if (len % 2) {
var left_over = input[Math.floor(len / 2)];
if (left_over == output[0]) {
output.push(left_over);
} else {
output.unshift(left_over);
}
}
Or see http://jsfiddle.net/d0j3Lfa3/1.
The solution sorts the numbers then alternates high and low. It deals with an odd number of elements, including corner cases such as [1,1,2] and [1,2,2] where it needs to push the middle element differently to pass. Since the input is sorted, input order doesn't affect the output.
This answer may help simplify things a bit.

Categories

Resources