Update page after input? - javascript

I'm making a small web program that takes in words and tells you them back alphabetically or if numbers, numerically. I believe everything is working except showing the words after gathering the input. I try to write the words to the page but they don't show up because I believe you have to update the page somehow which I have no idea on how to do so.
My JavaScript code:
var input = null;
var words = new Array();
alert("Enter one word at a time in the next prompts. Enter passw0rd to finish/stop.");
do {
input = prompt("Enter word...enter passw0rd to exit.");
if ( input != "passw0rd" ){
words.push(input);
}
else{
break;
}
}while( input != "passw0rd" );
words.sort();
alert("Your words alphabetically are...");
for ( var i = 0; i < newList.length; i++ )
{
document.writeln(words[i]);
}

It looks like your for loop is using newList, when it should be using words.

Related

Do While Loop Mixed With Function Loop

I am writing a simple program for school and running into trouble with an issue I am hoping someone could help me with
These are the assignment parameters:
Create a small JavaScript program that:
Creates a variable 'total' with a value of 0.
Use a do-while loop & function to prompt the user to enter a series of numbers, or the word "quit" - the quit command should be case insensitive.
If the user enters a number, add the new number to a running total.
If the user enters the word "quit" the loop should stop execution.
If the user enters a word other than quit the prompt message should change to let the user know they entered an invalid data type
When the loop is exited, display a message giving the total of the numbers entered
My code achieves all assignment parameters except I can't figure out how to get the prompt to disappear after the quit command is entered. The result still displays on the screen but the prompt keeps looping.
Here is my code:
var inputCounter = 0;
var total = 0;
newInput = null;
quit = /quit/i
function askForNum(a) {
do {
var newInput = prompt(a);
if (!newInput.match(quit)) {
if (newInput < "A" && newInput > "" ) {
var addThis = parseFloat(newInput);
}
if (isNaN(newInput)) {
askForNum("That wasn't a number! type \"quit\" to see the results!");
} else {
total += addThis;
inputCounter++;
askForNum("Every number you enter gets added together! type \"quit\" to see the results!");
}
}
} while (!newInput.match(quit)) {
document.getElementById("addition-script").innerHTML = "<b>TOTAL:</b> " + total + "<br><b># OF ENTRIES:</b> " + inputCounter;
return;
}
}
if (total == 0){
askForNum("initial: Every number you enter gets added together! type \"quit\" to see the results!");
}
You are calling the askForNum function from inside itself (recursion), in effect starting a new do-while loop inside the previous one every time you type anything other than "quit".

Need to get indexes from array and output them to user in JavaScript

We're displaying five input fields to user. He can type some information in them. After that, we need to find out if his input is correct. For that purpose we use an array of possible correct values.
Like:
var input = document.getElementById("input").value;
input = input.toLowerCase();
inputPos = possibleInputs.indexOf(input);
inputPosArray.push(inputPos);
The code for analysis looks like that for now:
function arrayLookup() {
var inputCorrect = true;
inputPosArray.forEach(function(item, i, inputPosArray) {
if (inputPosArray[i] == -1) {
wrongInput = cardRPos.indexOf(cardRPos[i]) + 1;
wrongInputsArray.push(wrongInput);
inputCorrect = false;
} else {
null;
}
});
if (inputCorrect == false) {
alert("Wrong input! Check field " + wrongInputsArray);
} else {
nextStep();
}}
For now it correctly finds out if input is wrong and alerts user.
The problem is in "wrongInputsArray" - it doesn't display output correctly. E.g. if user has typed wrong information in 2nd field, it will print out "2".
But if he has made mistakes in 2nd and 5th field, he gets "Wrong input! Check field 2,2" alert.
Please show me what am I doing wrong.
Kindly yours,
Richard
You are using this code to insert the wrong asnwers:
wrongInput = cardRPos.indexOf(cardRPos[i]) + 1;
If two questions has the same answer, indexOf will return always the first match. Try just using this:
wrongInput = i + 1;

JQUERY if string contains two words

