Display results from a JSON response based on game type

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This code currently works with no errors. I want to know if there is a way to improve its conding structure, as there is a lot of repetition and it looks really bad. I tried an alternative way by using a parameter to handle the type of game, but it broke the code completely.
What would be the best way of making this code shorter or just overall improved?
 $(document).ready(() => //Make sure the document is done loading
 $.ajax( //Initialize the ajax object
 url: "data/games.json", //set the target url`
 type: "GET",
 dataType: 'JSON', //set the expected response type
 success: function(response) ,
 error: function() 
 console.error('Whoops something went wrong...');
 
 );
 const displayOutput = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "Coming Soon" && game.ID <= 3) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayOutputHome = (games, hasCheckbox) => 
 //console.log(games[2]["Genres"]);
 //let ul = document.getElementById('ajax');
 let output = "";
 var x = 0;
 games.forEach(game => 
 if (game.Type === "Coming Soon") 
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayNewGames = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "New Release") 
 var x = 100;
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 //return true;
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 let $compare = $(".visible");
 $compare.on("click", (event) => 
 $(event.currentTarget).text($(event.currentTarget).text() === "Item added" ? "Compare" : "Item added");
 ).on("click", (event) => 
 $(event.currentTarget).addClass("visible");
 ) 
 const displayNewOutput = (newReleased, hasCheckbox) => 
 let Newoutput = "";
 newReleased.forEach(game => 
 if (game.Type === "New Release" && game.ID >= 4) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 Newoutput += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b> $ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return Newoutput;
 
 );
javascript jquery html json formatting
add a comment |Â
up vote
2
down vote
favorite
This code currently works with no errors. I want to know if there is a way to improve its conding structure, as there is a lot of repetition and it looks really bad. I tried an alternative way by using a parameter to handle the type of game, but it broke the code completely.
What would be the best way of making this code shorter or just overall improved?
 $(document).ready(() => //Make sure the document is done loading
 $.ajax( //Initialize the ajax object
 url: "data/games.json", //set the target url`
 type: "GET",
 dataType: 'JSON', //set the expected response type
 success: function(response) ,
 error: function() 
 console.error('Whoops something went wrong...');
 
 );
 const displayOutput = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "Coming Soon" && game.ID <= 3) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayOutputHome = (games, hasCheckbox) => 
 //console.log(games[2]["Genres"]);
 //let ul = document.getElementById('ajax');
 let output = "";
 var x = 0;
 games.forEach(game => 
 if (game.Type === "Coming Soon") 
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayNewGames = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "New Release") 
 var x = 100;
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 //return true;
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 let $compare = $(".visible");
 $compare.on("click", (event) => 
 $(event.currentTarget).text($(event.currentTarget).text() === "Item added" ? "Compare" : "Item added");
 ).on("click", (event) => 
 $(event.currentTarget).addClass("visible");
 ) 
 const displayNewOutput = (newReleased, hasCheckbox) => 
 let Newoutput = "";
 newReleased.forEach(game => 
 if (game.Type === "New Release" && game.ID >= 4) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 Newoutput += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b> $ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return Newoutput;
 
 );
javascript jquery html json formatting
 
 
 
 
 
 
 i think you need to add the- game.jsontoo as anyone who would like to re-write the code might need it in running condition.
 â Muhammad Omer Aslam
 Feb 9 at 10:46
 
 
 
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This code currently works with no errors. I want to know if there is a way to improve its conding structure, as there is a lot of repetition and it looks really bad. I tried an alternative way by using a parameter to handle the type of game, but it broke the code completely.
What would be the best way of making this code shorter or just overall improved?
 $(document).ready(() => //Make sure the document is done loading
 $.ajax( //Initialize the ajax object
 url: "data/games.json", //set the target url`
 type: "GET",
 dataType: 'JSON', //set the expected response type
 success: function(response) ,
 error: function() 
 console.error('Whoops something went wrong...');
 
 );
 const displayOutput = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "Coming Soon" && game.ID <= 3) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayOutputHome = (games, hasCheckbox) => 
 //console.log(games[2]["Genres"]);
 //let ul = document.getElementById('ajax');
 let output = "";
 var x = 0;
 games.forEach(game => 
 if (game.Type === "Coming Soon") 
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayNewGames = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "New Release") 
 var x = 100;
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 //return true;
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 let $compare = $(".visible");
 $compare.on("click", (event) => 
 $(event.currentTarget).text($(event.currentTarget).text() === "Item added" ? "Compare" : "Item added");
 ).on("click", (event) => 
 $(event.currentTarget).addClass("visible");
 ) 
 const displayNewOutput = (newReleased, hasCheckbox) => 
 let Newoutput = "";
 newReleased.forEach(game => 
 if (game.Type === "New Release" && game.ID >= 4) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 Newoutput += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b> $ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return Newoutput;
 
 );
