I am trying to add onto a variable each time a loop is ran. In this loop I want each loop to add hey to hey. So that hey is added 13 times. My loops is only adding it once which is confusing me. I am only trying to get this to show up in the console at the moment. Thank you!
const repeatString = function() {
let test = 'hey';
let add = 'hey';
for (let i = 0; i < 13; i++) {
return test += add;
}
}
console.log(repeatString());
You are returning from the function in the first iteration. You should return from outside of the loop (after the completion of the loop):
const repeatString = function() {
let test = 'hey';
let add = 'hey';
for (let i = 0; i < 13; i++) {
test += add;
}
return test;
}
console.log(repeatString());
Related
I tried to repeatedly increase the value of the variable at the end of the loop and then use the increased value to produce an array, the variable value stay the same
let chunks = [];
for (let i = 0; i < 5; i++){
let start; // and then use reassigned value
if(!start) start = 0
chunks.push(start);
start += 5 // here I'm trying reassiang value
}
console.log(chunks)
// result: [0,0,0,0,0] || expected [0,5,10,15,20]
You are declaring the value start inside the loop, which is resetting its value each iteration of the loop. Move it to outside the loop:
let chunks = [];
let start;
for (let i = 0; i < 5; i++) {
if(!start) start = 0
chunks.push(start);
start += 5
}
console.log(chunks)
Just move it outside! A good explanation here https://dev.to/nicolalc/why-you-should-stop-declaring-variables-inside-a-for-loop-3npo
let chunks = [];
let start; // and then use reassigned value
for (let i = 0; i < 5; i++){
if(!start) start = 0
chunks.push(start);
start += 5 // here I'm trying reassiang value
}
console.log(chunks)
// result: [0,5,10,15,20]
You are declaring variable inside for loop. Move variable declaration outside the loop.
let chunks = [];
let start;
for (let i = 0; i < 5; i++) {
if(!start) start = 0
chunks.push(start);
start += 5 // here I'm trying reassiang value
}
console.log(chunks)
How can I run a loop 20 times for an array that have only two entries?
I have the following code
const genders = ['male', 'female'];
const randChoice = arr => {
return arr[Math.floor(Math.random() * arr.length)];
};
const loop = () =>
for (let i = ''; i <= 20; i++) {
const data = randChoice(genders);
if(data === 'male') {
const name = randChoice(maleNames);
people.push(new Data(data, name, lastName))
} else {
const name = randChoice(femaleNames);
people.push(new Data(data, name, lastName))
}
console.log('people', people)
return people;
I have tried some different things but never get it working and looping 20 times.
thanksa for all answers.
But let make this more complex
in thisd case. This will not work
The mistake is at Line 8, when you wrote for (let i = ''; i <= 20; i++), you need to first set the variable i to 0, like this: for (let i = 0; i <= 20; i++).
The whole code for your loop will then be:
for (let i = 0; i < 20; i++) {
const data = randChoice(genders);
// here goes if else statement for gender
}
Another mistake that also #Hasan Balcı has pointed out, is that in your code, the loop runs 21 times instead of 20. Why? Because the variable i starts from 0, and the loop runs until it reaches 20, including when its value is 20!
To correct this, instead of writing for (let i = 0; i <= 20; i++), it will have to be for (let i = 0; i < 20; i++), changing i <= 20 to i < 20!
Hope this helped!
Like this you can iterate loop for 20 times:
const loop = () =>{
for (let i = 0; i < 20; i++) {
const data = randChoice(genders);
console.log(data);
}}
First, I want to thanks to Bails for his answer on question console.time shows different time running the same function. But it still confusing me that how can I compare running time of two different functions
Here's the code I have tried.
function fun1(arr) {
let a = 0
for(let i = 0;i < arr.length; i++) {
a += arr[i]
}
return a
}
function fun2(arr) {
let a = 0
arr.forEach((v) => {
a += v
})
return a
}
let array = []
for(let i = 0;i < 100; i++) {
array.push(Math.random())
}
console.time('fun1')
fun1(array)
console.timeEnd('fun1')
console.time('fun2')
fun2(array)
console.timeEnd('fun2')
We all know that forEach is faster than for. But when I run the code above, I got different results:
I am really confused about this code
var box = document.getElementsByClassName('box-value');
for(let i = 0; i < box.length; i++){
box[i].onclick = function(){
console.log(i);
}
console.log("End loop. i:" + i);
}
let i = 0;
box[i].onclick = function(){
console.log(i);
}
i = 9;
box[0].onclick();
In the first block, i is 0
But in the second block, i is 9.
I really don't understand why?
Because your first i is in a block and doesn't get changed afterwards, while your second i (is not in a block) and does get set to 9 before the click handler is run. You can emulate the behaviour from the loop by doing
{
let i = 0; // one variable that stays constant
box[i].onclick = function(){
console.log(i);
};
}
let i = 9; // a different variable
and you can also emulate the altering behaviour of the assignment by putting the scope around the loop:
let i = 0;
for(; i < box.length; i++) {
box[i].onclick = function() {
console.log(i);
};
console.log("End loop. i:" + i);
}
The i declared with let in the for loop won't exist after the loop ends. The second i is separate and you setting that to 9, that's why the value of the second i is 9.
let statement documentation
Is there a more concise (i.e. one line) way to do this in Javascript?:
//Media object hasn't been declared yet
var mediaObject.tags = [];
for(var i = 0; i < 100; i++) {
mediaObject.tags.push(entries[i]);
}
EDIT: Here is how my full function looks
var mediaObject = new CustomMedia();
mediaObject.text = entries[k]['text'];
mediaObject.tags = [];
for(var i = entries[k]['tags'].length - 1; i >= 0; i--) {
mediaObject.tags.push(entries[k]['tags'][i]['value']);
}
The last part is what I want to make into one line. Declaring the mediaObject.tags and pushing the content.
Try this:
var mediaObject.tags = entries.slice(0, 100);
If I understand correctly, what you need is Array.map():
mediaObject.tags = entries[k].tags.map(function (t) { return t.value; });