// Will print a diamond with n stars in the middle linefunctionprintDiamond(n){// validate inputif(n%2===0)thrownewError("can't have middle row size of an even number");if(n<0)thrownewError('input must be a positive number');constnumLines=n;letspaces=n-1;letstars=1;constmiddleLine=Math.floor(numLines/2);for(leti=0;i<numLines;i++){console.log(buildLine(stars,spaces));// before the middle line we will increment the number of stars by 2if(i<middleLine){stars+=2;spaces-=2;}// after the middle line we decrement the number of stars by 2else{stars-=2;spaces+=2;}}}// will build a line with the correct number of stars and spaces// ie given 3 stars and 2 spaces// will build ' *** 'functionbuildLine(stars,spaces){letline='';constlineSize=stars+spaces;// the first index in the line with a star.conststarIndex=Math.floor((lineSize-stars)/2);for(leti=0;i<lineSize;i++){// starts building starsif(i>=starIndex&&stars>0){line+='*';stars--;}elseline+='';}returnline;}
deviated from the spec in one specific way where I don't return null if a an even number or negative number is provided but instead throw an error.
I feel however this gives more information to the user.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
deviated from the spec in one specific way where I don't return null if a an even number or negative number is provided but instead throw an error.
I feel however this gives more information to the user.