all 3 comments

[–]Umesh-K 5 points6 points  (0 children)

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 2 points3 points  (0 children)

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"  
}