javascript jquery html json formatting
This code currently works with no errors. I want to know if there is a way to improve its conding structure, as there is a lot of repetition and it looks really bad. I tried an alternative way by using a parameter to handle the type of game, but it broke the code completely.
What would be the best way of making this code shorter or just overall improved?
 $(document).ready(() => //Make sure the document is done loading
 $.ajax( //Initialize the ajax object
 url: "data/games.json", //set the target url`
 type: "GET",
 dataType: 'JSON', //set the expected response type
 success: function(response) ,
 error: function() 
 console.error('Whoops something went wrong...');
 
 );
 const displayOutput = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "Coming Soon" && game.ID <= 3) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayOutputHome = (games, hasCheckbox) => 
 //console.log(games[2]["Genres"]);
 //let ul = document.getElementById('ajax');
 let output = "";
 var x = 0;
 games.forEach(game => 
 if (game.Type === "Coming Soon") 
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 const displayNewGames = (games, hasCheckbox) => 
 let output = "";
 games.forEach(game => 
 if (game.Type === "New Release") 
 var x = 100;
 x++;
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 //return true;
 output += `
 <li>
 <div class=""><input id="togg` + x + `" type="checkbox"><label for="togg` + x + `" class="$hasCheckbox ? 'visible' : 'invisible'">Compare</label></div></li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b>$ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return output;
 
 let $compare = $(".visible");
 $compare.on("click", (event) => 
 $(event.currentTarget).text($(event.currentTarget).text() === "Item added" ? "Compare" : "Item added");
 ).on("click", (event) => 
 $(event.currentTarget).addClass("visible");
 ) 
 const displayNewOutput = (newReleased, hasCheckbox) => 
 let Newoutput = "";
 newReleased.forEach(game => 
 if (game.Type === "New Release" && game.ID >= 4) 
 let ratingDiv = $("<div class='rating'></div>");
 for (var i = 0; i < game.Rating; i++) 
 ratingDiv.append('<span class="fa fa-star checked"></span>');
 
 for (var i = game.Rating; i < 5; i++) 
 ratingDiv.append('<span class="fa fa-star"></span>');
 
 Newoutput += `
 <li>
 <li><a href=#><img class="frontGames" src="$game.image">
 <p><b>Name:</b>$game.Name<br>
 <b>Release Date:</b>$game.ReleaseDate</br>
 <b>Genres:</b>$game.Genres</br>
 <b>Retail Price:</b>$game.RetailPrice</br>
 <b>Rating:</b> $ratingDiv.html()</br></p></a>
 </li>`;
 
 )
 return Newoutput;
 
 );
javascript jquery html json formatting
edited Feb 5 at 17:28


200_success
123k14143401
123k14143401
asked Feb 5 at 12:51
James Green
112
112
 
 
 
 
 
 
 i think you need to add the- game.jsontoo as anyone who would like to re-write the code might need it in running condition.
 â Muhammad Omer Aslam
 Feb 9 at 10:46
 
 
 
add a comment |Â
 
 
 
 
 
 
 i think you need to add the- game.jsontoo as anyone who would like to re-write the code might need it in running condition.
 â Muhammad Omer Aslam
 Feb 9 at 10:46
 
 
 
i think you need to add the
game.json too as anyone who would like to re-write the code might need it in running condition.â Muhammad Omer Aslam
Feb 9 at 10:46
i think you need to add the
game.json too as anyone who would like to re-write the code might need it in running condition.â Muhammad Omer Aslam
Feb 9 at 10:46
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186814%2fdisplay-results-from-a-json-response-based-on-game-type%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
i think you need to add the
game.jsontoo as anyone who would like to re-write the code might need it in running condition.â Muhammad Omer Aslam
Feb 9 at 10:46