Interview Questions

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

## Sources

http://www.hackreactor.com/blog/coding+schools+development+bootcamp+coding+bootcamp+learn+javascript

## 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
```

## Code for step 2

This part of the question is asking you to simply iterate through the whole array. We'll create a for loop that will access each number in the array, but we don't do anything with these numbers yet.
```for (var i = 0; i < arr.length; i++) {
// do something with arr[i] here...
}
```

## Code for step 3

We'll create a function that will take in some number and return the number multiplied by two.
```function doubling(num) {
return num * 2;
}

// you can test the above function with the following code
doubling(2); // returns 4
doubling(6); // returns 12
```

## Code for step 4

Now we can actually do something with the numbers when we use the for loop. We'll call the doubling function on each number in the array. We're not actually storing the result anywhere though, the code below simply calls the function on each number in the array.
```for (var i = 0; i < arr.length; i++) {
doubling(arr[i]);
}
```

## Code for step 5

The last step is to store each number along with its double in an object. An object is a container for elements that are accessed by a key and value. In our case, the key will be the original number and the value will be the doubled value we get from calling the doubling function.
```// create an empty object
var obj = {};

for (var i = 0; i < arr.length; i++) {

// the key will be the original number
// (we convert the number to a string because keys in JavaScript cannot be numbers)
var key = arr[i].toString();

// the value will be the doubled number
var value = doubling(arr[i]);

// store the key, value pair in the object
// e.g. {"5": 10}
// e.g. {"5": 10, "6": 12}
obj[key] = value;

}
```

## Final code

Below is the code with all the steps put together. Press the run code button to see the result, and you can also change the array values in the code.
```function doubling(num) {
return num * 2;
}

// create an array of arbitrary numbers and store the
// array in the variable arr
var arr = [1, 2, 5, 7, 10];

// create an empty object
var obj = {};

for (var i = 0; i < arr.length; i++) {

// the key will be the original number
var key = arr[i].toString();

// the value will be the doubled number
var value = doubling(arr[i]);

obj[key] = value;

}

// print the final object
obj;
```
mrdaniel published this on 11/24/15 |
• +
• 14
• -
• 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; ` with ` obj[arr[i]] = value; ` and it will work exactly the same!
• +
• 3
• -
• ```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]); } console.log(numberObj); } function multArray(num) { return num * 2; } problem(numberArray); ``` You do not have to use toString because a key will always be a string;
• +
• 2
• -
• 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; `
• +
• 0
• -
• Or a short version: `const doubling=array=>array.reduce((res,e)=>(res[e]=e*2)&&res,{});`
• +
• 0
• -
• Can do this too. ```function doubling(array){ var doubled = {}; var double = array.map(function(num){ return num*2; }); for(i = 0; i < array.length && i < double.length; i++){ doubled[array[i]] = double[i]; } return doubled; } doubling([2,3,4,5,6])```
• +
• 0
• -
• why is the obj[key] = value ? thanks!
• +
• -1
• -
• using map: `array.map((element)=>({[`\${element}`]:element*2}));`