Converting from score to grade - javascript

I am struggling all day with this one. I need to call function scoreGrade in order to get grade as a number. Could you tell me what do I need to correct? I tried with this code, but I can't figure out the rest. :/
function endQuiz() {
var grade = scoreGrade();
if (myAnswers[(lengthofobject-1)] {
var output = "<div class='output'>Резултат<br>";
var questionResult = "NA";
//console.log('Quiz Over');
for (var i = 0; i < myAnswers.length; i++ || "ten-countdown"<1)) {
if (data.quizcontent[i].correct == myAnswers[i]) {
questionResult = '<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>';
correct++;
} else {
questionResult = '<span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>';
}
output = output + '<p>Питање ' + (i + 1) + ' ' + questionResult + '</p> ';
}
var grade = scoreGrade(correct, lengthofobject)
document.getElementById("ocena").innerHTML = grade;
function scoreGrade(){
let score = (correct / lengthofobject) * 100;
let grade;
if (score>=85) {grade ='5'}
else if (score>=70) {grade ='4'}
else if (score>=55) {grade ='3'}
else if (score>=40) {grade ='2'}
else {grade ='1'}
return grade;
}
clearInterval(endTime);
output = output + '<p>Имате ' + correct + ' од ' + lengthofobject + ' тачних одговора.</p></div> ';
document.getElementById("quizContent").innerHTML = output;
} else {
//console.log('not answered');
}

I'm guessing your issue is inside the scoreGrade function, which does not return anything. Let's modify it like so:
function scoreGrade(correct, questions.length) {
let score = (correct/questions.length) * 100;
if (score>=85) { return (<h4>Vaša ocena je 5.</h4>); }
else if (score>=70) { return (<h4>Vaša ocena je 4.</h4>); }
else if (score>=55) { return (<h4>Vaša ocena je 3.</h4>); }
else if (score>=40) { return (<h4>Vaša ocena je 2.</h4>); }
else { return (<h4>Vaša ocena je 1.</h4>); }
}
This function would work, but not with vanilla js. Returning html in js is a jsx practice. If you're not using jsx consider just returning the grade value as a string or integer.

Related

Getting variables outside a bracket and adding it into the variables inside the bracket

i am currently doing a random minterm maxterm mini program.
var totalvar = getRandomInt(3,5);
var main = "";
for (var i = 1; i <= totalvar; i++) {
var test = getRandomInt(1,4);
//alert(test);
var myArray = ["A","B","C","D","A&apos;","B&apos;","C&apos;","D&apos;"];
var text ="";
for (var a = 1; a <= test; a++) {
function random(array) {
return array[Math.floor(Math.random() * array.length)]
}
var testing = random(myArray);
if (testing =="A") {
var testing2 ="A&apos;";
} else if (testing =="A&apos;") {
var testing2 ="A";
} else if (testing =="B") {
var testing2 ="B&apos;";
} else if (testing =="B&apos;") {
var testing2 ="B";
}else if (testing =="C") {
var testing2 ="C&apos;";
} else if (testing =="C&apos;") {
var testing2 ="C";
}else if (testing =="D") {
var testing2 ="D&apos;";
} else if (testing =="D&apos;") {
var testing2 ="D";
}
//alert(testing);
//alert(myArray);
text += testing
var index = myArray.indexOf(testing);
if (index > -1) {
myArray.splice(index, 1);
}
var index = myArray.indexOf(testing2);
if (index > -1) {
myArray.splice(index, 1);
}
}
var impt = totalvar - i;
var frontbracket = main.split("(").length;
var backbracket = main.split(")").length;
if (impt >= 2) {
var brackets = getRandomInt(1,3);
var chances = getRandomInt(1,3);
var chances1 = getRandomInt(1,3);
var lastLetter = main.charAt(main.length - 1);
alert(frontbracket);
alert(backbracket);
if (frontbracket == backbracket) {
if (brackets == 1) {
text = "(" + text;
if (main == "") {
main = text;
}else
main += "+" + text;
} else {
if (main == "") {
main = text;
} else if ( lastLetter == ')') {
if ( chances !== 1) {
main += text;
}else
main += "+" + text;
}else
main += "+" + text;
}
}
else if (frontbracket != backbracket){
text = text + ")";
main += "+" + text;
}
} else if (frontbracket != backbracket){
text = text + ")";
main += "+" + text;
}
else {
var brackets = getRandomInt(1,3);
var chances = getRandomInt(1,3);
var lastLetter = main.charAt(main.length - 1);
if (brackets == 1) {
text = "(" + text + ")";
if (main == "") {
main = text;
} else if ( lastLetter == ')') {
if ( chances !== '1') {
main += text;
}else
main += "+" + text;
}else
main += "+" + text;
} else {
if (main == "") {
main = text;
} else if ( lastLetter == ')') {
if ( chances !== 1) {
main += text;
}else
main += "+" + text;
}else
main += "+" + text;
}
}
}
Currently i am trying to create random questions like
(ABC+C'D'A')C+C'B'
BAC'D'+(CB'A'D)(B'A)
BCA+(B'C)(A'C')
(BC'D'A+CADB')A'DC'+B'+(D'A')
(BC'D'A+CADB')+B'+(D'A')
So based on this 4 questions, i can solve qn 2, qn 3, qn 5 by using the replace function but for qn1 and qn4,
i have to check if there is a plus sign inside the bracket and if there are variables outside the bracket, i will need to add the variables inside.
If there are no variables outside of the bracket like Qn5, i will just remove the brackets.
like this qn5
BC'D'A+CADB'+B'+D'A'
For example qn4 should look like this after the function is done with it
BC'D'AA'DC'+CADB'A'DC'+B'+D'A'
May i ask for some advice regarding this please?
on a function that checks whether there is a plus sign inside the bracket then check whether there are any variables directly outside the bracket and if there is, the variable outside must be added to the variables inside the bracket

javascript function to add values outside of bracket into values inside the bracket

i am trying to create a function that automatically create questions to do.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var totalvar = getRandomInt(2,4);
var main = "";
for (var i = 1; i <= totalvar; i++) {
var test = getRandomInt(1,3);
// alert(test);
var myArray = ["A","B","C","A&apos;","B&apos;","C&apos;"];
var text ="";
for (var a = 1; a <= test; a++) {
function random(array) {
return array[Math.floor(Math.random() * array.length)]
}
var testing = random(myArray);
if (testing =="A") {
var testing2 ="A&apos;";
} else if (testing =="A&apos;") {
var testing2 ="A";
} else if (testing =="B") {
var testing2 ="B&apos;";
} else if (testing =="B&apos;") {
var testing2 ="B";
}else if (testing =="C") {
var testing2 ="C&apos;";
} else if (testing =="C&apos;") {
var testing2 ="C";
}
//alert(testing);
//alert(myArray);
text += testing
var index = myArray.indexOf(testing);
if (index > -1) {
myArray.splice(index, 1);
}
var index = myArray.indexOf(testing2);
if (index > -1) {
myArray.splice(index, 1);
}
}
var brackets = getRandomInt(1,3);
var chances = getRandomInt(1,3);
var lastLetter = main.charAt(main.length - 1);
if (brackets == 1) {
text = "(" + text + ")";
if (main == "") {
main = text;
} else if ( lastLetter == ')') {
if ( chances !== 1) {
main += text;
}else
main += "+" + text;
}else
main += "+" + text;
} else {
if (main == "") {
main = text;
} else if ( lastLetter == ')') {
if ( chances !== 1) {
main += text;
}else
main += "+" + text;
}else
main += "+" + text;
}
}
I have manage to get it to display questions that i want
B'C'+(A'C'B')+BCA
B+(C')(CAB)
A'BC+(C')A+AB'
B'C'+AB(A'C'B')+BCA
I am stucked as i couldnt get the function for the next step where it multiples the value outside the bracket
B'C'+A'C'B'+BCA
B+C'CAB
A'BC+C'A+AB'
B'C'+ABA'C'B'+BCA
The above is what i hope to achieve but im unable to create the function out
any tips guys?
use replace:
var str = "B'C'+(A'C'B')+BCA";
var response = str.replace(/([\(\)]+)/g, '');
console.log(response);
// output: "B'C'+A'C'B'+BCA"

Javascript Rock Paper Scissors not working

I tried making rock paper scissors in JS. I wanted it to be so that it would count points and announce the winner. But my code is a mess. The computer always wins, always prints that you lose. I cant really make out what the issues are. I also tried to make it so that after every round, you can make a new choice, but that didnt really work out as planned either.
var pcChoice = ["Steen", "Papier", "Schaar"];
var userChoice = prompt("Steen, papier of schaar?");
var pcWins = 0;
var userWins = 0;
var totalWins = pcWins + userWins
var win = ("Je hebt de ronde gewonnen!")
var Verlies = ("Je hebt verloren")
function randomNumber() {
return (Math.floor(Math.random() * 3));
}
console.log("Computer koos: " + pcChoice[randomNumber()]);
console.log("Jij koos: " + userChoice);
if (pcWins + userWins === 2) {
prompt("Nieuwe keuze")
}
if (pcWins + userWins === 1) {
prompt("Nieuwe keuze")
}
while (pcWins + userWins < 3) {
if (userChoice === pcChoice) {
console.log("Gelijkspel");
} else if (userChoice === "Steen" && pcChoice === "Schaar") {
console.log(win) + userWins++
} else if (userChoice === "Papier" && pcChoice === "Steen") {
console.log(win) + userWins++
} else if (userChoice === "Schaar" && pcChoice === "Papier") {
console.log(win) + userWins++
} else {
console.log(Verlies) + pcWins++
}
}
// Het volgende zal de winner uitprinten
if (pcWins > userWins) {
console.log("De computer wint!")
} else {
console.log("Je hebt gewonnen")
}
A lot of typos and beginner mistakes.
I have cleaned your code in the snippet below.
Always make sure your While loops don't loop indefinitely!
var pcChoices = [
'Steen',
'Papier',
'Schaar'
];
var userChoice = prompt('Steen, papier of schaar?');
var pcWins = 0;
var userWins = 0;
var totalWins = pcWins + userWins;
var win = ('Je hebt de ronde gewonnen!');
var Verlies = ('Je hebt verloren');
function randomNumber() {
return (Math.floor(Math.random() * 2));
}
console.log('Computer koos: ' + pcChoices[randomNumber()]);
console.log('Jij koos: ' + userChoice);
if (pcWins + userWins === 2) {
prompt('Nieuwe keuze');
}
if (pcWins + userWins === 1) {
prompt('Nieuwe keuze');
}
while (pcWins + userWins < 3) {
var pcChoice = pcChoices[randomNumber()];
if (userChoice === pcChoice) {
console.log('Gelijkspel');
break;
}
else if (userChoice === 'Steen' && pcChoice === 'Schaar') {
console.log(win);
userWins++;
}
else if (userChoice === 'Papier' && pcChoice === 'Steen') {
console.log(win);
userWins++;
}
else if (userChoice === 'Schaar' && pcChoice === 'Papier') {
console.log(win);
userWins++;
}
else {
console.log(Verlies);
pcWins++;
}
} // Het volgende zal de winner uitprinten
if (pcWins > userWins) {
console.log('De computer wint!');
}
else {
console.log('Je hebt gewonnen');
}
I hope this helps.
It takes things one round at a time rather than all at once. The result or a round is calculated by looking up a little table. There is no reason that your version of calculating a win would not work though.
Please forgive my Dutch.
var choice = {STEEN: 0, PAPIER: 1, SCHAAR: 2};
var resultMatrix = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function playRound(round){
var userInput = prompt("Round " + round + ": steen, papier of schaar?");
var userChoice = choice[userInput.toUpperCase()];
var pcInput = Object.keys(choice)[getRandomInt(0,2)];
var pcChoice = choice[pcInput];
var result = resultMatrix[userChoice][pcChoice];
console.log("Ronde " + round);
console.log("Jij koos: " + userInput.toUpperCase());
console.log("Computer koos: " + pcInput);
switch(result){
case -1:
console.log("De computer wint!");
break;
case 0:
console.log("Gelijkspel");
break;
case 1:
console.log("Je hebt gewonnen");
break;
}
console.log(" ");
return result
};
var totalUserWins = 0;
for(var i=1; i<=3; i++){ totalUserWins += playRound(i); }
console.log("Laatste");
if (totalUserWins > 0) { console.log("Je hebt gewonnen"); } else
if (totalUserWins === 0) { console.log("Het is een gelijkspel"); } else
if (totalUserWins < 0) { console.log("De computer wint!"); }

No console output from code

I've been working on a simple script I can use for a JavaScript/HTML RPG game, but when building the battle script, I no getting any console output. I have tried everything I can think of but still no luck and I started to wonder if I've made a coding error somewhere.
var player = {hp:100, attack:10, defence:10, speed:20};
var enemy = {hp:100, attack:10, defence:10, speed:20};
function playeratk(){
console.log("enemy " + enemy.hp);
var pAtk = Math.floor(Math.random() * player.attack) + 1;
console.log(pAtk);
enemy.hp = enemy.hp - pAtk;
console.log("enemy " + enemy.hp);
console.log("-----");
}
function enemyatk(){
console.log("player " + player.hp);
var eAtk = Math.floor(Math.random() * enemy.attack) + 1;
console.log(eAtk);
player.hp = player.hp - eAtk;
console.log("player " + player.hp);
console.log("-----");
}
function battle() {
if (player.hp < 1) {
console.log("Enemy wins");
}
else if (enemy.hp < 1){
console.log("Player Wins");
}
else {
var pSpeed = Math.floor(Math.random() * player.speed) + 1;
var eSpeed = Math.floor(Math.random() * enemy.speed) + 1;
if (player.speed > enemy.speed){
playeratk();
enemyatk();
battle();
}
else if(enemy.speed > player.speed){
enemyatk();
playeratk();
battle();
}
else {
playeratk();
enemyatk();
battle();
}
}
}
You have to call the battle() function at least once outside of itself. Maybe at the end of the script.

Javascript code broken

This is my first attempt at a little more complex code structure. The thing is my IDE says it technically works , jsfiddle says it doesn't, it actually initializes only the two confirm functions that I declared "confirmUserName();" and "confirmFullName();"
Can someone explain why i did such a horrible job.
var userList = [];
var username = "";
var fullname = "";
var addUserName = addUser(username, userList); v
var addFullName = addUser(fullname, userList);
function addUser(usrName, list) {
if(list.length == 0) {
list.push(usrName); // userlist empty add the new user
} else {
for (var i = 0; i < list.length; i++) {
if(list[i] == undefined) {
list[i] = usrName;
return list;
} else if(i == list.length - 1) {
list.push(usrName);
return list;
}
}
}
} // Function that adds user and name to list
var usernameQuery;
function confirmUserName() {
confirm("Is " + username + " your first choice?");
if (confirmUserName == true) {
return fullnameQuery;
} else {
return usernameQuery;
}
}
var fullnameQuery; /
function fullnameConfirm() {
confirm("Is " + fullname + " your first choice ");
if (fullnameConfirm == true) {
return startRide;
} else {
return fullnameQuery;
}
}
if(username == undefined) {
usernameQuery = function() {
username = prompt("You are the first user to play, \n" +
" Chose and let the game begin !");
return addUserName;
};
} else {
usernameQuery = function() {
username = prompt("What username whould you like to have ? \n" +
" Chose and let the game begin !");
return addUserName;
};
}
confirmUserName();
if(fullname == undefined) {
fullnameQuery = function() {
fullname = prompt("Enter your real name !");
return addFullName;
};
} else {
fullnameQuery = function() {
fullname = prompt("Enter your real name!");
return addFullName;
};
}
fullnameConfirm();
There is a lot wrong with the code you posted -- I'll just take one chunk:
function confirmUserName() {
// The return value of `confirm` is ignored.
confirm("Is " + username + " your first choice?");
// confirmUserName is the name of your function.
// You sould be using `===` instead of `==`
// Or, not comparing against true at all.
if (confirmUserName == true) {
return fullnameQuery;
} else {
return usernameQuery;
}
}
Fixed function:
function confirmUserName() {
var res = confirm("Is " + username + " your first choice?");
if (res) {
return fullnameQuery;
} else {
return usernameQuery;
}
}
It does not throw Errors with this, but I dont know in which situation you want your code to be implemented and what it should do, I hope this is what you need:
var userList = [];
var username = "";
var fullname = "";
var addUserName = addUser(username, userList);
var addFullName = addUser(fullname, userList);
function addUser(usrName, list) {
if (list.length === 0) {
list.push(usrName); // userlist empty add the new user
} else {
for (var i = 0; i < list.length; i++) {
if (list[i] === undefined) {
list[i] = usrName;
return list;
} else if (i == list.length - 1) {
list.push(usrName);
return list;
}
}
}
} // Function that adds user and name to list
var usernameQuery;
function confirmUserName() {
confirm("Is " + username + " your first choice?");
if (confirmUserName === true) {
return fullnameQuery;
} else {
return usernameQuery;
}
}
var fullnameQuery;
function fullnameConfirm() {
confirm("Is " + fullname + " your first choice ");
if (fullnameConfirm === true) {
return startRide;
} else {
return fullnameQuery;
}
}
if (username === undefined) {
usernameQuery = function () {
username = prompt("You are the first user to play, \n" +
" Chose and let the game begin !");
return addUserName;
};
} else {
usernameQuery = function () {
username = prompt("What username whould you like to have ? \n" +
" Chose and let the game begin !");
return addUserName;
};
}
confirmUserName();
if (fullname === undefined) {
fullnameQuery = function () {
fullname = prompt("Enter your real name !");
return addFullName;
};
} else {
fullnameQuery = function () {
fullname = prompt("Enter your real name!");
return addFullName;
};
}
fullnameConfirm();

Categories

Resources