Using Objects for Lookups this is confusing

Tell us what’s happening:

I am having trouble understanding what must be done here.
I used my own kind of psuedo code bellow what i think must be done but it’s not passing

Your code so far


const range = (start, end) => {
if (end === start) {
    return [start];
} else {
    var numbers = range(start, end - 1);
    numbers.push(end);
    return numbers;
}
};
var lookup = var;
lookup [result]
/* switch statment convert into lookup
variable lookup
search vlue
connect to string result variable*/

// Sum numbers in array
const sum = (array) => {
if (array.length === 0) {
    return 0;
} else {
    console.log(array);
    var recursion = array[0] + sum(array.slice(1));
    console.log(array);
    return recursion;  
}
};

console.log(sum(range(1, 10)));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15.

Challenge: Using Objects for Lookups

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

The code you included doesn’t have anything to do with the “Using Objects for Lookups” challenge.

Weird I used the ask for help button inside that lesson called: Basic JavaScript: Using Objects for Lookups
with that code inside it? Could you elaborate what you mean excatly?

Look at your own post. The code in your post has sum and range functions.

well you might be right there i did find it odd that i could’t find the switch one
i go reset the lesson see if it helps :3
Update:
Something is not quite right. A report has been generated and the freeCodeCamp.org team have been notified.
seems like there was an error somwhere

If you still want help with a challenge, update your original post to include what your questions are and what you’ve done so far.

okay i will :3 thanks AL

// Setup
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup = {
    case "alpha":
      result = "Adams";
      break;
    case "bravo":
      result = "Boston";
      break;
    case "charlie":
      result = "Chicago";
      break;
    case "delta":
      result = "Denver";
      break;
    case "echo":
      result = "Easy";
      break;
    case "foxtrot":
      result = "Frank";
  }
var lookup = "var";
lookup [result];
  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");
  • Your syntax for lookup is not object syntax. It looks like you just removed the first line of a switch statement and replaced it with a curly brace.
  • You appear to have a space between “lookup” and “[”
  • You are always using the empty string result in the lookup.
  • You are not using the val argument variable at all.
1 Like

Alright I try again


// Setup

function phoneticLookup(val) {

var result = "";

// Only change code below this line

var lookup = {

"alpha":"Adams",

"bravo":"Boston",

"charlie":"Chicago",

"delta":"Denver",

"echo":"Easy",

"foxtrot":"Frank"

};

lookup [val]; // "val"

var value = result;

result[val]; // "val"

// Only change code above this line

return result;

}

// Change this value to test

phoneticLookup("val"); 

you have this but don’t do anything with this. also, you need to remove the space before the square parenthesis

you never use value after this, you can remove this line

this does not have sense, result is an empty string, plus you not assign this to anything, so even if it was useful, you don’t do anything with it

result is never changed above so you are still returning an empty string

2 Likes

Thanks that helped :3
i removed the extra space
and deleted the extra lines

// Setup

function phoneticLookup(val) {

var result = "";

// Only change code below this line

var lookup = {

"alpha":"Adams",

"bravo":"Boston",

"charlie":"Chicago",

"delta":"Denver",

"echo":"Easy",

"foxtrot":"Frank"

};

var result = lookup;


// Only change code above this line

return result;

}

// Change this value to test

phoneticLookup("val");

the last lines are troublesome

Use it to look up val and assign the associated string to the result variable.
the example they give out is kinda vague for me as well

var value = 2;
alpha[value]; // "Y"

i can only seem to confert it to

var lookup = "val";
lookup [val];

but thats no good either

how do you access an object property using a variable? that’s what you need to remember. there is a challenge just on this if you need to review

be careful of not changing the object before accessing the property. it you write lookup = "val" you are overwriting the object, and so can’t access the property anymore

right now you are just returning the lookup object (per the line result = lookup)

2 Likes