Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Hashmap functionality added #42

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
let array = require("./data/data.json");
let inspireArray = require("./data/inspire.json");
let viewed = new Array(array.length); //hashtable


function initialize() {
//call this function before using the getNewRandomLine()
viewed.fill(0, 0);
}
let favline = {};

function randomInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
return Math.floor(Math.random() * (max - min) + min);
}

module.exports.getRandomLine = function (type) {
let index = randomInt(0, array.length);
switch (type) {
case "isp":
index = randomInt(0, inspireArray.length);
return inspireArray[index].line;
default:
return array[index].line;
}
module.exports.getRandomLine = function(type) {
let index = randomInt(0, array.length);
viewed[index] = 1;
switch (type) {
case "isp":
index = randomInt(0, inspireArray.length);
return inspireArray[index].line;
default:
return array[index].line;
}
};
module.exports.getNewRandomLine = function() {
//new function to know which line was prevoiulsy generated and to not generate it again this time

module.exports.getLines = function (type) {
let index = randomInt(0, array.length);
switch (type) {
case "isp":
index = randomInt(0, inspireArray.length);
favline.line = inspireArray[index].line;
favline.book = inspireArray[index].book;
favline.author = inspireArray[index].author;
return favline;
default:
favline.line = array[index].line;
favline.book = array[index].book;
favline.author = array[index].author;
return favline;
}
let index = randomInt(0, array.length);
let cnt = Number(array.length);
while (!viewed[index] && cnt >= 0) {
//O(1) search
index = randomInt(0, array.length);
cnt--;
}
if (cnt < 0) {
index = randomInt(0, array.length);
}
viewed[index] = 1;
return array[index].line;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, here it will check if the line is previously generated or not, and if it is not generated then it will return the line?
It will be great if you can provide a short description of what your code exactly does

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ohk i will just desribe those

};

module.exports.getLines = function(type) {
let index = randomInt(0, array.length);
switch (type) {
case "isp":
index = randomInt(0, inspireArray.length);
favline.line = inspireArray[index].line;
favline.book = inspireArray[index].book;
favline.author = inspireArray[index].author;
return favline;
default:
favline.line = array[index].line;
favline.book = array[index].book;
favline.author = array[index].author;
return favline;
}
};