JavaScript Coding Challenge 1

March 16, 2017

JavaScript Coding Challenge 1

For a long time, JavaScript was a programming language I tried to stay away from. It’s notorious for being insecure, but maybe a lot of that is due to sloppy coding by some of the people who use it. These days, however, it’s nearly everywhere, so it’s hard to escape. When creating websites, it’s considered as essential to know as HTML and CSS. Recently, I noticed Udemy had a sale on virtually all classes, so I signed up for The Complete JavaScript Course: Build a Real-World Project. I’ve only been doing it a few days, so I’ve got a long way to go. Of the 113 lectures, some of them are called “Coding Challenges”, where the student tries to write a project based on what has been taught already.

Lecture 14 is Coding Challenge 1. The challenge is a simple game between two players. Each player takes their height in centimeters and adds it to five times their age, and the winner is the one with the highest number. To make the game a bit more complex, a third player later joins the game. The code below is my solution.

// simple game
// highest value of height (in cm) and five times age wins

var playerOneHeight = 180;
var playerOneAge    = 26;
var playerTwoHeight = 190;
var playerTwoAge    = 22;

var p1Score = playerOneHeight + playerOneAge * 5;
var p2Score = playerTwoHeight + playerTwoAge * 5;

/*
if (p1Score > p2Score) {
    console.log('Player One wins');
} else if (p1Score < p2Score) {
    console.log('Player Two wins');
} else {
    console.log('Players One and Two are tied');
}
*/

// make it more complex
var playerThreeHeight   = 176;
var playerThreeAge      = 54;

var p3Score    = playerThreeHeight + playerThreeAge * 5;

if (p1Score > p2Score && p1Score > p3Score) {
    alert('Player 1 wins!');
} else if (p2Score > p1Score && p2Score > p3Score) {
    alert('Player 2 wins!');
} else if (p3Score > p1Score && p3Score > p2Score) {
    alert('Player 3 wins!');
} else if (p1Score === p2Score && p1Score > p3Score) {
    alert('Players 1 and 2 have tied!');
} else if (p1Score === p3Score && p1Score > p2Score) {
    alert('Players 1 and 3 have tied!');
} else if (p2Score === p3Score && p2Score > p1Score) {
    alert('Players 2 and 3 have tied!');
} else {
    alert('All players have tied');
}
    
 console.log('Player 1: ' + p1Score + ' Player 2: ' + p2Score + ' Player 3: ' + p3Score);

I’ll post more of the coding challenges later.

Music and the Lollipop Lock Screen

June 15, 2015

Music and the Lollipop Lock Screen

After the battery started giving me trouble in my THL T6 Pro, I broke down and bought a slightly more upscale smartphone – an ASUS Zenfone 2. Both phones are about the same height and width, though the ASUS seems to be a bit thicker. One of the main differences in the phones is that the THL used KitKat (Android v. 4.4.x) and the ASUS uses Lollipop (Android v. 5.x). Anytime I get a new piece of technology, it takes awhile to figure it out and get it to work the way I want it to. I don’t plan on everything working right out-of-the-box.

Because it’s a good security practice, and because my employer requires it, I always set my mobile devices to require a PIN or password at the lock screen. This isn’t much of an issue, unless I’m listening to music. When I’m walking to or from work, I like to listen to music. The music app is set to shuffle, but it sometimes lands on a song that I don’t feel like listening to. With Android KitKat, I was able to go to the lock screen and fast-forward to another song, but I wasn’t to do this in Android Lollipop. Trying to figure it out, it looks like I’m not the only one with this problem.

Although I tried some of the online suggestions, they didn’t work for me. All of the suggestions I read had the same instructions: go to Settings -> Sound & Notification -> When device is locked -> Show all notification content. I did this but my lock screen was still only showing the time, the temp, and the number pad for the PIN.

To fix it, after doing the step above, I went into Settings -> Lock screen -> Skip the slide page -> Off. The Slide page is where the music widget shows up. Try it out.

Go to Music and start a song, then press the power button on the device to turn the screen off. Press the power button again to get the lock screen. Your song should still be playing, and you should be see the music widget, plus whatever other notifications you have enabled. If you want to unlock the device, you can swipe up from the bottom.

Tags