Determine, as a class, what the intent of today should be
What is the value of newArr
const newArr = [...[1, 2, 3], ...[4, 5, 6]];
What is the value of color
for newCar
?
What is the value of color
for secondNewCar
?
class Car {
constructor(brand, model, color = "soul red") {
this.brand = brand;
this.model = model;
this.color = color;
}
}
const newCar = new Car("Mazda", "MX-30");
const secondNewCar = new Car("Porche", "Vintage Carrera", "Yellow");
Here we have our Deck of Cards class, simplified to generate cards with a numeric value. We can generate a deck of shuffled cards. We want to create a method that will put the cards back in order.
Write your own sort
method that sorts the cards from 2 - 11. Do not use the array method .sort()
.
class DeckOfCards {
constructor() {
this.cards = [];
this.createDeck();
this.shuffle(this.cards);
}
createDeck() {
for (let i = 1; i <= 13; i++) {
if (i === 1) {
this.cards.push(11);
} else if (i === 11) {
this.cards.push(10);
} else if (i === 12) {
this.cards.push(10);
} else if (i === 13) {
this.cards.push(10);
} else {
this.cards.push(i);
}
}
}
sort(array) {
// your code
}
shuffle(array) {
let m = array.length;
let i = 0;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
[array[m], array[i]] = [array[i], array[m]];
}
return array;
}
}
const deck = new DeckOfCards();
console.log(deck);
Use this visualization to learn more about different sorting algorithms
Another visualization using colors and pixels
Watch a video about two different sorting algorithms
- Bubble Sort Once
.sort()
Practice - Please continue next session if not finished