Hi All!
I am doing a small challenge to review my study of Javascript, and I am having trouble with number 5 below. I am not sure how to set the key-value pairs in an object. I know that I could use my ‘num’ constructor to individually create new objects. But, how am I to pass an array element, and the result of the doubling function, into an object? Thanks so much in advance, and please let me know if I can clarify this post-I wasn’t sure how much info would be helpful.
The challenge:
1) Create an array of numbers, save it to a variable
2) Use a loop to iterate through each element of the array
3) Write a separate “doubling” function that returns any number it is given multiplied by two
4) Pass each number from the array to the “doubling” function in turn
5) Save the original numbers and the doubled results as key-value pairs in an object
My Code:
[code]var numArray = [ 1,2,3,4,5,6,7]
for (var i= 0; i<numArray.length; i ++){
//console.log(“The number in cell number ” + i + ” is ” + numArray[i]);
console.log(doubling(numArray[i]));
}
function doubling(n){
n=n*2;
return n;
}
function num(original, doubled){
this.original= original;
this.doubled= doubled;
}