div inside if statement javascript - javascript

For some reason, the div tags are not displayed in the webpage when the program is run. I tested every other component and have figured out that the problem is that the setting of the value of the div tags is in an if statement. The only problem is that I do not know how to fix the problem and still have the program run the way I want it to.
<div id="answerDisplay"></div>
<div id="correctOrNot"></div>
<script>
var div
var div2
var rightAnswer
var problemNum =Math.floor((Math.random() * 3) + 1);
if (problemNum == 1){
rightAnswer = 14;
div = document.getElementById("answerDisplay");
div.textContent = "4 + 2 * (10 - 3 * 2) / 4 + 8 = ?";
}
else if (problemNum == 2){
rightAnswer = 8;
div = document.getElementById("answerDisplay");
div.textContent = "(6 - 3) * 2 + (3^2 - 5) / 2 = ?";
}
else if (problemNum == 3){
rightAnswer = 6;
div = document.getElementById("answerDisplay");
div.textContent = "5 - 3 + 4^2 / (8 - 4 / 2) = ?";
}
else if (problemNum == 4){
rightAnswer = 3;
div = document.getElementById("answerDisplay");
div.textContent = "5^2 - (6 * 2) * 2 * (5 - 2) = ?";
}
function checkRightAnswer (){
var answer = document.getElementById('Input').value
if (problemNum == 1 && answer == 14){
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum =Math.floor((Math.random() * 3) + 1);
}
else if (problemNum == 2 && answer == 8){
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum =Math.floor((Math.random() * 3) + 1);
}
else if (problemNum == 3 && answer == 6){
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum =Math.floor((Math.random() * 3) + 1);
}
else if (problemNum == 4 && answer == 3){
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum =Math.floor((Math.random() * 3) + 1);
}
else {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Incorrect, try again";
}
}
</script>

Your code seems to work fine. I don't see the input container included in the code snippet you provided. Perhaps you need to make sure the id is correct there? I cleaned up the code a bit:
var div, div2, rightAnswer, problemNum = Math.floor((Math.random() * 3) + 1);
if (problemNum === 1) {
rightAnswer = 14;
div = document.getElementById("answerDisplay");
div.textContent = "4 + 2 * (10 - 3 * 2) / 4 + 8 = ?";
} else if (problemNum === 2) {
rightAnswer = 8;
div = document.getElementById("answerDisplay");
div.textContent = "(6 - 3) * 2 + (3^2 - 5) / 2 = ?";
} else if (problemNum === 3) {
rightAnswer = 6;
div = document.getElementById("answerDisplay");
div.textContent = "5 - 3 + 4^2 / (8 - 4 / 2) = ?";
} else if (problemNum === 4) {
rightAnswer = 3;
div = document.getElementById("answerDisplay");
div.textContent = "5^2 - (6 * 2) * 2 * (5 - 2) = ?";
}
function checkRightAnswer() {
var answer = document.getElementById('Input').value;
if (problemNum === 1 && answer === 14) {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum = Math.floor((Math.random() * 3) + 1);
} else if (problemNum === 2 && answer === 8) {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum = Math.floor((Math.random() * 3) + 1);
} else if (problemNum === 3 && answer === 6) {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum = Math.floor((Math.random() * 3) + 1);
} else if (problemNum === 4 && answer === 3) {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Correct";
problemNum = Math.floor((Math.random() * 3) + 1);
} else {
div2 = document.getElementById("correctOrNot");
div2.textContent = "Incorrect, try again";
}
}
<div id="answerDisplay"></div>
<div id="correctOrNot"></div>
<input type="text" id="Input" />

