Letter Lookup explainer

Aim of the puzzle: Write a function that takes a string and an object map of a string, and returns true if all of the letters of the string exist in the object, or false if they do not.
Walk through of solution: In a later puzzle, you will write a function that checks if a string is a subsequence of another string. For now, write a function that simply checks if all of the letters in one string exist in the other.

In the last puzzle, you wrote a function that turns a string into an object map. The output of that function will be used as input for this function.

To start, add a for loop to iterate through the input string. For example:

for (let letter of word) {

}

Then, add an if else statement that checks if a property with the name of the current character exists in the map. If a letter is found, the function does not need to do anything, but if the letter is not found, the function should return false. It should look something like this:

for (let letter of word) {
 if (map[letter] {
  
  } else {
    return false;
  }
}

At the end of the function, return true.

return true;

This line of code will only run if the function iterates through the entire string without returning false for any of the letters.
Javascript Concepts: Functions, Objects, Strings, Properties, For Loops, If Statements, Bracket Notation, Truthy and Falsy
Additional Code (hidden code that runs before the puzzle’s code):

let dictionaryWord = pickRandom([ 'able', 'peals', 'bale', 'kangaroo']);

let stringSequence = {
            a:[0],
            b: [1],
            p:[2,3,4],
            l:[5],
            e:[6,7]
        };

const makeMap = (o) => {
   var out = '';
   for (var p in o) {
      out += p + ': ' + '[' +  o[p] + ']' + ', ' ;
      console.log(out);
      out = ''
   }
   return o
};
2 Likes


What am I doing wrong

Hey there,

The function declaration has 2 parameters: word and object

object represents the string that has been mapped to an object. word is the string we want to compare it with.

In your if statement, you are using the string 'map' instead of the object parameter.

Try replacing the string 'map' with the identifier object (it will be its own key on the keyboard, so you won’t have to type it).

Hope this helps! Let me know if you have any questions.
-Ben

2 Likes

What am I doing wrong?

Hey there, delete return false from the code block in the if statement.

Then, in the else code block, change return true to return false.

Finally, on the last line of the function declaration, add return true

Hope this helps!
Ben

1 Like
var s = ['apple'];

var m ={a:[1],b:[1],p:[3],l:[1],e:[2]};

function compare(string, map){

  for(var element of string){

    if(map[element]){

    }else{

      return false;

    }

  }

return true;

};

console.log(compare(s, m))

what is wrong with this it is returning only false

2 Likes

Congrats on putting your code in a code block! I’d send you a medal if I could.

In your code, you have the variable s as an array containing one item, the string 'apple'.

This means that the for loop in the function is looping through the array, seeing the string 'apple', then checking if the map has a property named apple, which it does not. This is why it’s returning false.

If you just want the function to look at one string only, then remove the array brackets around 'apple'. If you want to make the function look at multiple strings, you’ll need to add a 2nd for loop inside the 1st one, like this:

var words = ['apple', 'orange'];

var mappedWord = {
  a: [1],
  b: [1],
  p: [3],
  l: [1],
  e: [2]
};

function compare(wordList, map) {
  for (var word of wordList) {
    for (var letter of word) {
      if (map[letter]) {

      } else {
        return false;
      }
    }
  }
  return true;
};

console.log(compare(words, mappedWord))

Hope this helps!
Ben

4 Likes
import { makeMap } from 'subsequence.functions';
import { dictionaryWord, stringSequence } from 'subsequence.words';
function compareLetters(word, object, ___) {
  for (var letter of word) {
    if (**object**[letter]) {
    } else {
      return false;
    }
  }
  return true;
};
console.log(dictionaryWord);
let stringSequenceObject = makeMap(stringSequence, ___);
console.log(compareLetters(dictionaryWord, stringSequenceObject));

Hope this helps.

Hi,

I’m in trouble again. Having successfully coded the challenge, I am attempting to reproduce the program using plain JavaScript. I have successfully reproduced previous challenges, but this one is proving more challenging.

I understand that programmers are problem solvers and that some problems require more time for a solution to be found, but given the hints and the walk-through, I cannot see where I am going wrong.

My code:

`

let word = prompt("", "Enter a word:");

function mapString(string) {
  let map = {};

  for (let i = 0; i < string.length; i++) {
    // make letters properties of map
    let letter = string[i];

    map[letter] ? map[letter].push(i): map[letter] = [i];
  }
  return map;
}

// display index of each letter
let stringMap = mapString(word);

for (let letter in stringMap) {
  console.log(letter + ": " + " index: " + stringMap[letter]);
}

// compare the string with the object
function compareLetters(word, object) {
  for (let element in word) {
    if (object[element]) {

     } else {
         return false;
    }
  }
  return true;
}

console.log(mapString(word));
console.log(compareLetters(word, stringMap));

`

I would appreciate some help.

Thank you.

2 Likes

Hey there, I love that you’re applying what you’ve learned!

I’m not very familiar with the browser’s prompt() function, so I looked up the documentation for it on MDN. MDN is a site maintained by Mozilla (the company that makes the Firefox browser) that contains all the info you’d ever want to know about JavaScript.

In any case, it looks like there are 2 issues here.

  1. Prompt isn’t returning the value you expect. It thinks 'Enter a word: ’ is part of the word.
  2. The compareLetters() function is always returning false, even when true is expected.

The 1st issue looks like a quick fix. The arguments for prompt() are in the wrong order. The 1st argument is a message to display, and the 2nd is a default string to place in the text box. MDN says the default string is optional, unless you’re using an old version of Internet Explorer.

This means you can change the line to let word = prompt("Enter a word")

The 2nd issue is also a quick fix. Your compareLetters() function uses a for…in loop to iterate through word, but word in this case is a string, and a for…in loop is used to iterate through objects. Change it to a for…of, and it should fix the problem.

Great job! I look forward to seeing more of your work!
Ben

1 Like

Ben,

thank you for your help. I feel a bit of an idiot, which is fine. I would not normally have put the prompt message in the default string section; all examples I have seen have rarely put a string in the second parameter of prompt .(Ben - 1 oldhack - 0)

As for for…in and for…of; I’ll have to do some reading so that I’m not caught out like that again. Was it an obvious mistake to make? (Ben - 2 oldhack - 0).

Thank you.

1 Like

Don’t worry, it’s a completely normal part of programming, and definitely nothing to beat yourself up over.

No one has ever written bug-free code. This is why testing exists, and why TypeScript is so popular. Both are tools that allow programmers to find the bugs they’ve created before they are unleashed upon users.

1 Like

Ben,

thank you for the links. I’ve heard about TypeScript being a superscript of JavaScript. Is learning the syntax worth the effort to have better error detection and recompiling of code to JavaScript? At this point, I’m still getting to grips with JavaScript syntax, but having coded a few TypeScript examples, I can see the attraction, particularly when using the Object Oriented approach.

The questions are, do I learn TypeScript; do I learn ES5; or do I continue to learn ES6? ES6 certainly seems the way to go; ES5 is still relevant, but TypeScript seems supplementary to a programmer’s knowledge and more useful in a production environment. The jury is out on the question of TypeScript.

1 Like

Hey there,

I wouldn’t worry about TypeScript until you’re comfortable working on full projects in JavaScript. Trying to learn it now probably wouldn’t provide any added benefit.

As for ES5/6, ES5 has mostly stuck around because it has taken a long time for software like web browsers to add support for ES6.

ES5 is also a fairly straightforward way to pick up the basic concepts of JavaScript. You can then integrate ES6 features as you learn them.

2 Likes

Sir . What is the usage of for of loop here.we can do it directly sir. I am in dilemma to solve this kind of problem sir. What can I do to become a good programmer sir

Sorry but what is going on with my code. I’m not getting an error but it won’t pass. Help?

Heelo teacher
I have question

I have done quiz somehow
But about the code i have some questions

Since the code i wrote is same as solution

But i dont understand that
from where(what code) those output (which is circled by red color pen on the image) comes.

So i’ve tried to figure it out.
By console.log coding

  1. I added ‘Console.log(makeMap(stringSequence)
    btw two console.log codes at the very bottom of the code.

And there appear same object but with
One more line of [object, object]
Here again i got curious that
Where these [object, object] comes from?

Thank you for helping all the time-:slight_smile:

1 Like

Thank you a lot, si fiican baad udhigtay

This worked for me, can try it

I hope it works