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.

Attic Insulation

February 19, 2017

As mentioned in the previous post, the attic of my house had virtually no insulation in it. Indianapolis is sort of in a weird place when it comes to weather. We don’t often get heavy snowfalls or bitter cold in the winter, however, they do happen every so often. Since heat rises, an uninsulated attic lets out a lot of heat. Adding insulation is like putting a thick quilt over the top of the house, trapping most of the heat underneath it, which is what you want in winter. By trapping the heat, the heating bills should be lower. So the theory goes, at least.

In fall of 2016, I started looking at insulation, hoping to install it before winter. My first choice was to get insulation made from recycled denim, but it isn’t carried by stores in my area and getting it shipped to my house would have been prohibitively expensive. Fiberglass insulation is also expensive, but it’s available almost everywhere. Reluctantly, I decided to go with fiberglass because it’s cheaper and more commonly available. The final product I chose was Owens-Corning kraft-faced R-38 batts. Each floor of the house is a little over 800 sq ft, so I planned on getting that much insulation. Since each pallet of insulation covers approximately 320 sq ft, I bought two pallets and planned on seeing how much I needed after going through it all.
read more …

The House

February 16, 2017

Home Sweet Home

Home Sweet Home

Last spring, I bought a house in the Haughville neighborhood of Indianapolis. It’s a two story house in the American Craftsman style and was built in the 1920s. Though the house has been well-maintained and is very livable, with a house this old, it’s not surprising it needs some work. As I do new projects around the house, I’ll post about them, along with pictures. But before I do that I want to write a bit about the neighborhood, in general, and the house, in particular.

I moved to Indianapolis in March of 2000, and prior to the move I’d lived in Colorado for more than 20 years. I didn’t know anything about Indy, but I knew that if I wouldn’t be able to see mountains, I wanted to live in an urban area, where the tall buildings would block the mountainless view. Basically, I didn’t want to see that the mountains weren’t there. Weird logic, I know. Upon moving to Indy, I got a 500 sq. ft studio apartment in downtown Indy at the corner of 13th and Pennsylvania (around the corner from the President Benjamin Harrison home). It was a nice place, but rather basic. Rent when I first moved in was $325 per month. And life went on.
read more …

Messing around with code

May 20, 2016

Coding is something I do so rarely I often have to look online for tips and go through alot* of trial and error to get things done. On the current project, I wanted to do something differently.

Normally, in most database tables, it’s common to create an ID field to hold a number that serves as a unique identifier for that record (commonly known as a primary key). It’s easy to setup because the database does all the work, therefore it’s a very common form of primary key. However, primary keys don’t necessarily have to be numbers. The only requirement is that they be unique within the table.

For this project I wanted each primary key to be 10 characters long and consist of numbers and letters. Ten digits, plus 26 lowercase characters, plus 26 uppercase characters in a ten character string means 141 trillion possible unique keys (10+26+26)10 = 141,167,095,653,412 That should be enough for any database… that I’m working on.

In playing around with the code I realized there were a lot of other possibilities. I didn’t have to limit myself to numbers and the Latin alphabet. In fact, I didn’t need to use them at all. The code below is the same code I came up with, but the characters I used are different.


// generates a 10 character random ID
function id_gen($newid) {
$chars = "⩽⩾⩿⪀⪁⪂⪃⪄⪅⪆⪇⪈⪉⪊⪋⪌⪍⪎⪏⪐⪑⪒⪓⪔⪕⪖⪗⪘⪙⪚⪛⪜⪝⪞⪟⪠⪯⪰⪱⪲⪳⪴⪵⪶⪷⪸⪹⪺";
$tmp = preg_split("//u", $chars, -1, PREG_SPLIT_NO_EMPTY);
shuffle($tmp);
$tmp2 = join("", $tmp);
return mb_substr($tmp2,0,10,"UTF-8");
}

