Test
Reflection
I got everything right so I will reflect on what makes the code that makes the quiz more efficient. This quiz is made by using a dictionary that holds all the questions, then using function every guess is checked against the dict. This allows the code in the function to be written only once, but run many times, making the code more efficient. There is also a for loop used to create all of the buttons and text. This means the formatting can be written only once and also allows new questions to be easily added because all you would have to do is put it in the dict.
values = [1, 2, 3, 4, 5];
newValues = [];
//For the length of the list
count = 4;
values.forEach(value => {
newValues += values[count];
count-=1;
});
values = newValues;
console.log(values);
sorted = false;
values = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0];
//Put the set into a holder
function ValuesHolder(values) {
this.values = values;
}
valuesHolder = new ValuesHolder(values);
//Displays the values
function getDisplayValues() {
stringOfValues = "";
//Count down from ten
for(let i = 10; i >= 0; i--) {
//For every value in values check if it is equal to or more than current i to determine box color
values.forEach(value => {
if(value>=i) {
stringOfValues+="π¦"
} else {
stringOfValues+="β¬"
}
});
stringOfValues+="\n"
}
return(stringOfValues);
}
step = 0;
console.log("Lets sort this random array using bubble sort!");
while(sorted == false) {
//Update log
console.log("Step " + step + ":\n" + getDisplayValues());
step+=1;
//See if the list is sorted using fail
fail = false;
//Go through all pairs in values
for(let i = 0; i<10; i++) {
if(values[i]>values[i+1]) {
fail = true;
//If fail, switch them
var temp = values[i];
values[i] = values[i+1];
values[i+1] = temp;
}
}
//Determine if done or not
if(fail == false) {
console.log("The array has been sorted! :) \n Final list: " + values)
sorted = true;
}
}