Interview Questions

  • View all interview questions
  • blank
  • Basic array manipulation with functions in JavaScript
    The following is an example question taken from the Hack Reactor blog (the original source link is below):
    (1) Create an array of numbers and save the array to a variable (2) Iterate through the array using a loop (3) Create a function that will multiply each number in the array by two (4) Pass each number in the array to this function (5) Save the original numbers and the doubled results as key-value pairs in an object


    Code for step 1

    In JavaScript, an array is basically a list that stores any number of things, in this case our array will store a list of numbers. We will store our array in the arr variable.
    // create an array of arbitrary numbers and store the 
    // array in the variable arr
    var arr = [1, 2, 5, 7, 10];   
    // now we can access each number in the array
    arr[0]; // will return 1
    arr[1]; // will return 2
    arr[4]; // will return 10    

    You need to be a premium member to see the rest of this question and code.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “If you're able to solve Medium Coderbyte problems and have a good understanding of web development basics [...] then you are probably ready for admissions at the top schools.” ― Huntly Mayo-Malasky
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 11/24/15 | javascript, bootcamp, array, Codesmith, Fullstack Academy, Hack Reactor, MakerSquare
  • +
  • 9
  • -
  • It is not necessary to convert arr[i] to a string in the above code. While it is true that keys must be strings, the object's bracket notation will automatically convert any number between the brackets into a string version of that number. So we can delete the line:
     var key = arr[i].toString(); 
    and replace it with:
     obj[key] = value; 
     obj[arr[i]] = value; 
    and it will work exactly the same!
  • +
  • 3
  • -
  • Aw shoot, I made some typing errors in that post. I can't delete it or edit it. But, we can delete this line:
      var key = arr[i].toString(); 
    And the line at the bottom:
      obj[key] = value; 
    can be replaced with:
      obj[arr[i]] = value; 
  • +
  • 2
  • -
  • var numberArray = [1, 2, 3, 4, 5, 6];
    function problem(array) {
      var numberObj = {};
      for (var i = 0; i < array.length; i++) {
        numberObj[array[i]] = multArray(array[i]);
    function multArray(num) {
      return num * 2;
    You do not have to use toString because a key will always be a string;
  • +
  • 1
  • -
  • Can do this too.
    function doubling(array){
    var doubled = {};
    var double ={
      return num*2;
    for(i = 0; i < array.length && i < double.length; i++){
      doubled[array[i]] = double[i]; 
    return doubled;
    Login to submit a comment