I am writing a program that will create an array of numbers and double the contents of each array and save the result as a key / value pair. I used to hardcode the array, so everything was fine.
Now I changed the logic a bit, I want to take data from users, and then save the value in an array.
My problem is that I cannot figure out how to do this using node.js. I installed the invitation module using npm install prompt and also looked at the documentation, but nothing works.
I know that I am making a small mistake here.
Here is my code:
//Javascript program to read the content of array of numbers //Double each element //Storing the value in an object as key/value pair. //var Num=[2,10,30,50,100]; //Array initialization var Num = new Array(); var i; var obj = {}; //Object initialization function my_arr(N) { return N;} //Reads the contents of array function doubling(N_doubled) //Doubles the content of array { doubled_number = my_arr(N_doubled); return doubled_number * 2; } //outside function call var prompt = require('prompt'); prompt.start(); while(i!== "QUIT") { i = require('prompt'); Num.push(i); } console.log(Num); for(var i=0; i< Num.length; i++) { var original_value = my_arr(Num[i]); //storing the original values of array var doubled_value = doubling(Num[i]); //storing the content multiplied by two obj[original_value] = doubled_value; //object mapping } console.log(obj); //printing the final result as key/value pair
Please help me with this, thanks.
vamosrafa
source share