How to return key of Object when the value is known

Tell us what’s happening:

How to return key of Object when the value is known, Please have a look at the comment i made inside my code.

Your code so far


function fearNotLetter(str) {

 //make a obj 
 let alphabetObj={
   a:1,
   b:2,
   c:3,
   d:4,
   e:5,
   f:6,
   g:7,
   h:8,
   i:9,
   j:10,
   k:11,
   l:12,
   m:13,
   n:14,
   o:15,
   p:16,
   q:17,
   r:18,
   s:19,
   t:20,
   u:21,
   v:22,
   w:23,
   x:24,
   y:25,
   z:26
 }
 let checkIsFullRange = alphabetObj[str.charAt(str.length-1)]-alphabetObj[str.charAt(0)]+1;

if(str.length===checkIsFullRange){
  return undefined;
}else{
 for(let i=0;i<str.length;i++){
   if(alphabetObj[str[i]]-alphabetObj[str[i+1]]===-2){

       //For test case abce at i=2 this condition will become true.Here I want to return KEY of missing aplhabet i.e d and it's value is four. How to do it ?
   }
 }
 
}
}
let alphabetObj={
   a:1,
   b:2,
   c:3}




Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Missing letters

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters

you really can’t, as keys in an object are not in any particular order
it seems you have found a wall, maybe you need to backtrack a little and find a slighlty different approach

You’re creating an object to give you index values for each character. If that’s the approach you’re considering, why not use the already-available codes?

Each letter has an ASCII value, and in javascript, we can easily get to them using charCodeAt(...).

console.log(`I can view the alphabet as numbers!
 a: ${'a'.charCodeAt(0)},
 b: ${'b'.charCodeAt(0)}
 and so on to z: ${'z'.charCodeAt(0) }`);

// And we can do it in reverse!
console.log(`I can also go the other way: 
ASCII code 97: ${ String.fromCharCode(97) }, 
ASCII code 98: ${ String.fromCharCode(98) }, 
all the way to ASCII code 122: ${ String.fromCharCode(122) }`)

You can use that object, and you can get property names by their values, but it’s a bit of a pain. But, using the tools javascript already provides you with, it shouldn’t be too difficult to work out.

They are, they’re in insertion order, OP will be able to get them in the exact same order they’re defined there.

Object.entries(alphabetObj) would give you an object like [['a', 1], ['b', 2], ...] @DVGY, you could get it that way (you can then use the array method find for example). However, this is a slightly bizarre way to do it, I’m still not quite sure what your thinking is here. If you really want to do something similar, use the character codes, as @snowmonkey says

1 Like

Pursuing another question (looking at the anagram dictionary post, and considering whether the Map would be a viable route to consider), I came across this line:

The keys in Map are ordered while keys added to object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.

Sounds like MDN doesn’t agree about object properties being “fetchable” by insertion order…

They are, though, (albeit by convention), afaik no engine in widespread use today does not order string keys in insertion order. And ES2015 specifies that they are guaranteed to be with specific methods (getOwnPropertyNames being the relevant one here). And for…in/Object.keys/etc should have guaranteed ordering added to the spec this year, just sailed straight through the tc39 process (it seems to have gone immediately to the final stage of review?) because it just codifies what everything already implements

1 Like

Yes i changed my code, because this approach is way too difficult. Thanks for Responding @ilenia @snowmonkeym @DanCouper

1 Like