I can't seem to find the answer out there for this one.
I have users filling out a form to enter their name for a ticket. This is done with one field for the whole name. Now it's important I check for both second and first names.
Everything is working perfectly. But for the life of me I can't seem to work out a new Elseif statement to check that the var contains TWO words.
Any tips?
$(document).ready(function(){
$('#user_details_form_button').live('click', function(){
var check_name_b = '';
var check_email_b = '';
var check_phone_b = '';
proceed = true;
window.location.hash = 'tickethash';
$('.userinput_name_b').each(function(){
check_name_b = $(this).val();
if(check_name_b == ''){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
proceed = false;
}else{
if(check_name_b.replace(/ /g,'').length < 5){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
$(this).val("");
proceed = false;
}else if(check_name_b.length > 40){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
$(this).val("");
proceed = false;
}else{
$(this).css('border-color','#CCCCCC');
}
}
});
You can use this regular expression:
if (!check_name_b.match(/^[a-z\.]+ [a-z]+/i)) {
alert('Invalid name');
}
Do not use split, like others says, because if I am just typed some spaces, then that will success also.
You can use split() to do this for you...
var check_name_b = $(this).val().trim().split(" ");
if (check_name_b.length < 2) {
// no space in name, therefore it's only one word (or none!)
}
However, I'd strongly recommend using 2 fields for first name and surname. Not only is this what people will expect, being used to seeing it on nearly all other websites, it's more robust and easier to handle input. What happens if I put "Mr Archer"? That's not my full name, but it's 2 words.
Check with this:
$(this).val().split(" ").size() > 1
Everything is working perfectly. But for the life of me I can't seem to work out a new Elseif statement to check that the var contains TWO words.
Two word mean, that there is a space between them.
Try the .split() function from jQuery.
Like:
words = input.split(' ');
if(words.length >= 2) {
//do something
}

javascript: counting digits in a text input

I have a piece of HTML that creates a web form with three text fields (name, group and number), all of which are validated using JavaScript to check that there is data inputted into them. In the last text field, I need to introduce an additional bit of JavaScript to check that the data inputted by the user is also four digits long (for example 2947 or 94Q3). As a complete JavaScript novice, I'm not sure how I would do this! Would I have to create a variable that could take the value of the inputted data, then count the digits of the variable, or could I do it directly from the field? Here is the Javascript section of my code:
function validateForm() {
var result = true;
var msg = ””;
if (document.Entry.name.value == ””) {
msg += ”You must enter your name\n”;
document.Entry.name.focus();
document.getElementById(‘name’).style.color = ”red”;
result = false;
if (document.Entry.group.value == ””) {
msg += ”You must enter the group\n”;
document.Entry.group.focus();
document.getElementById(‘group’).style.color = ”red”;
result = false;
}
if (document.Entry.number.value == ””) {
msg += ”You must enter the number\n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color = ”red”;
result = false;
}
if (msg == ””) {
return result;
} {
alert(msg)
return result;
}
}
If possible, could you tell me what code I would need to insert? Thank you!
Place this block in your conditions list:
if (document.Entry.number.length!=4) {
msg+=”You must enter 4 digits \n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color=”red”;
result = false;
}
if (document.Entry.number.value==””) {
msg+=”You must enter the number \n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color=”red”;
result = false;
}
change this to
if (document.Entry.number.length != 4){
msg+="Number must be exactly 4 characters \n";
document.Entry.number.focus();
document.getElementById('number').style.color="red";
result = false;
}

Javascript array length through user input

I have a problem in Javascript I want to make a form which have one input text field and one button when I click on the button window.prompt is called.
It will prompt depend upon my array length but I want array length get through input text field when I write 10 it will prompt 10 times when I write 2 it will prompt 2 times.
How can i write this type of query?
I tried this code but its not working.
words = new Array (4);
function a() {
for ( k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] = window.prompt( "Enter word # " + k, "" ) ;
}
}
Maybe you forgot to call your function a().
Some remarks about your code:
You don't have to specify an initial array size, e.g. words = [] or words = new Array() is enough.
Also k=k+1 is usually written as k++.
A remark about asking questions:
Use punctuation to make sentences! Your whole question is one sentence.
Hopefully it's just the snippet of code but I hope you are using var somewhere to declare all those variables.
Otherwise this should do the trick, however not sure what you are trying to achieve but this sounds like a bad user experience.
Here is the jsffidle http://jsfiddle.net/R2bCz/1/
function Handler(event) {
var count = event.target.value;
var i = 0;
var words = [];
var word;
for (; i < count; i++) {
word = window.prompt("Enter word # " + i, "");
words.push(word);
}
}
$("#multi").on("change", Handler);

Categories