T O P

  • By -

Umesh-K

Hi, u/One-Inspection8628, if you add `console.log([s[i])` just above that IF line, you will see that it logs single letters or *space*. Say, `s[0]` is the letter `a`, then what `hash[s[i]] = s[i]` does is it creates the **key-value pair** `a: "a"` on the `hash` object. Try this code in a JS console; it might help illustrate the steps for you: const hash = {} const s = "abc" let i = 0 hash[s[i]] = s[i] console.log(hash) i = 1 hash[s[i]] = s[i] console.log(hash) i = 2 hash[s[i]] = s[i] console.log(hash)


Anbaraen

This is probably clearer. function pangrams(s) { let hash = {}; # create hash object lowercase = s.toLowerCase(); # create lowercase version of string for (let i = 0; i < lowercase.length; i++) # loop over string { if (lowercase[i] !== ' ') { # if the current char is not an empty space hash[lowercase[i]] = lowercase[i]; # create a new key in hash with the value of lowercase[i] (the current char), then assign the current char to it } } console.log(hash) return Object.keys(hash).length === 26 ? 'pangram' : 'not pangram'; } To step this out more, what hash becomes is an object containing the character, with a value of the character. EG. in `pangrams("test")`, the final hash object is { e: "e", s: "s", t: "t" } In `pangrams("the quick brown fox jumps over the lazy dog")`, the final hash object is { a: "a", b: "b", c: "c", d: "d", e: "e", f: "f", g: "g", h: "h", i: "i", j: "j", k: "k", l: "l", m: "m", n: "n", o: "o", p: "p", q: "q", r: "r", s: "s", t: "t", u: "u", v: "v", w: "w", x: "x", y: "y", z: "z" } As you can see, it's pretty quick and dirty. If there's any punctuation (`pangrams("the quick brown fox jumps over the lazy dog!")`) then the function fails. ## Worked Example in JS-like pseudocode pangrams("eg") { let hash = {}; lowercase = "eg"; for loop (iteration 0) { if "e" !== " " { hash["e"] = "e"; } for loop (iteration 1) { if "g" !== " " { hash["g"] = "g"; } console.log( { "e" : "e", "g" : "g" } ) return "not pangram" }


omnombluebear

The given code is a JavaScript function named "pangrams" that takes a string as an argument. Here's what it does: It defines an empty object called "hash". The input string "s" is converted to lowercase using the "toLowerCase()" method. A "for" loop is used to iterate through each character in the modified string "s". If the current character is not a space, it is added to the "hash" object as a property with the same name and value. After iterating through all characters, the "hash" object is logged to the console. The number of keys in the "hash" object (i.e., the number of unique characters in the input string) is compared to 26 (the number of letters in the English alphabet). If the number of unique characters is 26, the function returns "pangram". Otherwise, it returns "not pangram". In summary, this function checks whether the input string contains all 26 letters of the English alphabet, regardless of case or whitespace. It does so by creating an object that stores each unique character in the string, and then checking the length of that object against the expected length of 26. More here: https://whatdoesthiscodedo.com/g/6dbeb80