//Define an enumeration type using an object
const data1 = {
ok:"correct",
ng:"incorrect",
end:"no more problem",
index:0//Indexes in use
};
//Numbers used in the question
const aryNum1=[2,3,5];//First number
const aryNum2=[4,1,6];//Second number
//Show number of questions
displayProblemNo("no1", data1.index + 1);
//Print the problem
printCalcProblem();
const btn1 = document.getElementById("btn1");
btn1.addEventListener("click", () =>
{
//Cases where the index is greater than the third question
if(data1.index==3)
{
alert(data1.end);
return;
}
//Evaluate the calculation
judgeCalc();
if(data1.index<3)
{
//Show number of questions
displayProblemNo("no1", data1.index + 1);
//Print the problem
printCalcProblem();
}
});
//Show number of questions
function displayProblemNo(id, no)
{
const obj =document.getElementById(id);
obj.innerText = "No:"+no ;
}
//Print the problem
function printCalcProblem()
{
const obj =document.getElementById("p1");
obj.innerText = String(aryNum1[data1.index]) + " + " + String(aryNum2[data1.index]);
}
//Evaluate the calculation
function judgeCalc()
{
let result = 0;
//Get the result of the user's input
const obj =document.getElementById("ans1");
result = obj.value;
//Check whether the result of the addition is the same as the number entered by the user
if(Number(aryNum1[data1.index] + aryNum2[data1.index]) == result)
{
alert(data1.ok);//correct
}
else
{
alert(data1.ng);//incorrect
}
data1.index++;//Increment to reference the next data
}
This sample asks three questions.
After you answer all three questions, the message "There are no more questions" is displayed and you cannot continue.
//Define an enumeration using an object
const data1 = {
ok:"correct",
ng:"incorrect",
end:"no more problem",
index:0//Indexes in use
};
An object is created and an associative array is used within it.
Therefore, the internal values can be changed.
If you want to use it as an enumeration constant, use freeze, which is explained below,
You can use the object mechanism to use the parts you need as variables.
In this case, the index within the object is used as the problem index for judgment, etc.
Defining constants for objects using freeze
This is a method to combine associative arrays into one object.
This method cannot be rewritten.
Question
Please answer with a few characters.
//Define an enumeration type using an object
const data2 = Object.freeze({
ok:"correct",
ng:"incorrect",
end:"no more problem",
count:3//problme count
});
//String to use in question
const aryData=["word","water","problem"];
let index = 0;
//Show number of questions
displayProblemNo("no2", index + 1);
//display problem
displayProblemString();
const btn2 = document.getElementById("btn2");
btn2.addEventListener("click", () =>
{
//Cases where the index exceeds the number of problem elements
if(index==data2.count)
{
alert(data2.end);
//Testing changing object values using freeze
checkChangeValue();
return;
}
//Determine the number of characters
judgeLength();
//If the following problem exists, perform the following steps:
if(index < data2.count)
{
//Show number of questions
displayProblemNo("no2", index + 1);
//Print the problem
displayProblemString();
}
});
//Testing changing object values using freeze
function checkChangeValue()
{
try{
data2.count = 0;//try to make the change
alert("Step performed");
}catch{
alert("catch:If get an error, go here");
}finally{
alert("finally");
alert("data2.count current value:"+data2.count);
}
}
//Determine the number of characters
function judgeLength()
{
let result = 0;
//Get the result of the user's input
const obj =document.getElementById("ans2");
result = obj.value;
//Determine the length of the string in question
if(aryData[index].length == result)
{
alert(data1.ok);//correct
}
else
{
alert(data1.ng);//incorrect
}
index++;//Increment to reference the next data
}
//display problem
function displayProblemString()
{
const obj =document.getElementById("p2");
obj.innerText = aryData[index];
}
//Show number of questions
function displayProblemNo(id, no)
{
const obj =document.getElementById(id);
obj.innerText = "No:" + no;
}
//Define an enumeration type using an object
const data2 = Object.freeze({
ok:"Correct",
ng:"Incorrect",
end:"No more questions.",
count:3//Number of questions
});
When you use "Object.freeze", you cannot change the values of the internally implemented associative array.
The advantage of this is that when developing a project with multiple people,
it reduces implementation errors that cause object values to be overwritten.
In this sample, the following judgment was made, and the result of using "Object.freeze"
would not allow the value to be overwritten.
//Test changing object value using freeze
function checkChangeValue()
{
try{
data2.count = 0;// Try the change process
alert("Step was executed");
}catch{
alert("catch:If an error occurs, go through here");
}finally{
alert("finally");
alert("Current value of data2.count:"+data2.count);
}
}
[Execution result]
Step
alert result
(1)
Step executed
(2)
finally
(3)
Current value of data2.count: 3
data2.count = 0;
Here, we rewrote the value.
However, as you can see from the alert result, no changes have been made.
We can see that rewriting is not possible.
Unique key using Symbol
Symbol is a mechanism introduced in ES6 (ECMAScript 2015).
It is used as a primitive type to represent a unique and immutable value.
//The result of comparing symbol with the variable
const data3 ={
ok1:Symbol(1),
ok2:Symbol(1),
};
const btn3 = document.getElementById("btn3");
btn3.addEventListener("click", () =>
{
if(data3.ok1.valueOf() === 1)
{
alert("equal");
}
else
{
alert("not equal");
}
});
//Comparison result between variables with the same symbol
const btn4 = document.getElementById("btn4");
btn4.addEventListener("click", () =>
{
if(data3.ok1 == data3.ok1)
{
alert("equal");
}
else
{
alert("not equal");
}
});
//Comparison result where the variable name is different for symbol
const btn5 = document.getElementById("btn5");
btn5.addEventListener("click", () =>
{
if(data3.ok1 == data3.ok2)
{
alert("equal");
}
else
{
alert("not equal");
}
});
valueOf()
Symbol objects can return the value stored in them by executing the valueOf() method.
Values defined using Symbol in JavaScript enumerations (defined in objects)
When compared with primitive values (numbers, strings, etc.), they usually do not match.
This is because Symbols are unique identifiers and are treated as a different type than primitive values.
This is because both strict (===) and non-strict (==) comparisons return false.
[Button execution results]
Button name
Alert result
Comparison result with symbol variable
Mismatch
Comparison result with same variable in symbol
Match
Comparison result with different variable name in symbol