1. const PALETTE = ["#5B8FF9", "#5AD8A6", "#F6BD16", "#E86452"];
    2. /**
    3. * ==========================================================================
    4. * @param startWithColor absent, or one of the colors in PALETTE
    5. */
    6. function getColorGenerator(
    7. startWithColor?: string
    8. ): Generator<string, string> {
    9. // Your code goes here
    10. let i = startWithColor ? PALETTE.indexOf(startWithColor) - 1 : -1;
    11. return (function* fn() {
    12. while (i < PALETTE.length) {
    13. i++;
    14. if (i === PALETTE.length) {
    15. i = 0;
    16. }
    17. yield PALETTE[i];
    18. }
    19. return '';
    20. })();
    21. }
    22. /**
    23. * ==========================================================================
    24. * Finish the function
    25. * Expected console output:
    26. * "#5B8FF9"
    27. * "#5AD8A6"
    28. * "#F6BD16"
    29. * "#E86452"
    30. */
    31. function main1() {
    32. const colorGenerator = getColorGenerator();
    33. for (let i = 0; i < 8; i++) {
    34. if (i % 2 !== 0) {
    35. continue;
    36. }
    37. // use colorGenerator to get color, and print it
    38. // console.log(color)
    39. const color = colorGenerator.next().value;
    40. console.log(color);
    41. }
    42. }
    43. main1();
    44. /**
    45. * ==========================================================================
    46. * Finish the function
    47. * Expected console output:
    48. * "#F6BD16"
    49. * "#E86452"
    50. * "#5B8FF9"
    51. * "#5AD8A6"
    52. */
    53. function main2() {
    54. const colorGenerator = getColorGenerator("#F6BD16");
    55. for (let i = 0; i < 4; i++) {
    56. // use colorGenerator to get color, and print it
    57. // console.log(color)
    58. const color = colorGenerator.next().value;
    59. console.log(color);
    60. }
    61. }
    62. main2();