Calling id_gen in some PHP code would result in something like this ⩾⪺⪐⪒⪞⪠⪎⪉⪚⪷. Imagine a table full of those as their primary keys. The key to the function is PHP’s builtin mb_substr function. The normal substr function will take a string and cut it to a particular length, but it doesn’t do well with most UTF-8 characters. The mb_substr function can handle multibyte characters, which is why it’s used in this function.

* Fuck prescriptivist grammarians. “Alot” is a perfectly legitimate English word. If it gets used and is understood in context, it’s part of the language.

新年快乐!

January 8, 2016
没有咖啡因 - without caffeine

没有咖啡因

新年快乐! 我不是新年决心迷,可是有的年我做决心。三年以前我退出了咖啡因。几年以前在工作我经常正常喝了咖啡。因为周末我不喝了咖啡,星期天我平常有了头疼。退出咖啡因以后我少有了头疼。

Another semester

August 30, 2015

Another semester, another Chinese class. This semester at IUPUI I’m taking EALC-C 301, Third Year Chinese 1. Classes started last week and I can already tell the class is going to be interesting. This is the first Chinese class I’ve had where I feel like I’m in the bottom-half of the class. Granted, it’s a small class, with only seven students, but some of the students have set a high bar. Two students are Chinese, so I’m not sure why they’re taking the class. Maybe they hope to test out of it. Another student taught English in China for a year. I’m under the impression he hasn’t been there for awhile, but he definitely has more practice.

Still, I’m resolved that the high bar isn’t insurmountable. I just have to work harder.

A Chinese coworker, who’s always good for telling me different chengyu (成语), told me about a saying of Mao that appears in classrooms over there:

好好学习,天天向上。
Hǎohāo xuéxí, tiāntiān xiàngshàng.
Study well, make progress everyday.

I take it to mean I should study everyday, including weekends, to make progress. To help me in my Chinese studies, I’ve also bought a year-long Chinese course from Transparent language. I’ll let you know how it goes.

Tags

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

Live Below the Line

April 11, 2015

A year or so ago I read about a challenge where some people in the UK tried to live on £1 a day for five days. This is the level the World Bank sets as the level of extreme poverty. Nearly 1.2 billion people around the world live in conditions of extreme poverty. In the US it equates to about $1.50. I found out about the challenge too late to participate last year, but this year I’m going to give it a go.

Coincidentally, this morning I read that Gwenyth Paltrow will be taking the Food Stamp Challenge. The Food Stamp Challenge is similar, but it sets the bar at about $30 for seven days, which is what a family in the US would have to live on if they relied on food stamps. Both challenges are meant to draw attention of people living in poverty, though Live Below the Line is an organized attempt to raise money for charities fighting conditions of poverty.

While I’ve never lived in conditions of extreme poverty, I remember we used to be on food stamps when I was a kid, and as an adult, there have been times when I was unemployed and wondered where my next meal was coming from. This gives me reason enough to try and help. The challenge takes place from April 27th to May 1st.

It’s not going to be a true challenge. People who live in extreme poverty have to use that money to pay for food, shelter, clothes, healthcare, transportation, sanitation, and so on. For me, the challenge will just pay for food. I already have clothes and health insurance. I don’t have a car, so I don’t have to worry about that cost. My rent, which isn’t exorbitant, runs about $18.50 a day. Since I don’t plan on living on the streets those five days, it’s not a real challenge, in my opinion. However, I plan on blogging and showing what $1.50 a day buys in the US. The answer is not much.

很嗔

March 28, 2015

Today I took the HSK 3, which was a disaster. The character 嗔 (chēn) means angry or annoyed, and that pretty much describes my feelings about today’s test.

