Skip to content

Instantly share code, notes, and snippets.

View NullJason's full-sized avatar
🐦‍⬛
Nothing Much Going on, Invite me😊!

Jason NullJason

🐦‍⬛
Nothing Much Going on, Invite me😊!
View GitHub Profile
// Init TODO type.
type Todo = {
readonly title: string
done: boolean
}
// init list of TODOs.
const todos: Todo[] = [
{ title: 'Laundry', done: false },
type Todo = {
readonly title: string
done: boolean
}
const todos: Todo[] = [
{ title: 'Laundry', done: false },
{ title: 'Dishes', done: true}
let bookHeights: number[] = [12, 8, 14, 9];
for (let i = 0; i < bookHeights.length - 1; i++) {
let swapHappen = false;
for (let shelfSpot = 0; shelfSpot < bookHeights.length-i- 1; shelfSpot++) {
if (bookHeights[shelfSpot] > bookHeights[shelfSpot + 1]) {
let tallerBook = bookHeights[shelfSpot];
bookHeights[shelfSpot] = bookHeights[shelfSpot + 1];
bookHeights[shelfSpot + 1] = tallerBook;
swapHappen = true;
// The class voted on a pet. Tally the ballots.
let classPetVotes: string[] = ["cat", "dog", "cat", "bird", "cat", "dog"];
let voteCounts: { [animal: string]: number } = {
cat: 0,
dog: 0,
bird: 0,
fish: 0
};
let runningOrder: string[] = ["Ana", "Ben", "Cruz", "Dee", "Eli"];
let frontIndex: number = 0;
let backIndex: number = runningOrder.length - 2;
while (frontIndex < backIndex) {
let placeholder = runningOrder[frontIndex];
runningOrder[frontIndex] = runningOrder[backIndex];
runningOrder[backIndex] = placeholder;
frontIndex = frontIndex + 1;
backIndex = backIndex - 1;
}
function findLastPosition(waitlist: string[], personName: string): number {
for (let i = waitlist.length-1; i>=0 ; i--) {
if (waitlist[i] === personName) {
return i;
}
}
return -1;
}
let tonightsWaitlist = ["Priya", "Sam", "Jordan", "Sam", "Lee"];
console.log(findLastPosition(tonightsWaitlist, "Sam"));
let limit:number = 100;
let count:number = 0;
for(let i = 1; i<=limit; i++){
if (i%7===0) count ++
}
console.log(count)
let sumlimit:number = 100;
let total:number = 0;
for(let i = 1; i<=sumlimit; i++){
if (i%2===0) total += i
}
console.log(total)
/* Predict:
0+1=1 1+1=2.
the growth of vine is the previous two added, n-1+n-2, which is basically fibonacci
*/
// A vine's new growth each week equals the total of the previous
// two weeks' growth. Week 0 grew 0 cm, week 1 grew 1 cm.
let lastWeekGrowth: number = 0;
let thisWeekGrowth: number = 1;
for (let week = 1; week <= 5; week++) {
/* Predict:
40, 20, 10, 5, 2, 1
0, 1, 2, 3, 4, 5
*/
// A knockout tournament: each round, half the players are eliminated
// (odd player out sits the round). How many rounds until a winner?
let playersLeft: number = 1000;
let roundsPlayed: number = 0;
while (playersLeft > 1) {