Interview Questions on Javascript

Quick Questions to check basic conceptual knowledge.

Question 4

print ping pong alternatively in JavaScript:
Input : 10 (number of occurrences)
Output :
ping
pong
ping
pong
ping
pong
ping
pong
ping
pong

For such question, main thing we need is modulus operator. Based on modulus operator, we can decide whether to print ping or pong. This is as simple as the code below. On similar concept the code can be written in any language.

Javascript:


	console.log('print ping pong alternatively in JavaScript')
								
	for (i = 1; i <= 10; i++) {
		if (i%2 === 1) {
			console.log('ping');
		} else {
			console.log('pong');
		}
	}