Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>

<div x-data="app" x-cloak>
	<div x-show="intro">
		<p>
			Welcome to the Marvel Guess the Decade game. In this game, I'll show you a random comic cover selected anywhere from 1950 to the 2020s. You need to guess the decade the comic was released. 
		</p>
		<button @click="start">Get Started</button>
	</div>
	<div x-show="!intro">
		
		<img x-show="imageSrc" :src="imageSrc" class="cover">
		<div x-show="!imageSrc">
			<p><i>Loading cover...</i></p>
		</div>

		<div x-show="guessReady" class="buttonRow">
			<!-- hard coded for 1950 to 2020. todo: come back in 7 years -->
			<template x-for="i in 8">
				<button @click="guess(1950+((i-1)*10))"><span x-text="1950+((i-1)*10)"></span>s</button>
			</template>
		</div>
		
		<div x-show="resultReady">
			<p>
			<strong x-text="msg"></strong> This cover is from <span x-text="cover.title"></span> in <span x-text="cover.year"></span>.
			</p>
			<p>
				<button @click="getImage">Guess Again</button>
			</p>
		</div>
		<p>
			You have currently gotten <span x-text="right"></span> correct and <span x-text="wrong"></span> wrong.
		</p>
	</div>
</div>

              
            
!

CSS

              
                [x-cloak] { display: none !important; }


img.cover {
	width: 415px;
	height: 615px;
	object-fit: cover;
	object-position: top;
	display: block;
	margin: auto;
}

div.buttonRow {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 10px;
}
              
            
!

JS

              
                document.addEventListener('alpine:init', () => {
  Alpine.data('app', () => ({
		intro:true,
		right: 0,
		wrong: 0,
		imageSrc:null,
		guessReady: false,
		resultReady: false,
		cover: { title:null, year:null },
		msg:'',
		start() {
			this.intro = !this.intro;
			this.getImage();
		},
		async getImage() {
			this.imageSrc = null;
			this.guessReady = false;
			this.resultReady = false;
			let coverReq = await fetch('https://randomcover.raymondcamden.workers.dev');
			this.cover = await coverReq.json();
			// add .year based on date
			this.cover.year = this.cover.date.split('/').pop();
			/*
			So, I kept seeing comics with WILDLY different years and figured out we could use regex.
			I'm keeping the code above, just in case, but this code here should help.
			*/
		  let found = this.cover.title.match(/\(([1-2][0-9]{3})\) #/);
			if(found) {
				console.log(`changing year to ${found[1]}`);
				this.cover.year = found[1];
			}
			this.imageSrc = this.cover.url;
			this.guessReady = true;
			console.log(this.cover);
		},
		guess(decade) {
			// so decade will come in as a base 4d year, like 2000, 
			// my year from the API is m/yyyy. so we can easily get y, 
			// and then simply check the first 3 digits
			console.log('guessing',decade);
			console.log('right is', this.cover.year);
			if(decade.toString().substring(0,3) === this.cover.year.substring(0,3)) {
				this.msg = 'You got it right!';
				this.right++;
			} else {
				this.msg = 'Sorry, you were wrong!';
				this.wrong++;
			}
			this.guessReady = false;
			this.resultReady = true;
						
		}
  }))
});


              
            
!
999px

Console