Try to guess the output of this code without running it. Let me know in the comment what you got 😉
let num = 10;
for(let i = 0; i < num; i++){
console.log(num);
num--;
}
hint: You may have got the first half right
Try to guess the output of this code without running it. Let me know in the comment what you got 😉
let num = 10;
for(let i = 0; i < num; i++){
console.log(num);
num--;
}
hint: You may have got the first half right
For further actions, you may consider blocking this person and/or reporting abuse
Arun -
Rafael Magalhaes -
Apache Doris -
ndesmic -
Once suspended, 0shuvo0 will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, 0shuvo0 will be able to comment and publish posts again.
Once unpublished, all posts by 0shuvo0 will become hidden and only accessible to themselves.
If 0shuvo0 is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Shuvo.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag 0shuvo0:
Unflagging 0shuvo0 will restore default visibility to their posts.
Top comments (16)
Output (i/num after iteration):
10 (1/9)
9 (2/8)
8 (3/7)
7 (4/6)
6 (5/5) - loop terminates as
!(i < num).
The output of this code is me telling you to fix that code 🤣 ... jokes aside, if you see a pice of code like this, your first question should be:
I'm sure that if you see this in "production", there was a typo somewhere, because the actual behavior (looping down from 10 to 6), can be achieved with a much clearer code, so is surely a mistake if you see something like that.
Cheers!
Yes, It was just a trick question for newbies
I know, and my comment is explaining "newbies" that you don't always have to resolve the problem in interviews. Sometimes the best answer is: "I don't understand your code/intention", instead of banging your head against the keyboard.
Nice one!
The solution is:
10
9
8
7
6
The explanation:
1- It'll output 10 since we didn't change it yet.
2- We'll increase i to
1
and num decreased to9
.3- Increases i to
2
and num decreased to8
.4- Increases i to
3
and num decreased to7
.5- Increases i to
4
and num decreased to6
.Then it ends, 'cause num will be
5
while i will be the equal, so it stops.10
9
8
7
6
5
4
3
2
am i correct?
Thanks for trying. but actually its wrong
a unicorn bookmark well earned. ty for this.
Many many Thank s 💓
10
9
8
7
6
this should be the output.
Is it uhmm
10
9
8
7
6
Correct
10, 9, 8, 7, 6
Correct
10, 9, 8, 7, 6
for loop stop when num = 5, ( every console.log in for decrease num for 1 )
right