Javascript

Messages
1,684
Reaction score
1,859
Points
1,140
Location
Belgium
So for school i'm making a mealorder website, i'm struggling with the sort tho.... Can someone help me out?

so i have this already:

"use strict";

document.addEventListener("DOMContentLoaded", init);

function init() {
let html = `<option value="id">id</option><option value="img">img</option>
<option value="book">book</option><option value="cal">calories</option>
<option value="serv">servings</option><option value="type">type</option>
<option value="price">price</option><option value="cook">cook</option>
<option value="quan">quantity</option>`;
document.querySelector('#sortby').innerHTML = html;
document.querySelector("#sortby").addEventListener("change", sortByCheck)
}



document.querySelector('input').addEventListener('keyup', search);

function search() {
let input, filter, i, txtValue, article, h3;
input = document.querySelector("input");
filter = input.value.toLowerCase();
article = document.querySelectorAll('article');
for (i = 0; i < meals.length; i++) {
h3 = article.getElementsByTagName("h3")[0];
txtValue = h3.textContent || h3.innerText;
if (txtValue.toLowerCase().indexOf(filter) > -1) {
article.style.display = "";
} else {
article.style.display = "none";
}
}
}



function sortByCheck() {
let sorters = document.querySelector('#sortby').options;
let wichsorted = document.querySelector('#sortby').selectedIndex;
let sorter = sorters[wichsorted].text;
console.log(sorter);
localStorage.getItem('mealsList');
allMeals.sort(function(a, b){return b.calories - a.calories}); // only did this to try if it works this way, it doesn't
addMeals();
}





this is the list, with meals:
it should be able to sort on every property of the list.


1545050475963.png




I already figured out to check on wich one the user wants it sorted, that works but i have no clue how to sort it then.... didn't find anything like this on the internet!
 
Javascript is the devil's language.

Use CoffeeScript or something. It's much simpler and it's basically Java, but reconstructed.

But ofc if you're in school, so you won't be allowed to use anything other than Javascript as you're probably learning about it.
 
This is partially fixed rn, i can sort them if it are numbers, doesn't work for strings tho. I'll just be happy with the fact more then 75% works.
 
here's what you can try

get it to work for sorting with strings for everything which can be done by checking against the first character of each of them and using ASCII values to determine which one will be highest and which go beneath.

Then once its working for strings, make it check for any characters in the string by doing a linear search through it, if there are no characters then it should mean it's a number and therefore you can convert it back into integer form and sort it

although I don't know much about actual javascript
 
Back
Top