1
\$\begingroup\$

Which of these 2 functions is better and more readable and why? (Both functions do the same thing).

My goal is to get the total time of a series, using the TMDB API (which has a limit of 20 different things in the append_to_response parameter).

    async function getSeriesRuntime1(tmdbId: number, numberOfSeasons: number): Promise<number> {
      let runtime: number = 0;
      let AppendToResponseString: string = '';
     
      for (let i: number = 1; i <= numberOfSeasons; i++) {
        (i < numberOfSeasons) ? AppendToResponseString += `season/${i},` : AppendToResponseString += `season/${i}`;
     
        if (i % 20 === 0 || i === numberOfSeasons) {  // exactly 20 seasons
          const response: Response = await fetch(`https://api.themoviedb.org/3/tv/${tmdbId}?append_to_response=${encodeURIComponent(AppendToResponseString)}&language=en-US`, options);
          const jsonObj: Record<string, any> = await response.json();
     
          // Adjusting the additional index to traverse the seasons in the JSON response
          let seasonIndex: number = (i % 20 === 0) ? i - 19 : i - (numberOfSeasons % 20);
     
          for (; seasonIndex <= i; seasonIndex++) {
            const seasonKey = `season/${seasonIndex}`;
            if (seasonKey in jsonObj && jsonObj[seasonKey].episodes) {
              jsonObj[seasonKey].episodes.forEach((episode: Episode) => {
                runtime += episode.runtime;
              });
            }
          }
     
          AppendToResponseString = '';  // Restarting the string for the next request
        }
      }
     
      return runtime;
    }
    async function getSeriesRuntime2(tmdbId: number, numberOfSeasons: number): Promise<number> {
      let runtime: number = 0;
      let firstSeasonToCount: number = 1;
      let encodedAppendToResponseString: string = '';
     
      for (let i: number = 1; i <= numberOfSeasons; i++) {
        encodedAppendToResponseString += `season/${i},`;
     
        if (i === numberOfSeasons) {
          encodedAppendToResponseString = encodeURIComponent(removeCommaFromEnd(encodedAppendToResponseString));
          const response: Response = await fetch(`https://api.themoviedb.org/3/tv/${tmdbId}?append_to_response=${encodedAppendToResponseString}&language=en-US`, options);
          const jsonObj: Record<string, any> = await response.json();
          for (let i: number = firstSeasonToCount; i <= numberOfSeasons; i++) {
            jsonObj[`season/${i}`].episodes.forEach((episode: Episode) => {
              runtime += episode.runtime;
            });
          }
          return runtime;
        }
        if (encodedAppendToResponseString.split(',').length === 21) {  // number of seasons equal to 20 (max allowed)
          encodedAppendToResponseString = encodeURIComponent(removeCommaFromEnd(encodedAppendToResponseString));
          const response: Response = await fetch(`https://api.themoviedb.org/3/tv/${tmdbId}?append_to_response=${encodedAppendToResponseString}&language=en-US`, options);
          const jsonObj: Record<string, any> = await response.json();
          for (let i: number = firstSeasonToCount; i <= firstSeasonToCount+19; i++) {
            jsonObj[`season/${i}`].episodes.forEach((episode: Episode) => {
              runtime += episode.runtime;
            });
          }
          firstSeasonToCount += 20;
          encodedAppendToResponseString = '';
        }
      }
     
      return runtime;
    }
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.