Programming Puzzle - hot question asked in interview
http://www.gufgaaf.com/Page/DisplayThread?threadId=5
Easily can be done since modifying for loop is allowed. Just have i<101 and increase i by i=i+1 and print i.
But this can be done without modifying the For loop. Just print ("1","2","3"......."100");
:)
Yeah. But that prints 1 to 100 but we have to print 100 to 1.
try this as well -
http://gufgaaf.com/Page/DisplayThread?threadId=13
Set I to 101 and decrease I to 1 or something like that.
i'd do this in scripting for similar needs -
I think the question asked to print 100 to 1:
for(i=100; i>0; i--) {
console.log(i);
}
and if you wanted 1 to 100:
for(i=1; i<101; i++) {
console.log(i);
}
wish all interview questions were as lame.
@ghogay_moro
Oops thats wrong.-
everything needs to be done within
for(var i=0){
}
you cant start another for loop outside closing parenthesis. and you can't print 1 to 100.
for(var i=0; i<100; i++) {
console.log(100-i);
}
a_guy, i think i am not seeing the obvious. care to explain a bit how my for loop is "starting" outside either parentheses? not trying to be a deek, but just trying to understand which part of the question i misread. cheers!
Last edited: 16-Jun-16 09:47 AM
This is super basic. Calling it a Puzzle, itself is a Puzzle. Lol.
@ghogay_moro: What I understood was that the initializer expression should be limited to var i = 0. @A_GuY: Correct me if I am wrong.
@fuckeetow: Yes, it's pretty straightforward. The only confusion folks had was with the initializer constraint.
thanks c88.., for the explanation. i can now wrap that in my head. the original question should have asked just that!
side note: talking about interviews, i once helped prepare a question for an interview - to come up with a typewriter effect via ActionScript (Flash was a thing then!), and he didn't waste more than 3 minutes to complete it. it takes me more than 3 minutes to just think through the problem. some people really are good at what they do!
Thanks guys for participating..
Please log in to reply to this post
You can also log in using your Facebook