Why is my loop not working in JavaScript? [closed] - javascript

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 2 years ago.
Improve this question
Why is my loop not working in JavaScript?
for (n = 1; n<4; n++)
{
var my_buttons = "";
my_buttons = my_buttons + `<div class="click" id="color${n}"></div>`;
document.getElementById("clicky").innerHTML = my_buttons;
}

You have two problems:
First you were overwriting my_buttons by declaring it within the loop. Second you were adding that overwritten variable to innerHTML within the loop also, so the content never got saved together.
my_buttons = "";
for (let n = 1; n<4; n++)
{
my_buttons = my_buttons + `<div class="click" id="color${n}"></div>`;
}
document.getElementById("clicky").innerHTML = my_buttons;

Related

Remove a given character from a string without using replace()? [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 3 days ago.
Improve this question
Is this the right way to remove a given character from String?
////not using replace method
function removeChar(str1, s) {
let tempĀ  = str1.split('')
let temp2 = []
for (i = 0; i < temp.length; i++) {
if (temp[i] != s) {
temp2.push(temp[i])
}
}
console.log(temp2.join(''));
}
removeChar("Hello","l")
You can do something like this
function removeChar(str1, s) {
return str1.split(s).join('')
}
this is another solution without replace()
const removeChar = (word, letter) => word.split("").filter(v => v !== letter).join("");
Just use the inbuilt replace function of javascript.
newString = oldString.replace(characterToBeReplaced, '');

How do I get results? [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 1 year ago.
Improve this question
I am a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()

Use array length to push items in an array [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 2 years ago.
Improve this question
I have an items array with specific length depending on how many items in an order there are.
I have to calculate the packaging for it, for every 2 items I use the large box and the single item goes in a small box.
So if there are 5 items its 2 large boxes and 1 small box.
How do I do this using the array length? or any other way?
Use the modulo operator:
let numBigBoxes = 0;
let numSmallBoxes = 0;
if (items.length === 1) {
numSmallBoxes = 1;
} else if (items.length % 2 === 0) {
numBigBoxes = items.length / 2;
} else {
numBigBoxes = (items.length - 1) / 2;
numSmallBoxes = 1;
}

Increment all values of [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 3 years ago.
Improve this question
I have that variable. var count_br = "4,7,10"
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
/// Increment here each values of count_br
// I want to get 8,11,14
//for the next loop I want to get 12,15,18
//etc
}
Each time if i > 0 I want to increment by 4 all the values of count_br.
How could i do to increment by 4 all the values .
thanks.
var count_br = "4,7,10";
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
for(let idx in count_) count_[idx] = (parseInt(count_[idx])+4).toString()
}
console.log(count_)
you should take the length of the variable... in this case 4... less 1 is 3 and use it on a for to go trougth all the positions of you array and for each iteration you add 4

How to declare a json object or array collection with variable size in javascript [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 need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

Categories