list problem in my javascript todo list exercise [duplicate] - javascript

This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed last month.
I've checked many times but since I'm new I'm constantly missing something. In the console, my list is does not appear as like this....and my output is like here
Everything is correct, but is there something I don't know about the console?
let input = prompt("What would you like to do?");
const todos= ['list app finish', 'js practice'];
while (input !== 'quit' && input !=="go away aysel"){
if(input === 'list') {
console.log('***********')
for (let num = 0; num< todos.length; i++){
console.log("${num} ${todos[num]}");
}
console.log('*************')
}
input= prompt('What would you like to do')
}
console.log('Good bye')

Template literals work with the backtick char `.
They do not work with string quotes like ' and "
so you need to change the logging to
console.log(`${num} ${todos[num]}`);

Related

How to get split function to work properly? [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 4 months ago.
i am trying to create a function that takes a form input (in this case the name), splits it using the space, and then sends either an error message or an all good message. however, it fails to acknowledge conditions for some reason? it always says that its correct even if i only input a name.
function validateName(){
var nameVal = document.getElementById("yourname");
var nameParts = nameVal.toString().split(" ");
if (nameParts.length<2) {
document.getElementById("yourname").nextSibling.innerHTML = "Please enter your First and Last Name.";}
else {
document.getElementById("yourname").nextSibling.innerHTML = "Thank you for entering your First and Last Name!";
}
}
ive tried changing the code in various ways but there is always some error.
Looks like you forgot to add .value in the first line of your function like this document.getElementById("yourname").value.
function validateName(){
var nameVal = document.getElementById("yourname").value;
var nameParts = nameVal.toString().split(" ");
if (nameParts.length<2) {
document.getElementById("yourname").nextSibling.innerHTML = "Please enter your First and Last Name.";}
else {
document.getElementById("yourname").nextSibling.innerHTML = "Thank you for entering your First and Last Name!";
}
}

Average calculator finding strange and incorrect numbers [duplicate]

This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Closed 3 years ago.
I am trying to make an average calculator. This is my code for it:
var data = [];
var yesno = confirm("Would you like to add more data?");
while (yesno) {
var newdata = prompt("Enter a piece of data (must be a number)");
data.push(newdata);
var yesno = confirm("Would you like to add more data?");
}
var total = 0;
var i = 0;
if (!yesno) {
while (i < data.length) {
total += data[i];
i++;
}
}
var average = total / data.length;
document.write(average);
It seems to take input well, however something goes wrong when it comes to calculation. It says that the average of 6 and 6 is 33, 2 and 2 is 11, and 12 and 6 is 306. These are obviously wrong. Thank you in advance for your help.
You need to take a number, not a string value from the prompt.
The easiest was is to take an unary plus + for converting a number as string to a number
data.push(+newdata);
// ^
Your first example shows, with '6' plus '6', you get '66', instead of 12. The later divsion converts the value to a number, but you get a wrong result with it.
It is taking the input as string. Convert the input to floats before putting them in the array. I think its performing string additions like 6+6=66 and then 66/2 = 33. Similar is the case of 2 and 2.

Javascript, is there a way to write an array as a single text string without commas or gaps? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
array join() method without a separator
I'm trying to code a simple array that will then be displayed as a single, continuous line of text with no comma's gaps or separations of any kind. For example, if the array was about fruit, and the fruit involved were apples[0] and bananas[1], it would be displayed as applesbananas.
I am also using socket io and tried the array.join command, but that came up as a 'native expression' in the cmd, which I wasn't sure what to do with.
This is the code I have so far:
var A = 0
var B = 0
var master = new Array();
io.sockets.on("connection", function (socket) {
socket.on("message", function (data) {
var new_data = data.split(',');
if (new_data == 'A') {
master.push(new_data)
console.log(A);
}
else if (new_data == 'B') {
master.push(new_data)
console.log(B);
}
var final = (master.join);
console.log(final);
socket.emit("message", 'master,' + final);
socket.broadcast.emit("message", 'master,' + final);
Right now, this .join expression is being displayed as a native expression in the cmd. Is there any way to join the array elements in a way the cmd or socket io will understand?
Thanks for the help!
You're seeing that error because you're missing parenthesis after your call to .join.
You can join an array with no spaces using .join('').
Try this:
var final = master.join("");

Simple RegExp question - how to find keys [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Get query string values in JavaScript
Use the get paramater of the url in javascript
I have a long list of URLs where in one part of the URL I've got a command such as 'KEY=123'. I would like to find all those keys.
For Example: /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1
How could this be accomplished? My idea was just to search all the 'KEY' words and look for the number next to it - but I guess there is something much quicker for this.
The language of preference would be Javascript.
EDIT:
The URLs are cluttered and can't be extrapolated out of the text easily. a small example of the text:
2011-07-29 01:17:55.965/somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 685ms 157cpu_ms 87api_cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13`
2011-07-29 01:05:19.566 /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 29ms 23cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13
2011-07-29 01:04:41.231 /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 972ms 78cpu_ms 8api_cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13
The Javascript you'd need would be something like -
var text = 'ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1&key=678';
var matches = text.match(/KEY=\d*|key=\d*/g);
for (i=0; i<matches.length; i++) {
alert(matches[i]);
}
If you wanted just the number, you could do something like -
var text = 'ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1&key=678';
var matches = text.match(/KEY=\d*|key=\d*/g);
for (i=0; i<matches.length; i++) {
alert(matches[i].toLowerCase().replace('key=',''));
}
If you are interested only in the KEY value:
var regex = new RegExp("KEY=(\d+)");
var result = regex.exec(window.location.href);
result would be "123" in your case. If you have multiple lines, then:
var regex = new RegExp("KEY=(\d+)", "gm");
var results = regex.exec(window.location.href);
in this case results is an array.
a = "/somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1";
a.match(/KEY\=(\d+)/gi)

Javascript replace function won't remove the string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I have no idea why this doesn't work, but doing some validation functions and trying to output a dynamic message to an alert when they hit submit and forgot to fill out a field. It works only on the second click of the submit button, as far as removing the string, everything else works when it should.
Here's the code:
var fname = $('#fname');
var lname = $('#lname');
function validatefname(){
var a = fname.val().length;
if(a < 2) {
fname.prev().addClass("error");
if(msg.search("First Name") == -1) {
msg+= "-Please enter your First Name\n";
}
return false;
} else {
fname.prev().removeClass("error");
msg.replace(/Please enter your First Name\n/g, "");
return true;
}
}
fname.blur(validatefname);
fname.keyup(validatefname);
step4submit.click(function(){
if(validatefname()) {
step4form.submit();
return true
} else {
msg+= "\nPlease fill out the fields marked in red";
alert(msg);
msg = "";
return false;
}
});
String.replace returns a new string, rather than editing the string that makes the replace call.
You need
msg = msg.replace( blah )
In JavaScript, Strings are immutable, they can never change. So if you are calling a function to modify a string in some way, that function will return a new string with your changes, rather than modify the original string.
Change the line that is performing the replacement to save the result of the replacement back into the original variable, like so:
msg = msg.replace(/Please enter your First Name\n/g, "");
(there are various performance and safety reasons for this, but that is for another day/another question).
Try changing
msg.replace(/Please enter your First Name\n/g, "");
to
msg = msg.replace(/Please enter your First Name\n/g, "");

Categories

Resources