Here is a way you would rewrite it to use an array of objects as ErikE suggested in his comment.
var questions = [
{
q: "4 + 2 * (10 - 3 * 2) / 4 + 8 = ?",
a: 14
},
{
q: "(6 - 3) * 2 + (3^2 - 5) / 2 = ?",
a: 8
},
{
q: "5 - 3 + 4^2 / (8 - 4 / 2) = ?",
a: 6
},
{
q: "5^2 - (6 * 2) * 2 * (5 - 2) = ?",
a: 3
}
],
currentQuestion, // the index of the current question will
// be stored here so we can look up the answer later
timerId,
doc = document,
elQuestion = doc.getElementById('question')
elAnswer = doc.getElementById('answer'),
elUserAnswer = doc.getElementById('user-answer'),
elCheck = doc.getElementById('check'),
displayQuestion = function (index) {
currentQuestion = index;
elQuestion.textContent = questions[index].q;
},
displayRandomQuestion = function () {
displayQuestion(Math.floor(Math.random() * questions.length));
},
removeAnswer = function () {
timerId = setTimeout(function () {
elAnswer.textContent = ''
}, 1000);
},
checkAnswer = function () {
var userAnswer = elUserAnswer.value;
clearTimeout(timerId);
// the '' + explicitly converts the .a to a string because userAnswer will be a string
// using the strict comparison operator (===) is a good habit to get into
if (userAnswer === '' + questions[currentQuestion].a) {
elAnswer.textContent = 'Correct!';
displayRandomQuestion();
} else {
elAnswer.textContent = 'Incorrect, try again';
}
removeAnswer();
};
elCheck.addEventListener('click', checkAnswer, false);
displayRandomQuestion();
Working jsfiddle
Doing it this way is shorter, cleaner and easier to maintain as well. Adding a new question/answer pair is as easy as adding another object to the array. No more copy/pasting if statements and other redundant code. In addition since there is only one place where each action is performed (questions/answers are displayed, the answer is checked, etc) if there is a bug with any of these features there is a single well defined place to look for the bug and you only have to fix it once instead of once for every question in your quiz.
The way I have abstracted lots of the work also makes it easier to extend the code in the future. For instance if you further abstract out the random number code:
getRandomIndex = function () {
return Math.floor(Math.random() * questions.length);
},
You can alter displayRandomQuestion so it will never repeat the same question twice in a row:
displayRandomQuestion = function () {
var index;
do {
index = getRandomIndex();
} while (index === currentIndex);
displayQuestion(index);
},
Or even ensure questions never repeat until all the questions have been asked:
askedIndexes = [],
displayRandomQuestion = function () {
var index;
do {
index = getRandomIndex();
} while (askedIndexes.indexOf(index) !== -1);
if (askedIndexes.length === questions.length - 1) {
askedIndexes = [];
}
askedIndexes.push(index);
displayQuestion(index);
},

Related

Why is my javascript not carrying out my "if" statements in a function?

