I am trying to debug my code.
When I add console.log('aaa'); it shows ok in the test output
When I add console.log statements within a for loop however
console.log('aaa');
for(let i = 0; i++; i < 10) {
console.log(i);
}
I get the 'aaa' as output but I do not get the numbers in the loop.
The for loop should have the check before the increment
for(let i = 0; i < 10; i++)
^ ^
switch these
Related
For example I have this string "()[]|}" and I want to compare each characters with eachother, but first I can't get the loop to work correctly. For the first log it works correctly, but after instead of going +1 it should go +2 I believe so it doesn't start from ). How do can I do this properly?
function isValid(s) {
let temp = s.split("");
for (let i = 0; i < s.length/2; i++) {
console.log(temp[i], temp[i+1]);
}
};
isValid("()[]|}");
I want to log this
( )
[ ]
| }
not
( )
) [
[ ]
Your code is correct but your for loop is slightly off:
for (let i = 0; i < s.length; i += 2) {
Loop while i is less than the length, but add 2 instead of 1.
function isValid(s) {
let temp = s.split("");
for (let i = 0; i < s.length; i += 2) {
console.log(temp[i], temp[i+1]);
}
};
isValid("()[]|}");
If the question is 'how do I increment by two instead of by 1', use i += 2 instead of i++ (since the increment operator only increments by 1)
I need to show in console the numbers from 1 to 20 without an array..that s what i did:
function oneToTwenty() {
for (i = 1; i <= 20; i++) {
print i;
}
}
console.log(i);
what is wrong?
The console.log(i); should be inside the for loop:
for (i = 1; i <= 20; i++) {
console.log(i);
}
there's no such thing as "print". use console.log instead of print.
You also have an extra closing "}", which doesn't make sense.
for (i = 1; i <= 20; i++) {
console.log(i);
}
You need to put your console.log inside the loop. Otherwise, you firstly do the looping and then try to log the non-existent i.
Also, note that you have to declare i, if you have not done it, using var or let.
for (let i = 1; i <= 20; i++) {
console.log(i);
}
Hello I need to write a pair of loops that would go through numbers in specific way. I'll try to show on example.
lets say I have loops like this.
for(let i = 0; let i <4; i++){
for (let j = ?; condition;? ){
console.log(j)
}
and from this I would want result in console like this: 12345 2345 345 45 (but numbers printed in console indivudally not as string)
If it was i < 3 result should be 1234 234 34 etc.
How would you write the conditions or what would you do to get the result?
Thank you for any help.
Edit: I need the numbers to be printed by console.log as individual number and not a s string, sorry for bad description, same way this code would
(let i =0; i<4; i++ )
{
console.log(i)
}
here you go:
for(let i = 1; i <= 4; i++){
if(i!=4){
for (let j = i; j<=4;j++){
console.log(j)
}
}
}
A general solution using for-loop.
function ReturnNumberList(n){
let arrayList=[], base= "";
//loop to build the base number
for(let i=1;i<n+1;i++){
base= base + (i).toString();
}
//loop to print the list
for(let i=0;i<n-1;i++){
let temp = base.substr(i,base.length);
console.log(temp)
arrayList.push(temp);
}
return arrayList.join(",").replace(/,/g,"");
}
I'm trying to target a list of elements using a for loop:
for(var i = 1; i < 5; ++i){
console.log(i)
target[i].classList.remove('redText')
anchor[i].classList.remove('redText')
}
The expected result is:
target1.classList.remove('redText')
anchor1.classList.remove('redText')
target2.classList.remove('redText')
anchor2.classList.remove('redText')
....etc.
in the console I get
ReferenceError: target is not defined
Which means the index is not being appended to target and anchor.
Is this possible to accomplish?
This is what you're looking for:
for(var i = 1; i < 5; ++i){
console.log(i)
document.getElementById('target' + i).classList.remove('redText')
document.getElementById('anchor' + i).classList.remove('redText')
}
Fiddle.
Why do nested for loops work in the way that they do in the following example:
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
for (var i = 0; i < times.length; i++) {
var newTimes = [];
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
console.log(newTimes);
}
}
In this example I would have thought console.log would give me the following output:
["04/11/10"]
["86kg"]
["05/12/11"]
["90kg"]
["06/12/11"]
["89kg"]
However, I actually get this:
["04/11/10"]
["04/11/10", "86kg"]
["05/12/11"]
["05/12/11", "90kg"]
["06/12/11"]
["06/12/11", "89kg"]
Is anyone able to help me understand this?
EDIT:
Thanks for all your responses!
You are redefining newTimes on every single loop and you are outputting to the console on each column push.
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
var newTimes = [];
for (var i = 0; i < times.length; i++) {
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
}
}
console.log(newTimes);
Returns: ["04/11/10", "86kg", "05/12/11", "90kg", "06/12/11", "89kg"]
http://jsfiddle.net/niklasvh/SuEdt/
// remember that the increment of the counter variable
// is always executed after each run of a loop
for (var i = 0; i < n; i++) {
// some statement(s) to do something..
// initializes child-loop counter in the first run of the parent-loop
// resets child-loop counter in all following runs of the parent-loop
// while i is greater than 0 and lower than n
for (var j = 0; j < p; j++) {
// some statement(s) to do something..
// initializes grandchild-loop counter in the first run of the child-loop
// resets grandchild-loop counter in all following runs of the child-loop
// while j is greater than 0 and lower than p
for (var k = 0; k < q; k++) {
// some statement(s) to do something..
// or add more internal loop-nestings if you like..
}
}
}
// if the counter variables of the descendent-loops were set before the loop-nesting,
// the inner loops would only run once, because the counter would keep the value
// of the abortion condition after the loop is finished
Do this:
var newTimes = [];
for (var i = 0; i < times.length; i++) {
for(var x = 0; x < times[i].length; x++) {
newTimes.push(times[i][x]);
console.log(newTimes);
}
}
You are re-initializing newTimes each time through the loop.
You output would be appropriate if the log statement would read
console.log(times[i][x]);
Instead you output your complete new list newTimes which is initialized outside the inner loop and grows with each inner loop iteration.
The problem is in the second round of the inner loop, where it pushes the second element into newTimes. Anyway I don't understand the reason of inner loop. You can write much simpler:
var times = [
["04/11/10", "86kg"],
["05/12/11", "90kg"],
["06/12/11", "89kg"]
];
for (var i = 0; i < times.length; i++) {
console.log(time[i][0]);
console.log(time[i][1]);
}