I need to allocate urls to students. And my website gives only JavaScript playground in which some variables are predefined within the server and I can only use them and their results and not to change them but I'm able to define new variables, functions etc..
What I'm looking for is to use the predefined variables' results and create new ones.
So the variable I'm talking about is "l18", the student name variable. I need to make every single student a url and I'm allowed to do this in that playground (define new).
I give every student a random number and the result is for example: Jack: 65214587, Peter: 84562321, Sally: 25412365, Richard: 96352148,etc.. The url begins with, for example, https://www.students.com/StudentCode= and after the "=" sign every student code should be added so for Peter we'll have https://www.students.com/StudentCode=84562321
Better clarify the question more: l18 gives you up names and you need to define variables that uses these names to allocate to their results (for example Peter) a number (84562321) to be used in url variable. Let's give it a result and suppose that l18 gives you Peter, then how will the code be? Is it something like (sorry about the codes, they're not even beginner level):
Const StudentCode = {
Jack: 65214587,
Peter: 84562321,
Sally: 25412365,
Richard: 96352148
}
if (l18 = $StudentCode[
//*one of these student names in Const StudentCode*
]) {
let url = "https://www.students.com/StudentCode=" + [
//*the student code (next to student name obtained from l18)
]
}
console.log(url)
Let me say that to clarify again that I don't aim to make any changes to l18, cause it's predefined.
const StudentCode = [
{"id":"118","name":"Jack","code":"65214587"},
{"id":"119","name":"Peter","code":"84562321"},
{"id":"120","name":"Sally","code":"25412365"},
{"id":"121","name":"Richard","code":"96352148"}
];
var checkcode = '65214587'; // How you get this code is up to you //
var match= StudentCode.filter(function(item) { return item.code=== checkcode });
var url_code = match[0].id;
var url_name = match[0].name;
var url = "https://www.students.com/StudentCode=" + url_code;
console.log("ID from CODE "+checkcode);
console.log("Link for "+url_name);
console.log(url);
var checkid = '118'; // How you get this code is up to you //
var match2= StudentCode.filter(function(item) { return item.id=== checkid });
var url_code = match2[0].code;
var url_name = match2[0].name;
var url = "https://www.students.com/StudentCode=" + url_code;
console.log("CODE from ID "+checkid);
console.log("Link for "+url_name);
console.log(url);
<script src="https://code.jquery.com/jquery.js"></script>
You have not explained it that clearly because no where in your data is any reference to 118? But if I understand correctly, you want to create an array with the students id, name and code. Then when you have the student code you want to get the student id to put in a link or vice versa?
If that is correct, then this will work.
const StudentCode = [
{"id":"118","name":"Jack","code":"65214587"},
{"id":"119","name":"Peter","code":"84562321"},
{"id":"120","name":"Sally","code":"25412365"},
{"id":"121","name":"Richard","code":"96352148"}
];
var checkcode = '65214587'; // How you get this code is up to you //
var match= StudentCode.filter(function(item) { return item.code=== checkcode });
var url_code = match[0].id;
var url_name = match[0].name;
var url = "https://www.students.com/StudentCode=" + url_code;
console.log("ID from CODE "+checkcode);
console.log("Link for "+url_name);
console.log(url);
var checkid = '118'; // How you get this code is up to you //
var match2= StudentCode.filter(function(item) { return item.id=== checkid });
var url_code = match2[0].code;
var url_name = match2[0].name;
var url = "https://www.students.com/StudentCode=" + url_code;
console.log("CODE from ID "+checkid);
console.log("Link for "+url_name);
console.log(url);
This code simply creates an array with your data, get the code you want and searches for it in the array and returns the id you need and the other way round too. Note, there is no error checking for if the code does not exist in it!
This should point you in the right direction hopefully