I have a code here.
It is creating the correct noOfChoices when I console.log it, I can see it. But it is not executing the if (noOfChoices === "2") {} scenarios. Can someone please help make this work (see the commented out area in code), thanks.
Also, how can I run diceRoll during this function then use the constants (randomNumber1- randomNumber4) that I got in this roll in the code following? (see the commented out part towards the end)
<!--begin snippet: js hide: false console: true babel: false -->
<!--language: lang - js-->
let links = document.querySelectorAll('#list li')
links.forEach((el) => {
el.addEventListener('click', (event) => {
let numberOfChoices = event.target.innerText
document.getElementById('dropdownMenu').innerHTML = `${numberOfChoices}<span class="caret"></span>`
if (numberOfChoices === "2") {
$("#img3, #img4, .threeChoices, .fourChoices").addClass("invisible")
}
if (numberOfChoices === "3") {
$("#img4, .fourChoices").addClass("invisible");
$("#img3, .threeChoices").removeClass("invisible")
}
if (numberOfChoices === "4") {
$("#img3, #img4, .threeChoices,
.fourChoices ").removeClass("
invisible ");
}
})
})
// Responding to Submit
document.getElementById("submit").addEventListener("click", function (e) {
e.preventDefault();
// Storing Data into variables
var choice1 = $("#choice1").val();
var choice2 = $("#choice2").val();
var choice3 = $("#choice3").val();
var choice4 = $("#choice4").val();
var noOfChoices = $("#dropdownMenu").text();
// Rotate animation
$(".dice").addClass("rotate");
// Changing text to user input
$("#caption1").html(choice1);
$("#caption2").html(choice2);
$("#caption3").html(choice3);
$("#caption4").html(choice4);
console.log(noOfChoices);
// THE FOLLOWING IS NOT WORKING DESPITE noOfChoices BEING CORRECT IN THE PREVIOUS LINE
if (noOfChoices === "2") {
$("#caption1, #caption2").removeClass("invisible");
$("#caption3, #caption4").addClass("invisible");
}
if (noOfChoices === "3") {
$("#caption1, #caption2, #caption3").removeClass("invisible");
$("#caption4").addClass("invisible");
}
if (noOfChoices === "4") {
$(".caption").removeClass("invisible");
}
$("#submit").html("Again");
// SEE HERE IS THE SECOND PROBLEM:
// Rolling Dice
diceRoll();
// Determining winner
if (noOfChoices === "2") {
if (randomNumber1 > randomNumber2) {
$("#title").html(choice1 + "wins! 🏆");
} else if (randomNumber2 > randomNumber1) {
$("#title").html(choice2 + "wins! 🏆");
} else if (randomNumber2 = randomNumber1) {
$("#title").html("Oops, try again!");
}
}
})
function diceRoll() {
// 1st dice
var randomNumber1 = Math.floor(Math.random() * 6 + 1);
var Image1 = "dice" + randomNumber1 + ".png";
document.querySelectorAll("img")[1].setAttribute("src", Image1);
// 2nd dice
var randomNumber2 = Math.floor(Math.random() * 6 + 1);
var Image2 = "dice" + randomNumber2 + ".png";
document.querySelectorAll("img")[2].setAttribute("src", Image2);
// 3rd dice
var randomNumber3 = Math.floor(Math.random() * 6 + 1);
var Image3 = "dice" + randomNumber3 + ".png";
document.querySelectorAll("img")[3].setAttribute("src", Image3);
// 4th dice
var randomNumber3 = Math.floor(Math.random() * 6 + 1);
var Image4 = "dice" + randomNumber3 + ".png";
document.querySelectorAll("img")[4].setAttribute("src", Image4);
}
<!--end snippet-->
EDIT: on hindsight you're using .innerText which returns strings anyways. So this answer is probably wrong, but I'll leave it here nonetheless. #Eugen Sunic's comment should be the correct answer.
Most probably it's a type problem, try using the number type. The differences are shown here:
if (numberOfChoices === "2") {
^ this is a string
if (numberOfChoices === 2) {
^ this is a number
Or if you want type coercion (which I wouldn't recommend) don't use the === operator but ==, like this:
if (numberOfChoices == "2") {
^ coerce numberOfChoices to string, then compare strings
Both solutions should work but the first one is better in my personal opinion because it leads to stricter code practices.

Set default value to an Input field but make it editable (to be able to change it manually)?

I have an online form that takes values from some input fields and does some calculations on them. please check fiddle URL:
https://jsfiddle.net/moeadas/fv027knx/44/
I basically need to set a default value to a specific input field named (Rounded Valuation) name="B200". I want this input to take the value from another field name="B52" as default but I want users to be able to manually edit this field to enter another number (which will be used for other calculations).
Finally I want to hide the "Submit" button in the last stage as this is not really a form and its just an online form which users can play with.
I really appreciate all your kind comments and efforts. Please have a look at the fiddle link to better understand the current structure.
JAVASCRIPT CODE BELOW. CHECK THE JSFIDDLE FOR THE FULL STRUCTURE
function milesIt(num) {
return Math.floor(num).toString().split("").reverse().map((n, i, a) =>
(i + 1) % 3 === 0 && i + 1 != a.length && "," + n || n).reverse().join("");
}
var inputs = document.querySelectorAll("[type='text']");
inputs.forEach(function(input) {
//change the values to include the thousand separator each time the user changes an input
input.onchange = function(e) {
inputs.forEach(function(inp) {
inp.value = inp.value.replace(/[,]/g, '');
});
calcule2();
inputs.forEach(function(inp) {
inp.value = milesIt(inp.value);
});
}
input.value = milesIt(input.value);
});
////////////////////////////////////////////
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab
function showTab(n) {
// This function will display the specified tab of the form ...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
// ... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("nextBtn").innerHTML = "Submit";
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
// ... and run a function that displays the correct step indicator:
fixStepIndicator(n)
}
function nextPrev(n) {
// This function will figure out which tab to display
var x = document.getElementsByClassName("tab");
// Exit the function if any field in the current tab is invalid:
if (n == 1 && !validateForm()) return false;
// Hide the current tab:
x[currentTab].style.display = "none";
// Increase or decrease the current tab by 1:
currentTab = currentTab + n;
// if you have reached the end of the form... :
if (currentTab >= x.length) {
//...the form gets submitted:
document.getElementById("CreativeAcquisitions").submit();
return false;
}
// Otherwise, display the correct tab:
showTab(currentTab);
}
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false:
valid = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
function fixStepIndicator(n) {
// This function removes the "active" class of all steps...
var i, x = document.getElementsByClassName("step");
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(" active", "");
}
//... and adds the "active" class to the current step:
x[n].className += " active";
}
function calcule2() {
var i = 0;
for (i = 0; i <= 50; i++) {
calcule();
}
}
function calcule() {
CreativeAcquisitions.B28.value = parseFloat(CreativeAcquisitions.B17.value) + parseFloat(CreativeAcquisitions.B19.value) + parseFloat(CreativeAcquisitions.B21.value) + parseFloat(CreativeAcquisitions.B23.value) + parseFloat(CreativeAcquisitions.B25.value);
CreativeAcquisitions.B52.value = (parseFloat(CreativeAcquisitions.B4.value) * parseFloat(CreativeAcquisitions.B6.value)) + parseFloat(CreativeAcquisitions.D81.value) +
parseFloat(CreativeAcquisitions.B300.value);
CreativeAcquisitions.B55.value = parseFloat(CreativeAcquisitions.B4.value) * parseFloat(CreativeAcquisitions.B6.value);
CreativeAcquisitions.B56.value = parseFloat(CreativeAcquisitions.B28.value);
//CreativeAcquisitions.B200.value = CreativeAcquisitions.B52.value; //testing
//document.getElementById("B200").value = CreativeAcquisitions.B52.value;
CreativeAcquisitions.B66.value = parseFloat(CreativeAcquisitions.B200.value);
CreativeAcquisitions.B67.value = parseFloat(CreativeAcquisitions.D81.value);
CreativeAcquisitions.B68.value = parseFloat(CreativeAcquisitions.B36.value) + parseFloat(CreativeAcquisitions.B37.value) + parseFloat(CreativeAcquisitions.B38.value) + parseFloat(CreativeAcquisitions.B39.value) + parseFloat(CreativeAcquisitions.B40.value);
CreativeAcquisitions.B300.value = (parseFloat(CreativeAcquisitions.B36.value) + parseFloat(CreativeAcquisitions.B37.value) + parseFloat(CreativeAcquisitions.B38.value) + parseFloat(CreativeAcquisitions.B39.value) + parseFloat(CreativeAcquisitions.B40.value)) * -1;
CreativeAcquisitions.B69.value = parseFloat(CreativeAcquisitions.B41.value);
CreativeAcquisitions.B70.value = (parseFloat(CreativeAcquisitions.B200.value) * parseFloat(CreativeAcquisitions.B46.value)) / 100;
CreativeAcquisitions.B71.value = (parseFloat(CreativeAcquisitions.B200.value) * parseFloat(CreativeAcquisitions.B47.value)) / 100;
CreativeAcquisitions.B72.value = parseFloat(CreativeAcquisitions.B66.value) - parseFloat(CreativeAcquisitions.B67.value) - parseFloat(CreativeAcquisitions.B70.value) - parseFloat(CreativeAcquisitions.B69.value) - parseFloat(CreativeAcquisitions.B71.value);
CreativeAcquisitions.B76.value = parseFloat(CreativeAcquisitions.B17.value);
CreativeAcquisitions.D76.value = (parseFloat(CreativeAcquisitions.B76.value) * parseFloat(CreativeAcquisitions.C76.value)) / 100;
CreativeAcquisitions.B77.value = parseFloat(CreativeAcquisitions.B19.value);
CreativeAcquisitions.D77.value = (parseFloat(CreativeAcquisitions.B77.value) * parseFloat(CreativeAcquisitions.C77.value)) / 100;
CreativeAcquisitions.B78.value = parseFloat(CreativeAcquisitions.B21.value);
CreativeAcquisitions.D78.value = (parseFloat(CreativeAcquisitions.B78.value) * parseFloat(CreativeAcquisitions.C78.value)) / 100;
CreativeAcquisitions.B79.value = parseFloat(CreativeAcquisitions.B23.value);
CreativeAcquisitions.D79.value = (parseFloat(CreativeAcquisitions.B79.value) * parseFloat(CreativeAcquisitions.C79.value)) / 100;
CreativeAcquisitions.B80.value = parseFloat(CreativeAcquisitions.B25.value);
CreativeAcquisitions.D80.value = (parseFloat(CreativeAcquisitions.B80.value) * parseFloat(CreativeAcquisitions.C80.value)) / 100;
CreativeAcquisitions.B81.value = parseFloat(CreativeAcquisitions.B76.value) + parseFloat(CreativeAcquisitions.B77.value) + parseFloat(CreativeAcquisitions.B78.value) + parseFloat(CreativeAcquisitions.B79.value) + parseFloat(CreativeAcquisitions.B80.value);
CreativeAcquisitions.D81.value = parseFloat(CreativeAcquisitions.D76.value) + parseFloat(CreativeAcquisitions.D77.value) + parseFloat(CreativeAcquisitions.D78.value) + parseFloat(CreativeAcquisitions.D79.value) + parseFloat(CreativeAcquisitions.D80.value);
CreativeAcquisitions.B91.value = parseFloat(CreativeAcquisitions.B36.value);
CreativeAcquisitions.B92.value = parseFloat(CreativeAcquisitions.B37.value);
CreativeAcquisitions.B93.value = parseFloat(CreativeAcquisitions.B38.value);
CreativeAcquisitions.B94.value = parseFloat(CreativeAcquisitions.B39.value);
CreativeAcquisitions.B95.value = parseFloat(CreativeAcquisitions.B40.value);
CreativeAcquisitions.B96.value = (parseFloat(CreativeAcquisitions.B52.value) * parseFloat(CreativeAcquisitions.B46.value)) / 100;
CreativeAcquisitions.B97.value = parseFloat(CreativeAcquisitions.B41.value);
CreativeAcquisitions.B98.value = (parseFloat(CreativeAcquisitions.B91.value) + parseFloat(CreativeAcquisitions.B92.value) + parseFloat(CreativeAcquisitions.B93.value) + parseFloat(CreativeAcquisitions.B94.value) + parseFloat(CreativeAcquisitions.B95.value) + parseFloat(CreativeAcquisitions.B96.value) + parseFloat(CreativeAcquisitions.B97.value)) * -1;
CreativeAcquisitions.C98.value = (parseFloat(CreativeAcquisitions.C91.value) + parseFloat(CreativeAcquisitions.C92.value) + parseFloat(CreativeAcquisitions.C93.value) + parseFloat(CreativeAcquisitions.C94.value) + parseFloat(CreativeAcquisitions.C95.value) + parseFloat(CreativeAcquisitions.C96.value) + parseFloat(CreativeAcquisitions.C97.value)) * -1;
CreativeAcquisitions.B101.value = parseFloat(CreativeAcquisitions.B4.value);
CreativeAcquisitions.B105.value = (parseFloat(CreativeAcquisitions.B101.value) * parseFloat(CreativeAcquisitions.B103.value)) / 100;
CreativeAcquisitions.B107.value = parseFloat(CreativeAcquisitions.C98.value);
CreativeAcquisitions.B108.value = parseFloat(CreativeAcquisitions.B101.value) - parseFloat(CreativeAcquisitions.B105.value) + parseFloat(CreativeAcquisitions.B107.value);
}
I don't know how these fields are created but following your code, this may be helpful:
//////////Newcode///////////////////////////
function milesIt(num) {
return Math.floor(num).toString().split("").reverse().map((n, i, a) =>
(i + 1) % 3 === 0 && i + 1 != a.length && "," + n || n).reverse().join("");
}
var inputs = document.querySelectorAll("[type='text']");
inputs.forEach(function(input) {
//change the values to include the thousand separator each time the user changes an input
input.onchange = function(e) {
inputs.forEach(function(inp) {
inp.value = inp.value.replace(/[,]/g, '');
});
calcule2();
inputs.forEach(function(inp) {
inp.value = milesIt(inp.value);
});
//e.target.value = milesIt(e.target.value.replace(/[.]/g, ''));
}
//change the values of the inputs at the beginning, although you could write them as HTML with the thousand separator values in the source code
input.value = milesIt(input.value);
});
////////////////////////////////////////////
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab
function showTab(n) {
// This function will display the specified tab of the form ...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
// ... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("nextBtn").style.display='none';
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
// ... and run a function that displays the correct step indicator:
fixStepIndicator(n)
}
function nextPrev(n) {
// This function will figure out which tab to display
var x = document.getElementsByClassName("tab");
// Exit the function if any field in the current tab is invalid:
if (n == 1 && !validateForm()) return false;
// Hide the current tab:
x[currentTab].style.display = "none";
// Increase or decrease the current tab by 1:
currentTab = currentTab + n;
// if you have reached the end of the form... :
if (n < (x.length - 2))
document.getElementById("nextBtn").style.display='inline';
// Otherwise, display the correct tab:
showTab(currentTab);
}
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false:
valid = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
function fixStepIndicator(n) {
// This function removes the "active" class of all steps...
var i, x = document.getElementsByClassName("step");
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(" active", "");
}
//... and adds the "active" class to the current step:
x[n].className += " active";
}
function calcule2() {
var i = 0;
for (i = 0; i <= 50; i++) {
calcule();
}
}
function calcule() {
CreativeAcquisitions.B28.value = parseFloat(CreativeAcquisitions.B17.value) + parseFloat(CreativeAcquisitions.B19.value) + parseFloat(CreativeAcquisitions.B21.value) + parseFloat(CreativeAcquisitions.B23.value) + parseFloat(CreativeAcquisitions.B25.value);
CreativeAcquisitions.B52.value = (parseFloat(CreativeAcquisitions.B4.value) * parseFloat(CreativeAcquisitions.B6.value)) + parseFloat(CreativeAcquisitions.D81.value) +
parseFloat(CreativeAcquisitions.B300.value);
CreativeAcquisitions.B55.value = parseFloat(CreativeAcquisitions.B4.value) * parseFloat(CreativeAcquisitions.B6.value);
CreativeAcquisitions.B56.value = parseFloat(CreativeAcquisitions.B28.value);
CreativeAcquisitions.B200.value = Math.round(parseFloat(CreativeAcquisitions.B52.value)); //here is your field
CreativeAcquisitions.B66.value = parseFloat(CreativeAcquisitions.B200.value);
CreativeAcquisitions.B67.value = parseFloat(CreativeAcquisitions.D81.value);
CreativeAcquisitions.B68.value = parseFloat(CreativeAcquisitions.B36.value) + parseFloat(CreativeAcquisitions.B37.value) + parseFloat(CreativeAcquisitions.B38.value) + parseFloat(CreativeAcquisitions.B39.value) + parseFloat(CreativeAcquisitions.B40.value);
CreativeAcquisitions.B300.value = (parseFloat(CreativeAcquisitions.B36.value) + parseFloat(CreativeAcquisitions.B37.value) + parseFloat(CreativeAcquisitions.B38.value) + parseFloat(CreativeAcquisitions.B39.value) + parseFloat(CreativeAcquisitions.B40.value)) * -1;
CreativeAcquisitions.B69.value = parseFloat(CreativeAcquisitions.B41.value);
CreativeAcquisitions.B70.value = (parseFloat(CreativeAcquisitions.B200.value) * parseFloat(CreativeAcquisitions.B46.value)) / 100;
CreativeAcquisitions.B71.value = (parseFloat(CreativeAcquisitions.B200.value) * parseFloat(CreativeAcquisitions.B47.value)) / 100;
CreativeAcquisitions.B72.value = parseFloat(CreativeAcquisitions.B66.value) - parseFloat(CreativeAcquisitions.B67.value) - parseFloat(CreativeAcquisitions.B70.value) - parseFloat(CreativeAcquisitions.B69.value) - parseFloat(CreativeAcquisitions.B71.value);
CreativeAcquisitions.B76.value = parseFloat(CreativeAcquisitions.B17.value);
CreativeAcquisitions.D76.value = (parseFloat(CreativeAcquisitions.B76.value) * parseFloat(CreativeAcquisitions.C76.value)) / 100;
CreativeAcquisitions.B77.value = parseFloat(CreativeAcquisitions.B19.value);
CreativeAcquisitions.D77.value = (parseFloat(CreativeAcquisitions.B77.value) * parseFloat(CreativeAcquisitions.C77.value)) / 100;
CreativeAcquisitions.B78.value = parseFloat(CreativeAcquisitions.B21.value);
CreativeAcquisitions.D78.value = (parseFloat(CreativeAcquisitions.B78.value) * parseFloat(CreativeAcquisitions.C78.value)) / 100;
CreativeAcquisitions.B79.value = parseFloat(CreativeAcquisitions.B23.value);
CreativeAcquisitions.D79.value = (parseFloat(CreativeAcquisitions.B79.value) * parseFloat(CreativeAcquisitions.C79.value)) / 100;
CreativeAcquisitions.B80.value = parseFloat(CreativeAcquisitions.B25.value);
CreativeAcquisitions.D80.value = (parseFloat(CreativeAcquisitions.B80.value) * parseFloat(CreativeAcquisitions.C80.value)) / 100;
CreativeAcquisitions.B81.value = parseFloat(CreativeAcquisitions.B76.value) + parseFloat(CreativeAcquisitions.B77.value) + parseFloat(CreativeAcquisitions.B78.value) + parseFloat(CreativeAcquisitions.B79.value) + parseFloat(CreativeAcquisitions.B80.value);
CreativeAcquisitions.D81.value = parseFloat(CreativeAcquisitions.D76.value) + parseFloat(CreativeAcquisitions.D77.value) + parseFloat(CreativeAcquisitions.D78.value) + parseFloat(CreativeAcquisitions.D79.value) + parseFloat(CreativeAcquisitions.D80.value);
CreativeAcquisitions.B91.value = parseFloat(CreativeAcquisitions.B36.value);
CreativeAcquisitions.B92.value = parseFloat(CreativeAcquisitions.B37.value);
CreativeAcquisitions.B93.value = parseFloat(CreativeAcquisitions.B38.value);
CreativeAcquisitions.B94.value = parseFloat(CreativeAcquisitions.B39.value);
CreativeAcquisitions.B95.value = parseFloat(CreativeAcquisitions.B40.value);
CreativeAcquisitions.B96.value = (parseFloat(CreativeAcquisitions.B52.value) * parseFloat(CreativeAcquisitions.B46.value)) / 100;
CreativeAcquisitions.B97.value = parseFloat(CreativeAcquisitions.B41.value);
CreativeAcquisitions.B98.value = (parseFloat(CreativeAcquisitions.B91.value) + parseFloat(CreativeAcquisitions.B92.value) + parseFloat(CreativeAcquisitions.B93.value) + parseFloat(CreativeAcquisitions.B94.value) + parseFloat(CreativeAcquisitions.B95.value) + parseFloat(CreativeAcquisitions.B96.value) + parseFloat(CreativeAcquisitions.B97.value)) * -1;
CreativeAcquisitions.C98.value = (parseFloat(CreativeAcquisitions.C91.value) + parseFloat(CreativeAcquisitions.C92.value) + parseFloat(CreativeAcquisitions.C93.value) + parseFloat(CreativeAcquisitions.C94.value) + parseFloat(CreativeAcquisitions.C95.value) + parseFloat(CreativeAcquisitions.C96.value) + parseFloat(CreativeAcquisitions.C97.value)) * -1;
CreativeAcquisitions.B101.value = parseFloat(CreativeAcquisitions.B4.value);
CreativeAcquisitions.B105.value = (parseFloat(CreativeAcquisitions.B101.value) * parseFloat(CreativeAcquisitions.B103.value)) / 100;
CreativeAcquisitions.B107.value = parseFloat(CreativeAcquisitions.C98.value);
CreativeAcquisitions.B108.value = parseFloat(CreativeAcquisitions.B101.value) - parseFloat(CreativeAcquisitions.B105.value) + parseFloat(CreativeAcquisitions.B107.value);
}

Javascript does not execute correctly

Good evening,
I am using Blockly to learn to programm.
In the exercise part of the code is not executed correctly. If "maandloon" is above > 2000, then there is a reduction of 25% on 'Kindergeld'. However if the result after the reduction is below 25 euro per child, then there is no reduction.
The problem is that calculation keeps using the 25% when maandloon >2000, even if the result afer reduction is below 25 euro per child.
This is my code:
var aantalKinderen, maandloon, kindergeld, kindergeldBasis, toeslag3ekind, toeslag5ekind, i;
do {
aantalKinderen=parseInt((parseFloat((output = window.prompt('Hoeveel kinderen?')) ? output : "")));
var blocktype_number = true;
if (output == null) {
window.alert("No empty input allowed");
break;
}
} while(isNaN(aantalKinderen));
do {
maandloon=(parseFloat((output = window.prompt('Wat is het maandloon?')) ? output : ""));
var blocktype_number = true;
if (output == null) {
window.alert("No empty input allowed");
break;
}
} while(isNaN(maandloon));
kindergeldBasis = 25;
toeslag3ekind = 12.5;
toeslag5ekind = 7.5;
kindergeld = kindergeldBasis * aantalKinderen;
if (aantalKinderen > 2) {
kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind;
}
if (aantalKinderen > 4) {
kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind + (aantalKinderen - 4) * toeslag5ekind;
}
if (maandloon <= 500) {
kindergeld = kindergeld * 1.25;
}
if (maandloon > 2000) {
kindergeld = kindergeld * 0.75;
} else {
if ((kindergeld * 0.75) / aantalKinderen < 25) {
kindergeld = kindergeld;
}
}
window.alert(String('Het kindergeld bedraagt ') + String(kindergeld)+'\n');
Can someone help me?
Thank you.
From what it sounds like you want to move your extra if check into the > 2000 statement. Otherwise it won't fire. Your if else statement is linear. It doesn't hit your else after > 2000 unless the conditional hasn't been matched by any of the previous if statements and the value is less than 2000. It does not run regardless or when the value was changed by a previous if condition.
var aantalKinderen, maandloon, kindergeld, kindergeldBasis, toeslag3ekind, toeslag5ekind, i;
do {
aantalKinderen=parseInt((parseFloat((output = window.prompt('Hoeveel kinderen?')) ? output : "")));
var blocktype_number = true;
if (output == null) {
window.alert("No empty input allowed");
break;
}
} while(isNaN(aantalKinderen));
do {
maandloon=(parseFloat((output = window.prompt('Wat is het maandloon?')) ? output : ""));
var blocktype_number = true;
if (output == null) {
window.alert("No empty input allowed");
break;
}
} while(isNaN(maandloon));
kindergeldBasis = 25;
toeslag3ekind = 12.5;
toeslag5ekind = 7.5;
kindergeld = kindergeldBasis * aantalKinderen;
if (aantalKinderen > 2) {
kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind;
}
if (aantalKinderen > 4) {
kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind + (aantalKinderen - 4) * toeslag5ekind;
}
if (maandloon <= 500) {
kindergeld = kindergeld * 1.25;
}
if (maandloon > 2000) {
kindergeld = kindergeld * 0.75;
if (kindergeld / aantalKinderen < 25) {
kindergeld = 25;
}
}
window.alert(String('Het kindergeld bedraagt ') + String(kindergeld)+'\n');

How to use Formatting for large numbers

I have a large number issue here. I'm using the nFormatter for large numbers in javascript but i have no idea how to use it with my existing code.
Here is the formatter im using.
function nFormatter(num) {
if (num >= 1000000000000) {
return (num / 1000000000000).toFixed(1).replace(/\.0$/, '') + 'Trillion';
}
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'Billion';
}
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'Million';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'Thousand';
}
return num;
}
nFormatter;
I need to add this code to my other code but i am not sure how i am going to do this.
Here is my current code.
var gameProfit = 5000;
var tinyOwned = 0;
var tinyCost = 5000;
var tinyIncome = 0;
function tinyGamePlay() {
if (gameProfit >= tinyCost) {
tinyOwned++;
gameProfit -= tinyCost;
tinyIncome = 15000 * tinyOwned;
tinyCost = 5000 * tinyOwned;
document.getElementById('tiny-owned').innerHTML = tinyOwned;
document.getElementById('tiny-income').innerHTML = "Income : $ " + tinyIncome;
document.getElementById('tiny-cost').innerHTML = "Next Cost : $ " + tinyCost;
document.getElementById('currentProfit').innerHTML = "Profit : $ " + gameProfit;
}
}
tinyGamePlay;
So all of the my variable will be more than 1000 at one point so the formatter needs to be used on all my variables.
I dont mind using a JS plugin either if anyone knows of something that could help,
Can anyone help please?
You just need to call this nFormatter function when you are printing the output, see snipped below, for bigger numbers you can use http://jsfromhell.com/classes/bignumber :
function nFormatter(num) {
if (num >= 1000000000000) {
return (num / 1000000000000).toFixed(1).replace(/\.0$/, '') + ' Trillion';
}
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + ' Billion';
}
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + ' Million';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + ' Thousand';
}
return num;
}
var gameProfit = 5100;
var tinyOwned = 0;
var tinyCost = 5000;
var tinyIncome = 0;
function tinyGamePlay() {
if (gameProfit >= tinyCost) {
tinyOwned++;
gameProfit -= tinyCost;
tinyIncome = 15000 * tinyOwned;
tinyCost = 5000 * tinyOwned;
console.log(tinyCost);
document.getElementById('tiny-owned').innerHTML = nFormatter(tinyOwned);
document.getElementById('tiny-income').innerHTML = "Income : $ " + nFormatter(tinyIncome);
document.getElementById('tiny-cost').innerHTML = "Next Cost : $ " + nFormatter(tinyCost);
document.getElementById('currentProfit').innerHTML = "Profit : $ " + nFormatter(gameProfit);
}
}
tinyGamePlay();
<p id="tiny-owned"></p>
<p id="tiny-income"></p>
<p id="tiny-cost"></p>
<p id="currentProfit"></p>

