1.hey guys,how do i separate these?
2.sorry im newb in this,but been cracking my head for a while,how to make this code write sentences separetly without writing them non stop?
<html>
<body>
<script type="text/javascript">
var x=[15,22,28,30,25,11,12,29,27,26];//right numbers
var c=1;
var e=0;
var i=0;
for (c=1;c<=10;c++) {
var y=Number(prompt("enter number from 10 to 30",0));
for(i=0;i<=9;i++) {
if(y==x[i]) {//checking every number in array
document.write("u right.<br>");
e=e+1;
}
else {
document.write("u wrong.<br>");//this writes every time it goes trough the loop,i tried breaking,but it just quits the loop on first number in array,i tried continue,no luck
}
}
}
if(e<5) {//amount of time you guessed right
document.write("u lose ");
}
else {
document.write("u win");
}
</script>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var x=[15,22,28,30,25,11,12,29,27,26];//right numbers
var c=1;
var e=0;
var i=0;
for (c=1;c<=10;c++)
{
var y=Number(prompt("enter number from 10 to 30",0));
var right = false;
for(i=0;i<=9;i++)
{
if(y==x[i]) //checking every number in array
{
right = true;
}
}
if (right)
{
e++;
document.write("u right.<br>");
}
else {
document.write("u wrong.<br>");
}
}
if(e<5)//amount of time you guessed right
{
document.write("u lose ");
}
else
{document.write("u win");}
</script>
</body>
</html>
Your code can be optimized in many areas:
<html>
<body>
<script type="text/javascript">
var x=[15,22,28,30,25,11,12,29,27,26];//right numbers
var c=1;
var e=0;
var c=0;
while (c < 10)
{
c++;
var y=Number(prompt("enter number from 10 to 30",0));
if (x.indexOf(y) != -1)
{
e++;
document.write("u right.<br>");
}
else {
document.write("u wrong.<br>");
}
}
if(e<5)//amount of time you guessed right
{
document.write("u lose ");
}
else
{
document.write("u win");
}
</script>
</body>
</html>
Use indexOf method of array for checking whether value is available in array or not.
If value is not available then return -1 else return position of that value.
<html>
<body>
<script type="text/javascript">
var x = [15,22,28,30,25,11,12,29,27,26];
var e = 0;
for (var c=1;c<=10;c++) {
var y = Number(prompt("enter number from 10 to 30",0));
x.indexOf(y) > 0?(document.write("u right.<br>"),e++):document.write("u r wrong.<br>");
}
e<5?document.write("u lose "):document.write("u win");
</script>
</body>
</html>
Related
This is my typing game code. Feel free to test it if needed
<!DOCTYPE html>
<html onkeypress="myFunction(event)">
</head>
<body>
<style>
p::first-letter {
background-color: blue;
color: white;
}
</style>
<p id="demo"></p>
<script scr="script.js">
//base value for correct letters typed
var z = 0
//chooses random sentence (changable)
var myArray = [
"Repl.it is the best website in the world",
"Nothing can bet Repl.it in code editors",
"I hope the developers will give me hacker plan for complimenting Repl.it",
"Post your sentence suggestions below because I am horrible at creating sentences",
"I have been on Repl.it for 2 months",
];
var str = myArray[Math.floor(Math.random()*myArray.length)];
//replaces ('demo') with random sentence
document.getElementById("demo").innerHTML = str;
var a = str.length
var n = str.charAt(z)
console.log(a)
var x = null
var y = null
function myFunction(event) {
//find key pressed
x = event.keyCode;
y = String.fromCharCode(x);
myFunction2() //see below
}
function myFunction2() {
if (y == n) { //check if correct key
z = z + 1;
n = str.charAt(z)
console.log(n)
test()
document.getElementById("demo").style.color = "black";
} else { //if not correct key change to red
document.getElementById("demo").style.color = "red";
}
}
function test() {
if (z == a) {
document.write("END")
} else {
document.getElementById("demo").innerHTML = str.slice(z)
}
}
</script>
</body>
</html>
The str.slice is slicing off the spaces.
Is there any way to not slice the space?
or is there any other function which does it?
Which still leaves a space? (I couldn't find one online)
Thanks in advance,
Matthew
I am a bit new to JavaScript, and just want to search my website for a particular string of text and find it, list it or whatever.
I looked at this solution:
Javascript Search Engine (Search own site)
But am not getting what I want out of it. I'm sure my syntax from HTML to JavaScript is a bit off. I am happy to search the current page for now, but will ultimately want to span across all HTML pages, which are all in a single directory.
Here is my code, and it does not error, but it also does not appear to do anything except clear the textbox.
I have gone through a plethora of examples using W3Schools and some other textbooks on Safari
<!DOCTYPE HTML>
<head>
<style>
</style>
</head>
<body>
<br>
<input type="number" id="str">
<button onclick="findInPage(str.value)">Search</button>
<script language=JavaScript>
var NS4 = (document.layers);
var IE4 = (document.all);
var win = window;
var n = 0;
function findInPage(str) {
var txt, i, found;
if (str == "")
return false;
if (NS4) {
if (!win.find(str))
while(win.find(str, false, true))
n++;
else
n++;
if (n == 0)
alert("Not found.");
}
if (IE4) {
txt = win.document.body.createTextRange();
for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
if (found) {
txt.moveStart("character", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
}
else {
if (n > 0) {
n = 0;
findInPage(str);
}
else
alert("Sorry, we couldn't find. Try again");
}
}
return false;
}
</script>
</body>
</HTML>
<html>
<script>
var tal;
var array = [];
var element=parseIFloat();
function bubbleSort(A){
var swapped,
len = A.length;
if(len === 1) return;
do {
swapped = false;
for(var i=1;i<len;i++) {
if(A[i-1] > A[i]) {
var b = A[i];
A[i] = A[i-1];
A[i-1] = b;
swapped = true;
}
}
}
while(swapped)
}
function insertnumber(){
var element=document.getElementById("element").value;
insert (element,array);
}
function insert(element, array) {
array.push(element);
alert(array);
bubbleSort(array);
alert(array);
}
</script>
<body>
<input type="button" value="Mata in" onclick="insertnumber()" id="resultat">
tal<input type="number" id="element" autofocus>
</body>
</html>
This my code but i really dont know how to get it working again, my problem is that i cant get it to read numbers correctly, trying to use "var element=parseIFloat(); " but that doesnt seem to work..
Thanks :)
Sure, var element=parseIFloat();
was meant to be var element=parseFloat();
and put between
var element=document.getElementById("element").value;
and
insert (element,array);
Would anyone clerify why my program is outputting undefined or NAN? I know for sure that my random number generator is working. Also, I'm trying to sum up all of the "score" value when the number generator has generated the value 10 times. Thanks for the help
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function main()
{
randomnumber()
totalscore()
}
function randomnumber()
{
var randomnumber;
randomnumber = Math.random()*3;
return(Math.floor(randomnumber+0.5));
}
function totalscore()
{
var n;
var score;
var number;
number = randomnumber()
for (n=0;n<11; n=n+1)
{
if (number==0)
{
score =score+0
}
if (number==2)
{
score =score+2
}
if (number==3)
{
score =score+3
}
document.write(score)
}
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
<INPUT NAME = "dobutton" TYPE = "button" value = "Start game" on Click = "game()">
<INPUT NAME = "dobutton" TYPE = "button" value = "Leaderboard" on Click = "leader()">
</BODY>
</HTML>
Because score variable is undefined. You should initialize it with a number for instance: var score = 0;
Variable score is declared in your totalscore function but never initialised. Adding anything to undefined gives NaN, which is what your function writes to the page.
You need to initialize your score variable with 0. Until you don't, it's value is undefined and when you do math operations on an undefined object, you wil get a NaN error. I have also formatted your code a bit.
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function main()
{
randomnumber()
totalscore()
}
function randomnumber()
{
var randomnumber;
randomnumber = Math.random()*3;
return(Math.floor(randomnumber+0.5));
}
function totalscore()
{
var n;
var score = 0;
var number = randomnumber();
for (n = 0 ; n < 11 ; ++n)
{
if (number == 0){
score += 0;
}
else if (number == 2)
{
score += 2;
}
else if (number == 3)
{
score += 3;
}
document.write(score)
}
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
<INPUT NAME = "dobutton" TYPE = "button" value = "Start game" on Click = "game()">
<INPUT NAME = "dobutton" TYPE = "button" value = "Leaderboard" on Click = "leader()">
</BODY>
</HTML>
EDIT: If I understand your comment correctly, you should be doing something like this
function totalscore()
{
var n;
var score = 0;
for (n = 0 ; n < 10 ; ++n)
{
score += randomnumber();
document.write(score)
}
var grandTotal = score;
}
Hi, I am wondering how I would be able to do this code. I have an idea on how to make the javascript code, but I'm not sure if my code is correct. The question I have been given is:
Design and develop a javascript program that will determine if input
number is positive and negative.Consider zero(0) as
positive(considering that it contains no negative sign).
here is my javascript code, I'm not sure if this is correct based on the question above.
<!DOCTYPE html>
<html>
<head>
<title>activity 1</title>
</head>
<body>
<script type="text/javascript">
var stringNumber = prompt("Enter Number: ", "");
stringNumber = parseInt(stringNumber);
if(stringNumber >=80){
document.write("Positive");
}else{
document.write("Negative");
}
</script>
</body>
</html>
any answers are greatly appreciated.
Just check if the number is greater than or equal to 0
if(stringNumber >= 0)
{
document.write("Positive");
}
else
{
document.write("Negative");
}
var theNumber = prompt("Enter a Number:");
if (theNumber < 0) {
document.write("Negative");
}
else {
document.write("Positive");
}
Why you comparing number with 80? It should be 0
<script type="text/javascript">
var stringNumber = prompt("Enter Number: ", "");
stringNumber = parseInt(stringNumber);
if(stringNumber >=0){
document.write("Positive");
}else{
document.write("Negative");
}
</script>
And it work fine
Positive, negative & NaN:
var stringNumber = prompt("Enter Number: ");
stringNumber = parseInt(stringNumber, 10);
if (isNaN(stringNumber)) {
document.write("Not a Number");
} else if(stringNumber >=0) {
document.write("Positive");
} else {
document.write("Negative");
}