Display results from a JSON response based on game type

The name of the pictureThe name of the pictureThe name of the pictureClash 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;






);






share|improve this question





















  • 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
















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;






);






share|improve this question





















  • 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












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;






);






share|improve this question













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;






);








share|improve this question












share|improve this question




share|improve this question








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.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















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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation