Skip to content

Commit

Permalink
Merge pull request #141 from GerFr/LogicFixing
Browse files Browse the repository at this point in the history
Fixed Random Suggestion
  • Loading branch information
GerFr authored Jun 8, 2024
2 parents dc3ce54 + b40b5cc commit fb3a0ab
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions warenkorb.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
const baseData =JSON.parse(localStorage.getItem("data"))
const cartData = []


//filter the data from the base data set, needed for the cart
if ((!(cartIDdata===null)) && (!(baseData===null))){
for (var j = 0; j < cartIDdata.length; j++){
for (var i = 0; i < baseData.length; i++){
Expand All @@ -38,6 +38,7 @@
}
}

//counting the amount of colors inside the cart for the product recommendation
var colorcount = {blueStar:0,
blueWhiteStar:0,
orangeRedStar:0,
Expand Down Expand Up @@ -70,7 +71,8 @@
colorcount.yellowWhiteStar = colorcount.yellowWhiteStar+ 1
}
}


//create the dataset used for the suggestions
var max = ['',0]
for (var i = 0; i < Object.entries(colorcount).length; i++) {
if (Object.entries(colorcount)[i][1] > max[1]){
Expand All @@ -83,17 +85,22 @@
maxcolor.push(baseData[i])
}
}
const ran = Math.floor(Math.random() * (maxcolor.length-1))
var ran2 = ran
while(ran === ran2){
ran2 = Math.floor(Math.random() * (maxcolor.length-1))
}
const prod_suggestion = [maxcolor[ran], maxcolor[ran2]]
render(prod_suggestion, '[type="text/x-handlebars-suggestion"]')
console.log(colorcount)
console.log(max)
console.log(prod_suggestion)
}

//filter the elements out of the dataset if already in cart
let set1 = new Set(cartData.map(item => item.StarID))
let result = maxcolor.filter(item => !set1.has(item.StarID))

//render random product suggestion if more than one element in the suggestion dataset
if(result.length > 1){
const ran = Math.floor(Math.random() * (maxcolor.length-1))
var ran2 = ran
while(ran === ran2){
ran2 = Math.floor(Math.random() * (maxcolor.length-1))
}
const prod_suggestion = [result[ran], result[ran2]]
render(prod_suggestion, '[type="text/x-handlebars-suggestion"]')
}
}



Expand Down

0 comments on commit fb3a0ab

Please sign in to comment.