i found this kata on codewars which I found it to be interesting, in short, I am supposed to make a diamond shape of strings using n input into the function. Kata here https://www.codewars.com/kata/give-me-a-diamond/train/javascript
This is my solution where I work on the bottom diamond first before the top diamond and concat it together. It is not v efficient as it uses 2 loops with repeat code to track bottom and top diamond. I couldn’t understand the solutions based on other people code where they use a single loop to solve, could anyone explain to me how to solve this solution in a single loop? i appreciate any help thanks !
this is my code
function diamond(n){
// kind of hacky solution with playing around with top diamond value
if(n < 0 || n % 2 === 0) return null;
// bottom diamond
let bResult = "";
let bCounter = 0;
for(let i = n; i > 0; i-=2){
let space = " ".repeat(bCounter);
bResult += `${space}${`*`.repeat(i)}\n`;
bCounter++;
}
// top diamond
let tResult = "";
let tCounter = bCounter-1;
for(let i = 1; i < n; i+=2){
let space = " ".repeat(tCounter);
tResult += `${space}${`*`.repeat(i)}\n`;
tCounter--;
}
return tResult.concat(bResult)
}