1

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 :)

Giefdonut
  • 89
  • 1
  • 2
  • 9
  • 7
    Sounds like a basic `for` loop to me. Are you familiar with the [modulus operator](http://stackoverflow.com/questions/8900652/what-does-do-in-javascript)? – Blazemonger Jan 04 '16 at 14:57
  • 2
    Have you tried anything? – Madara's Ghost Jan 04 '16 at 14:57
  • I want to output a a name. If the week number is "5" is should out "Jeppe". Like this article does just with nth in css. https://css-tricks.com/how-nth-child-works/ – Giefdonut Jan 04 '16 at 15:00
  • 1
    Do you need to use the arrays, or can you use another structure like an object? – M. Damian Mulligan Jan 04 '16 at 15:01
  • I dont have to use an array at all, it were just my take on it. Im open to any suggestions. The only this that stays the same is the number of weeks. But im gonna add and remove values in the "students" array – Giefdonut Jan 04 '16 at 15:05
  • `studentForTheWeek = students[weekNumber % students.length - 1];` – sabithpocker Jan 04 '16 at 15:16

5 Answers5

1
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/

online Thomas
  • 7,441
  • 5
  • 32
  • 67
0

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);
});
XCS
  • 23,776
  • 24
  • 82
  • 135
0

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
user3896501
  • 2,695
  • 1
  • 16
  • 22
0

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....

0

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

guysigner
  • 2,594
  • 1
  • 15
  • 23