Complex regular expression with validation rules and so on

Maybe I got killed here but I need to ask if this insane is possible through RegEx, see I have this Javascript/jQyery function for validates VAT number in Venezuela (RIF):
function(value) {
if (value === '') {
return true;
}
var $rif = value.val();
var $last_number = $rif.substr($rif.length - 1);
var $number = $rif.substr(0, 8);
$valid = /^([VEJPG]{1})([0-9]{9}$)/.test($rif_type + $rif);
if ($valid) {
if ($rif_type == "V") {
$sum = 1 * 4;
} else if ($rif_type == "E") {
$sum = 2 * 4;
} else if ($rif_type == "J") {
$sum = 3 * 4;
} else if ($rif_type == "P") {
$sum = 4 * 4;
} else if ($rif_type == "G") {
$sum = 5 * 4;
}
$n0 = $number.charAt(0) * 3;
$n1 = $number.charAt(1) * 2;
$n2 = $number.charAt(2) * 7;
$n3 = $number.charAt(3) * 6;
$n4 = $number.charAt(4) * 5;
$n5 = $number.charAt(5) * 4;
$n6 = $number.charAt(6) * 3;
$n7 = $number.charAt(7) * 2;
$sum += $n0 + $n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7;
$mod = $sum % 11;
$last_val = 11 - $mod;
if ($last_val == 11 || $last_val == 10) {
$last_val = 0;
}
if ($last_number == $last_val) {
return true;
} else {
return false;
}
}
return false;
}
I'm asking if it's possible to write a RegEx to check the same and if so, how?
These are valid VAT numbers:
J298081775
J295809000
J298720620

Categories

Resources