The HSK 1 exam which I took last year had a vocabulary list of 150 words, and I passed that test. The HSK 2 test, which I took and passed this past December had a vocabulary list of 300 words. The HSK 3 exam has a vocabulary list of 600 words, and I wasn’t familiar with many of them. My original plan was to take the HSKK (speaking ability) today and take the HSK 3 next fall, but our teacher required us to take the HSK 3. Just taking the exam is 5% of our grade, regardless of our score on the exam. The reason it’s required is because somebody in the department is apparently doing a study to compare our success rate to students at IU Bloomington. The only reason I took the test is because it accounts for 5% of my grade in class and because we were promised reimbursement for the cost of the test. Personally, I have better things to do with my weekends than to take a test I’m not prepared for.

All that aside, the test isn’t what I expected either. It started out fine, beginning with the listening section, which was divided into four parts. The first two parts went fine, but when the third part started, the audio on the test stopped. I double checked to make sure my headphones were plugged in, and they were. I adjusted the audio on the PC, and it was fine. I would hear a ping whenever the volume was adjusted. Despite this there was still no audio from the test, and it isn’t much use having a listening section with no audio. I raised my hand and got the attention of the teacher conducting the test. She got another teacher, and they contacted somebody (I assume someone from Hanban in China). The instructions were to close the browser and restart the test, and it would pick up from where I left off. I followed the instructions and was back in business. Annoying, but no harm done, from what I understand.

The next section was reading, which was fine, despite the fact that I didn’t know many of the words. I’m in my second year of Chinese classes, but felt as if I was asked to take the final exam for a third-year Chinese class. Needless to say, I guessed on a pretty much everything in that section.

The final section was writing. It should have been my best section, but it ended up being the worst. I took the paper practice exams, and did okay because I could write down the characters even if I didn’t know how they were pronounced. I did pretty good at guessing nouns, verbs, adjectives, adverbs, etc, so putting them in the right order wasn’t too hard. On the computer, you can’t do that. You can’t type in something if you don’t know the Pinyin for it. I tried to copy-and-paste, but there was no way to do so. Clicking on the characters did nothing. Double-clicking did nothing. Right-clicking did nothing. Ctrl+C and Ctrl+V did nothing. The only way to input a character is to know the pinyin for it, and I only knew about half the characters.

In the last part of the writing section, they have a sentence, and they give the pinyin for a character they want you to type in. It should have been easy, but as I was getting ready to type in my answer to the first question (it related to the cost of something, and the pinyin was ‘yuan’, so my response was to be 元) it kicked me out. I thought I had a couple of minutes left, but I don’t know if I ran out of time, or if there was an error of some sort.

So, lessons learned. First off, in the future, I’ll only take the tests when I’m sure I’m ready for them. If I’m required to take a test, I’ll a) make sure I don’t have to pay for it, and b) set out to deliberately fail it. I see no reason to try something I know I’m not ready for. Since this was part of somebody’s study, I’d like to fuck up their results out of spite.

Perhaps the most important lesson, however, is that the paper practice exams are worthless for anyone taking a computer-based exam. Supposedly, there is an online practice test at the HSK website, but I’ve never been able to get it to work. Maybe it just doesn’t like Macs. Regardless, if I can’t practice taking the computer-based exam, I’d feel much better taking the paper based test.

Tags

Noun + adjective + 得 + 不得了

February 10, 2015

We just started working on Chapter 19 of Integrated Chinese last week, and part of the homework had us practicing the phrase 不得了 (bùdéliǎo), which means extremely or exceedingly. It usually follows this pattern:

noun + adjective + 得 + 不得了。
Noun is extremely adjective.

挪威冷得不得了。
Nuówēi lěng de bùdéliǎo.
Norway is extremely cold.

李友用功得不得了。
Lǐ Yǒu yònggōng de bùdéliǎo.
Li You is extremely hardworking.

This sentence pattern is very useful. In fact, I’ve even been able to use it with one of my coworkers. If they receive a suspicious email, I encourage the users I support to send the email to me to see if it is legitimate or if it’s a scam. This morning, a 中国人 coworker sent me an email that was suspicious, and I replied:

我觉得它坏得不得了。
I think it’s extremely bad.

Maybe it’s overkill to describe a spam email, but it was a good chance to practice, and she complimented my Chinese 🙂