Reputation: 89
In the example below im trying to attach the week number to a person dynamically. If the week number is "5" i want to write out the name "Jeppe". https://jsfiddle.net/wgw8yhnL/
That means if i add or remove a person to the "students" array it will still match values to the current numbers of persons.
var weeks = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];
I want to match the names, with numbers like this "Jeppe" with the numbers 1, 5, 9 "Tommy" with the numbers 2, 6, 10 "Rene" with the numbers 3, 7, 11 "Charlotte" with the numbers 4, 8, 12
Hope you can help me :)
Upvotes: 1
Views: 126
Reputation: 9381
Date.prototype.getWeek = function() {
// Create a copy of this date object
var target = new Date(this.valueOf());
// ISO week date weeks start on monday
// so correct the day number
var dayNr = (this.getDay() + 6) % 7;
// ISO 8601 states that week 1 is the week
// with the first thursday of that year.
// Set the target date to the thursday in the target week
target.setDate(target.getDate() - dayNr + 3);
// Store the millisecond value of the target date
var firstThursday = target.valueOf();
// Set the target to the first thursday of the year
// First set the target to january first
target.setMonth(0, 1);
// Not a thursday? Correct the date to the next thursday
if (target.getDay() != 4) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
// The weeknumber is the number of weeks between the
// first thursday of the year and the thursday in the target week
return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000
}
function getName(weeknr, students) {
mod = weeknr % students.length;
console.log(mod);
return students[mod];
}
var today = new Date();
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];
console.log(getName(today.getWeek(), students));
https://jsfiddle.net/wgw8yhnL/2/
Upvotes: 1
Reputation: 2922
Using students as a struct you can easily display:
{"A":[1,4,7,10],"B":[2,5,8,11],"C":[3,6,9,12]}
for given:
var students = {"A": [], "B": [], "C":[]};
var weeks = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
See this JSFiddle
Upvotes: 0
Reputation: 114
I don't understand very clearly the kind of output you are looking for, and don't understand either why the weeks array is made of strings. Could that array have other values than 1, 2, ..., 12? String values? Anyway, if what you are looking for is a matching code, a thing like this would be enough:
var strRet = "";
for(var i = 1; i < 13; i++)
{
strRet += i + ": ";
var index = (i - 1) % 4;
strRet += students[index] + "\n";
}
alert(strRet);
The new line and the alert just for the sake of the code, of course....
Upvotes: 0
Reputation: 28137
Simply get the index in the students
array based on the current week, you don't even need the weeks
array.
var week = 6;
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];
var index = (week - 1) % 4
var student = students[index]
alert(student);
Here is an improved demo: https://jsfiddle.net/oL2otkj9/2/
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];
function getStudentName(week) {
var index = (week - 1) % students.length;
return students[index];
}
var weekInput = document.getElementById('week');
var output = document.getElementById('student');
weekInput.addEventListener('change', function() {
output.innerHTML = getStudentName(this.value);
});
Upvotes: 0
Reputation: 3037
maybe this helped you
var students = JSON.stringify({
Jeppe: [1,5,9],
Tommy: [2,6,10],
Rene: [3,7,11],
Charlotte: [4,8,12]
});
var needle = '1'; // find Jeppe
var part = students.slice(0, students.indexOf(needle));
var lastIndex = part.lastIndexOf('"');
var name = part.slice(part.slice(0, lastIndex).lastIndexOf('"') + 1, lastIndex); // returns Jeppe
Upvotes: 0