I am new to javascript. I am doing a unit test on my project, and getting some error. Here is my code:
function timeConvert() {
let n = document.getElementById('yourNo').value;
var Jam = Math.floor(n / 3600);
var Menit = Math.floor(n / 60);
var Detik = Math.floor(n % 60);
document.getElementById("result")
.innerHTML = "Waktu yang tersisa: " + "<br>"
+ Jam + " Jam " + "<br>"
+ Menit + " Menit " + "<br>"
+ Detik + " Detik ";
}
console.log("normal use", timeConvert(1633790418));
And i got error like this:
C:\Users\USER\Desktop\backend_test\backend.js:3
let n = document.getElementById('yourNo').value;
^
Related
i was planning to add the value of luas_lingkaran and luas_persegi in variable luas_total,but it comes up with showing the value of luas_lingkaran and luas_persegi not adding each value
<script language="JavaScript">
function CalculateArea(){
var jari =document.form1.jari_jari.value;
var luas_lingkaran = ("<P>Luas Lingkaran " + (jari * jari * Math.PI) + "</p>");
document.write(luas_lingkaran);
var keliling_lingkaran = ("<P>Keliling lingkaran " + (2 * jari * Math.PI) + "</p>");
document.write(keliling_lingkaran);
var luas_persegi = ("<P>Luas Persegi " + (Math.pow(jari, 4)) + "</p>");
document.write(luas_persegi);
var keliling_persegi = ("<p>Keliling Persegi " + (jari * 4) + "</p>");
document.write(keliling_persegi);
var luas_total = ("<p> Luas Persegi dan Lingkaran" + (luas_persegi + luas_lingkaran) + "</p>");
document.write(luas_total);
}
</script>
Here is a fix for your code:
function CalculateArea(){
var jari =document.form1.jari_jari.value;
var luas_lingkaran = ("<p>Luas Lingkaran " + (jari * jari * Math.PI) + "</p>");
document.write(luas_lingkaran);
var keliling_lingkaran = ("<p>Keliling lingkaran " + (2 * jari * Math.PI) + "</p>");
document.write(keliling_lingkaran);
var luas_persegi = ("<p>Luas Persegi " + (Math.pow(jari, 4)) + "</p>");
document.write(luas_persegi);
var keliling_persegi = ("<p>Keliling Persegi " + (jari * 4) + "</p>");
document.write(keliling_persegi);
var luas_total = ("<p> Luas Persegi dan Lingkaran" + (Math.pow(jari, 4) + (jari * jari * Math.PI)) + "</p>");
document.write(luas_total);
}
Notice the change in luas_total. You should use the actual calculations without any strings or text values.
https://jsfiddle.net/kelliwilli/6bgrt7bL/128/
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100;
alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent "%!");
}
Seems to just be a simple syntax error, which is not what I thought you were getting at with the question or lack of...
Anyways alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent + "%!"); should fix it. You simply forgot to concat the last variable.
Look into template literals, they're super handy for throwing together strings with many variables, along with the fact I find them to be awesome syntax wise.
Template literal form:
alert(`There has been ${drawPercent}% of draws. I won ${AliPercent}%, and ${p1} won ${p1Percent}%!});
https://jsfiddle.net/kelliwilli/6bgrt7bL/224/
//percent funtion
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100);
alert("There has been " + drawPercent + "% of draws. Ali won " + AliPercent + "%, and " + p1 + " won " + p1Percent + "% of the games! Thanks for playing!!!");
}
There was some code out of sequence, calling a function before it was defined, and an "if" loop that should have been a "while".
Simple fixes. :)
This fiddle link works!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to generate random equations in JavaScriptp and then output them into an HTML tag.
This is the code:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
function getRandomNumber(results) {
var rollDie = getRandomizer( 1, 10 );
for ( var i = 0; i < 3; i++ ) {
results += rollDie() + "";
}
getRandomNumber.result = results;
}
function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) {
var num_7,
num_6,
num_5,
num_4,
num_3,
num_2,
num_1
getRandomNumber(num1).result = num_7;
getRandomNumber(num2).result = num_6;
getRandomNumber(num3).result = num_5;
getRandomNumber(num4).result = num_4;
getRandomNumber(num5).result = num_3;
getRandomNumber(num6).result = num_2;
getRandomNumber(num7).result = num_1;
var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x",
equation2 = "" + num_1 + " x " + num_2 + " = y",
equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3,
equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x",
equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7,
equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7,
equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5
var values = [
// there is an easier way to do this, too lazy
"" + equation1,
"" + equation2,
"" + equation3,
"" + equation4,
"" + equation5,
"" + equation6,
"" + equation7
]
var i = 0;
var e;
if (i > values.length) {
i = 0;
}
var randomEquation = values[i];
i++;
e = values[i];
this.output = randomEquation;
this.e = e;
}
function getEquation() {
var bl1,
bl2,
bl3,
bl4,
bl5,
bl6,
bl7,
equationOutput;
var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e;
getEquation.equation = eq;
}
function change() {
var final = getEquation().equation;
document.getElementById("generate").innerHTML = final;
}
</script>
</body>
</html>
But it dosen't work. Any help?
P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.
This code is a complete mess. I dont know where it comes from, but definitely not Javascript.
Try the following instead:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
function getRandomNumber() {
var results="";
for ( var i = 0; i < 3; i++ ) {
results += getRandomizer( 1, 10 );
}
return results;
}
function getRandomEquation() {
var num_7 = getRandomNumber(),
num_6 = getRandomNumber(),
num_5 = getRandomNumber(),
num_4 = getRandomNumber(),
num_3 = getRandomNumber(),
num_2 = getRandomNumber(),
num_1 = getRandomNumber();
var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
equation2 = num_1+" x "+num_2+" = y",
equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5
var randomEquation = [
equation1,
equation2,
equation3,
equation4,
equation5,
equation6,
equation7
]
return randomEquation.join("<br>");
}
function change() {
document.getElementById("generate").innerHTML = getRandomEquation();
}
</script>
</body>
</html>
Well, you could do this:
var type = (Math.floor(Math.random(4));
var ret = 0;
var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))];
if (type == 0) ret = n[0] + n[1]
else if (type == 1) ret = Math.abs(n[0] - n[1])
else if (type == 2) ret = n[0] * n[1];
else if (type == 3) ret = n[0] / n[1]
else ret = n[0] / 5 % n[1]
// do something with ret
It's fully expandable, just edit n and the if statements
function numberToTime(num){
var d = 0, h = 0, m = 0;
var numToMinutes = num*60;
while(numToMinutes > 59){
numToMinutes -= 60;
h++;
if(h > 23){
h-= 24;
d++;
}
m = numToMinutes;
}
if( d > 0){
return d + " days " + h + " hours " + m +" minutes ";
}else{
return h+":"+m;
}
This code was given to me by a very nice user here on Stack Overflow.
Since I am very new to programming, especially JavaScript I have no idea where to put my variable. I have a var howLong = (0,1 * amount + 0,2 * time) I want to convert it to hours and minutes with the code above, but I don't know how to tell the function it's about var howLong.
Can somebody help me out?
May be like this?
var ndays = Math.floor(sec/86400);
var nhours = Math.floor((sec%86400)/3600);
var nminutes = Math.floor(((sec%86400)%3600)/60);
var nseconds = ((sec%86400)%3600)%60;
return ndays + " days " + nhours + " hours " + nminutes + " minutes " + nseconds + " seconds";
I've written a random insult generator. It is fine, and works well to what I need. The problem is that when I run the program more than once, the insult is the same every time, unless I copy all the vars again. Here is my code:
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
var randomInsult = "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
What I'm trying to do is when I run randomInsult; again, I want a different result.
Use a function:
function generateRandomInsult() {
// ... all of your existing code ...
return randomInsult;
}
generateRandomInsult(); // this is what you repeat each time
As per my comment above, you need to use a function.
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var randomInsult = (function() {
var bp, a, a2, an;
var bp = bodyPart[Math.floor(Math.random() * 5)];
var a = adjective[Math.floor(Math.random() * 4)];
var a2 = adjectiveTwo[Math.floor(Math.random() * 5)];
var an = animal[Math.floor(Math.random() * 5)];
var insult = "Your" + " " + bp + " " + "is more" + " " + a + " " + a2 + " " + "than a" + " " + an + "'s" + " " + bp + ".";
alert(insult);
});
document.getElementById('test').addEventListener('click', randomInsult, false);
<button id="test">Click me</button>
You'll have to do something like this to select a random bodyPart, adjective, adjectiveTwo and animal at each call.
function randomInsult() {
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
return "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
}
In your piece of code you are generating the "randomInsult" string only once. It need to be generated at each call, with new random values.
So, simply embed your code in a function.
Call it this way:
randomInsult();