I'm trying to figure out how to alternate the "*" symbol on both sides of the string "a" within my do..while loop. I've changed a few things around, but ultimately no dice- I'm stumped on how to get the desired result. Does anyone have any suggestions on what I can do? This is the code I came up with:
function padIt(str,n){
var str="a", n= "*"
do{
str+=n;
n++;
} while(n<=5)
return str;
}
function padIt(str = "a", n = 0){
var pad = "*";
do {
if (n%2 === 0) str+=pad;
else str = pad + str;
n++;
} while(n<=5)
return str;
}
Padding alternation starts at the end when n
is odd. You can swap the operations on the if
and else
statement if you want it to behave the other way.
padIt() // Output: '***a***'
padIt('TESTING',1) // Output: '***TESTING**'
padIt('TESTING',2) // Output: '**TESTING**'
padIt('TESTING',3) // Output: '**TESTING*'
padIt('TESTING',4) // Output: '*TESTING*'
padIt('TESTING',5) // Output: '*TESTING'
padIt('TESTING',6) // Output: 'TESTING*'
padIt('TESTING',7) // Output: '*TESTING'
// ...