Session 4
Take home exercise, pt 1
Create a piece of code that detects whether a given string is a pangram or not.
Your program should report on whether or not a given string is a pangram, where it also states the string in its output. As an example, your program should display the following in the Console tab of the debugging tool that all common browsers support:
'the quick brown fox jumps over the lazy dog’ is a pangram.‘the quick brown fox jumps over the lazy programmer’ is not a pangram.Completed code
let str = 'the quick brown fox jumps over the lazy dog';let letters = 0;let counter = 0;let length = str.length;let isPangram = ' is a pangram';let notPangram = ' is not a pangram';let a = 0;let b = 0;let c = 0;let d = 0;let e = 0;let f = 0;let g = 0;let h = 0;let i = 0;let j = 0;let k = 0;let l = 0;let m = 0;let n = 0;let o = 0;let p = 0;let q = 0;let r = 0;let s = 0;let t = 0;let u = 0;let v = 0;let w = 0;let x = 0;let y = 0;let z = 0;while (counter < length) { if (str[counter] == 'a') { letters = letters + 1; a = a + 1; } if (str[counter] == 'b') { letters = letters + 1; b = b + 1; } if (str[counter] == 'c') { letters = letters + 1; c = c + 1; } if (str[counter] == 'd') { letters = letters + 1; d = d + 1; } if (str[counter] == 'e') { letters = letters + 1; e = e + 1; } if (str[counter] == 'f') { letters = letters + 1; f = f + 1; } if (str[counter] == 'g') { letters = letters + 1; g = g + 1; } if (str[counter] == 'h') { letters = letters + 1; h = h + 1; } if (str[counter] == 'i') { letters = letters + 1; i = i + 1; } if (str[counter] == 'j') { letters = letters + 1; j = j + 1; } if (str[counter] == 'k') { letters = letters + 1; k = k + 1; } if (str[counter] == 'l') { letters = letters + 1; l = l + 1; } if (str[counter] == 'm') { letters = letters + 1; m = m + 1; } if (str[counter] == 'n') { letters = letters + 1; n = n + 1; } if (str[counter] == 'o') { letters = letters + 1; o = o + 1; } if (str[counter] == 'p') { letters = letters + 1; p = p + 1; } if (str[counter] == 'q') { letters = letters + 1; q = q + 1; } if (str[counter] == 'r') { letters = letters + 1; r = r + 1; } if (str[counter] == 's') { letters = letters + 1; s = s + 1; } if (str[counter] == 't') { letters = letters + 1; t = t + 1; } if (str[counter] == 'u') { letters = letters + 1; u = u + 1; } if (str[counter] == 'v') { letters = letters + 1; v = v + 1; } if (str[counter] == 'w') { letters = letters + 1; w = w + 1; } if (str[counter] == 'x') { letters = letters + 1; x = x + 1; } if (str[counter] == 'y') { letters = letters + 1; y = y + 1; } if (str[counter] == 'z') { letters = letters + 1; z = z + 1; } counter = counter + 1;}if (a < 1) { isPangram = notPangram;}if (b < 1) { isPangram = notPangram;}if (c < 1) { isPangram = notPangram;}if (d < 1) { isPangram = notPangram;}if (e < 1) { isPangram = notPangram;}if (f < 1) { isPangram = notPangram;}if (g < 1) { isPangram = notPangram;}if (h < 1) { isPangram = notPangram;}if (i < 1) { isPangram = notPangram;}if (j < 1) { isPangram = notPangram;}if (k < 1) { isPangram = notPangram;}if (l < 1) { isPangram = notPangram;}if (m < 1) { isPangram = notPangram;}if (n < 1) { isPangram = notPangram;}if (o < 1) { isPangram = notPangram;}if (p < 1) { isPangram = notPangram;}if (q < 1) { isPangram = notPangram;}if (r < 1) { isPangram = notPangram;}if (s < 1) { isPangram = notPangram;}if (t < 1) { isPangram = notPangram;}if (u < 1) { isPangram = notPangram;}if (v < 1) { isPangram = notPangram;}if (w < 1) { isPangram = notPangram;}if (x < 1) { isPangram = notPangram;}if (y < 1) { isPangram = notPangram;}if (z < 1) { isPangram = notPangram;}console.log(str + isPangram);console.log(length);Take home exercise, pt 2
Create an improved version of the program that you implemented for the first part of this challenge, where the new version ascertains not only the pangrammicity of a given input string, but also (if the string really is a pangram), whether or not it is a perfect pangram. An example of the output from such a program is as follows:
‘the quick brown fox jumps over the lazy programmer’ is not a pangram.‘mr Jock, tv quiz phd, bags few lynx’ is a perfect pangram.Completed code
let str = 'the quick brown fox jumps over the lazy dog';let letters = 0;let counter = 0;let length = str.length;let isPangram = ' is a pangram';let notPangram = ' is not a pangram';let perfectPangram = ' is a perfect pangram';let a = 0;let b = 0;let c = 0;let d = 0;let e = 0;let f = 0;let g = 0;let h = 0;let i = 0;let j = 0;let k = 0;let l = 0;let m = 0;let n = 0;let o = 0;let p = 0;let q = 0;let r = 0;let s = 0;let t = 0;let u = 0;let v = 0;let w = 0;let x = 0;let y = 0;let z = 0;while (counter < length) { if (str[counter] == 'a') { letters = letters + 1; a = a + 1; } if (str[counter] == 'b') { letters = letters + 1; b = b + 1; } if (str[counter] == 'c') { letters = letters + 1; c = c + 1; } if (str[counter] == 'd') { letters = letters + 1; d = d + 1; } if (str[counter] == 'e') { letters = letters + 1; e = e + 1; } if (str[counter] == 'f') { letters = letters + 1; f = f + 1; } if (str[counter] == 'g') { letters = letters + 1; g = g + 1; } if (str[counter] == 'h') { letters = letters + 1; h = h + 1; } if (str[counter] == 'i') { letters = letters + 1; i = i + 1; } if (str[counter] == 'j') { letters = letters + 1; j = j + 1; } if (str[counter] == 'k') { letters = letters + 1; k = k + 1; } if (str[counter] == 'l') { letters = letters + 1; l = l + 1; } if (str[counter] == 'm') { letters = letters + 1; m = m + 1; } if (str[counter] == 'n') { letters = letters + 1; n = n + 1; } if (str[counter] == 'o') { letters = letters + 1; o = o + 1; } if (str[counter] == 'p') { letters = letters + 1; p = p + 1; } if (str[counter] == 'q') { letters = letters + 1; q = q + 1; } if (str[counter] == 'r') { letters = letters + 1; r = r + 1; } if (str[counter] == 's') { letters = letters + 1; s = s + 1; } if (str[counter] == 't') { letters = letters + 1; t = t + 1; } if (str[counter] == 'u') { letters = letters + 1; u = u + 1; } if (str[counter] == 'v') { letters = letters + 1; v = v + 1; } if (str[counter] == 'w') { letters = letters + 1; w = w + 1; } if (str[counter] == 'x') { letters = letters + 1; x = x + 1; } if (str[counter] == 'y') { letters = letters + 1; y = y + 1; } if (str[counter] == 'z') { letters = letters + 1; z = z + 1; } counter = counter + 1;}if (a < 1) { isPangram = notPangram;}if (b < 1) { isPangram = notPangram;}if (c < 1) { isPangram = notPangram;}if (d < 1) { isPangram = notPangram;}if (e < 1) { isPangram = notPangram;}if (f < 1) { isPangram = notPangram;}if (g < 1) { isPangram = notPangram;}if (h < 1) { isPangram = notPangram;}if (i < 1) { isPangram = notPangram;}if (j < 1) { isPangram = notPangram;}if (k < 1) { isPangram = notPangram;}if (l < 1) { isPangram = notPangram;}if (m < 1) { isPangram = notPangram;}if (n < 1) { isPangram = notPangram;}if (o < 1) { isPangram = notPangram;}if (p < 1) { isPangram = notPangram;}if (q < 1) { isPangram = notPangram;}if (r < 1) { isPangram = notPangram;}if (s < 1) { isPangram = notPangram;}if (t < 1) { isPangram = notPangram;}if (u < 1) { isPangram = notPangram;}if (v < 1) { isPangram = notPangram;}if (w < 1) { isPangram = notPangram;}if (x < 1) { isPangram = notPangram;}if (y < 1) { isPangram = notPangram;}if (z < 1) { isPangram = notPangram;}if ( a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z == 26) { isPangram = perfectPangram;}console.log(a);console.log(str + isPangram);console.log(length);Notes
It took me a total of about 4 days to arrive at the above solutions. I overcomplicated the task again by trying to be "smart".
I could have probably got this finished within a few hours if I had just gone with the idea I used in my failed solution 2 of creating a variable for each alphabetic letter.
I was hesitant to declare so many variables as it felt like the code was repeating itself so much, it felt wrong to do. I always thought "there must be a smarter way" and I'm sure there is, but reading back through Sessions 1 - 3 after hours of trying the "smarter way" I realised that to get the job done, maybe I was going to have to repeat myself a lot.
There was one thing from the task that I chose not to tackle, which was the uppercase "J" in the perfect pangram. I could have added all uppercase characters to my while loop but I would be doubling the amount of code I use. I decided to leave this out, this may be where i've gone drastically wrong here and created an incorrect/imperfect solution.
I did not youtube or google my way to the solution, which is a first.
Incorrect solutions
I'm hoping this shows a little bit inside my mind of where / what I was thinking before I solved it. I kept a full log of the various attempts and annotated them along the way so as to show my thought process.
My unedited notes from the very start of the exercise:
My approach to the problem is like this: Check each letter of the alphabet in the string -> check if the letter exists in the string -> if not -> return the statement -> not a pangram
My thoughts are:- I think I can probably do it in a similar way I saw richard do it in Session 4.
Using a while loop and some if satements, with the addition of the else if statement for my code.
### Incorrect solution 1let str = "the quick brown fox jumps over the lazy dog";let letters = 0;let counter = 0;let length = str.length;let isPangram = " is a pangram";let notPangram = " is not a pangram";while (counter < length) { if (str[counter] == "a") { letters = letters + 1; } else if (str[counter] == "b") { letters = letters + 1; } else if (str[counter] == "c") { letters = letters + 1; } else if (str[counter] == "d") { letters = letters + 1; } else if (str[counter] == "e") { letters = letters + 1; } else if (str[counter] == "f") { letters = letters + 1; } else if (str[counter] == "g") { letters = letters + 1; } else if (str[counter] == "h") { letters = letters + 1; } else if (str[counter] == "i") { letters = letters + 1; } else if (str[counter] == "j") { letters = letters + 1; } else if (str[counter] == "k") { letters = letters + 1; } else if (str[counter] == "l") { letters = letters + 1; } else if (str[counter] == "m") { letters = letters + 1; } else if (str[counter] == "n") { letters = letters + 1; } else if (str[counter] == "o") { letters = letters + 1; } else if (str[counter] == "p") { letters = letters + 1; } else if (str[counter] == "q") { letters = letters + 1; } else if (str[counter] == "r") { letters = letters + 1; } else if (str[counter] == "s") { letters = letters + 1; } else if (str[counter] == "t") { letters = letters + 1; } else if (str[counter] == "u") { letters = letters + 1; } else if (str[counter] == "v") { letters = letters + 1; } else if (str[counter] == "w") { letters = letters + 1; } else if (str[counter] == "x") { letters = letters + 1; } else if (str[counter] == "y") { letters = letters + 1; } else if (str[counter] == "z") { letters = letters + 1; } else if (str[counter] == " ") { letters = letters + 1; } else { isPangram = notPangram; break; } counter = counter + 1;}console.log(length);console.log(str + isPangram);console.log(letters);I was pretty convinced this was the working answer to the solution. I thought my else if " " with a space was a bit of a hack possibly but I essentially thought it would work.
Problem is that I can still remove a few letters and it'll say its a pangram. My logic is obviously flawed.
Lets say I remove the "d" from "dog", I still get "isPangram". Which is not what I want.
Incorrect solution 2
I decided to pull apart the Session 4 code to see if I could figure out a way of counting individual letters, spaces, etc.
I wanted to break down the problem to a slightly smaller problem or maybe re-frame the problem to figure out the logic that I might need
With this code, I've figured out how to count individual characters and count spaces.
/\*let Str = "naw is the wanter of aur discantent";let aCount = 0;let blankSpace = 0;let bCount = 0;let CharacterPosition = 0;let Limit = Str.length;while (CharacterPosition < Limit) {if (Str[CharacterPosition] == "a") {aCount = aCount + 1;} else if (Str[CharacterPosition] == " ") {blankSpace = blankSpace + 1;} else if (Str[CharacterPosition] == "b") {bCount = bCount + 1;} else if (bCount == 0) {bCount = 0;}CharacterPosition = CharacterPosition + 1;}console.log("No. of a's: " + aCount);console.log("No. of blank spaces: " + blankSpace);console.log("No. of b's: " + bCount);Now I just need to figure out how to refactor this for a full alphabet of statements?
I am thinking that I don't need to declare all of these variables "aCount, blankSpace, bCount" I can just do if statements for a more generically named variable.
Something like charExists?
Now i'm going to go back to my old code and see if I can add some extra statements to get it to work.
Incorrect solution 3
I just want to start with one character for now and build it up, i've re-worded a few variables just because it felt easier in my head.
let str = 'the quaick brown foax jumps ovear the lazy dog';let letters = 0;let counter = 0;let length = str.length;let isPangram = ' is true';let notPangram = ' is false';while (counter < length) { if ((str[counter] == 'a') >= 1) { letters++; aCount++; console.log( 'is it an A?', letters, 'where is the A in the string?:', aCount ); // what on earth is happening here? } else if (str[counter] != 'a') { aCount++; console.log('letter count2:', letters, 'counting not an a:', aCount); // what on earth is happening here? } counter = counter + 1;}console.log(letters);console.log(str + isPangram);// 35 letters, 8 spaces == lengthconsole.log(length);// dont understand why even this simple else if doesn't work// trying to go back to basics here. what would be the simplest solution?// Really struggling to work out how to flag if a character does not exist in the string. I feel like my code is looking for the first instance where that's not the case and disregarding the rest straight away.// this solution took me about 3-4 days to write. I just could not imagine that writing this much code is the "right" way to do it. but it does work. did i overcomplicate things? It was at the front of my brain after the previous attempt but I just could not imagine that I should be declaring all of these variables.I got really attached to the idea that this would work for too long:
else if (str[counter] != 'a') { aCount++ console.log('letter count2:', letters, 'counting not an a:', aCount) }Which I now understand is basically traversing the string looking at when something doesn't equal "a". Which obviously happens a lot in a string of 40+ characters.
Lesson learned, stop getting bogged down in a specific solution, and console.log can be very useful at telling you the output when you're not exactly sure what to expect with the outcome.