I need help with JavaScript

code line #13, alert function contains only a quoted string.

Instead should be:

alert((bread*2) + (cheese*4) )

Carlos Z

Not sure what the pdf solution looks like but you can alert a message with one or more variables like this.

alert("Your order total is £" + sum_total);

where you have calculated sum_total earlier in the script, obviously.

Screenshot 2020-11-01 at 19.50.35

If you want to make provide more information like line totals for each item you can do something like this.

alert("Your order total is £" + sum_total + "\n" + "Your items are \n" 
+ bread_count + " loaves of bread at £" + bread_cost + " each. Total £" + bread_total + ".\n" 
+ cheese_count + " pieces of cheese at £" + cheese_cost + " each. Total £" + cheese_total + ".");

which looks like this and where you have created the necessary variables to hold the cost of each item, how many were ordered and the total cost of each item.

Screenshot 2020-11-01 at 19.54.45

It can be easier to read and write the javascript if you use string interpolation.

String interpolation

alert(`Your order total is £${sum_total}`);

No need for the + and everything is enclosed between two backticks `

The more complicated example becomes

alert(`Your order total is £${sum_total}\n Your items are
${bread_count} loaves of bread at £${bread_cost} each. Total £${bread_total}.
${cheese_count} pieces of cheese at £${cheese_cost} each. Total £${cheese_total}.`);

\n creates a new line

Screenshot 2020-11-01 at 20.08.03

Hope that helps

1 Like

code and browser on left is mine, code and browser on right is IoT. I can’t figure out why it won’t loop.

1 Like

@monstrosity

Hi, I think it loops, but you need to print the toPrint variable. like this document.write(toPrint);

Happy learning, :slight_smile:
Abel S

2 Likes

/*Hey helper,
I want to create an HTML

element and set the text inside with “Hello”. It works as the following:
*/

i also got a problem i’m now doing a little excercise from an article from the academy. there you have a function called object.keys. when i try to run that code it says that the object isn’t defined but it is and i can’t wrap my head around it what the problem is if anyone could give some clafication about this that would really help.

use Gyazo to make screenshots very easy

Hi @jelle_van_der_meer,

You need to capitalize the “O” on the “Object” keyword. When you have a lowercase, “object”, it thinks that you have declared variable and it is defined somewhere in your code. Whereas the capital “Object” refers to the predefined constructor in the Javascript language.

Hope this clears out the confusion.

1 Like

yes this was it thanks a lot ! i now see what objects.keys does and understand it thank you.

Hi Mauro, I am stuck on one thing. I am getting different results using alert vs. document.write. Here is a simple program that has an input and a button to write the input. If I use alert the input box and button remain, but if I use document.write they disappear. Trying to figure out why this is happening. I want the input box and button to remain using document.write.

Program 1 using alert:

<title>Jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Print Text

Program 2 using document.write:

<title>Jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Print Text

Thank you.

I figured it out the c in column, one of the column i spelt witha capital Column.
image

ended up having another problem it won’t break to the next row after each print. I thought maybe it is because i am using document.write instead of console.log but for some reason i can’t use consule.log I don’t have it as an option.

image

this is what i am seeing…
image

Hi All - i’m taking the Javascript Programming for Blockchain Developers (so the basic one) and i have a question.

The homework question was:

  1. The difference between two integers.

Create a script that requests and reads in two integers.

The script will then calculate the difference between the two numbers and print the answer to the console.

I wrote the following:

var x = 25;
undefined
var y = 19;
undefined
console.log (x%y)
6

…and ivans solution was:

var​ x = prompt(​"Enter the value of x"​); console​.log(x);
var​ y = prompt(​"Enter the value of y"​); console​.log(y);

console​.log(​"The difference between x and y is"​, (x-y));

… we both get the same answer so i’d like to know what the difference is… also i completely do not understand the reason for adding in: prompt

any help would be appreciated - thanks all.

1 Like

Hi @JDW, The above statement is the operator for getting the remainder between two numbers after you divided it. It does not give the difference between two numbers. Please try with any other two numbers and you will notice it. It was just happy chance that Ivan’s answer and your answer coincided.

Well, the prompt is used so that the user can enter values in real time instead of having a value coded into the script. Nothing fancy there. :smiley:

Hope this helps. :slight_smile:

1 Like

Thanks @Malik - i’ve been battling through the course which is twisting my melon but i think i’m getting there !

1 Like

Tried doing the exercise in Booleans, and, if, else

Dont know what i did wrong, still very new & getting frustrated.

Looked in solutions PDF that was posted, fixed what i thought was wrong and still got syntax error (still dont know what that specifies or if thats just java telling me i f’ed up)

So I copy & pasted ivans script & got the same result. So now im very confused as neither of our scripts work which make it very difficult to figure out where i went wrong.

Any help is appreciated


  var passanger = 0;
  var station1 = parseInt(prompt("How many people will get on the bus?"));
  if (passanger + station1 >= 30){
  console.log("The bus is full", passanger + station1 - 30 , "will have to walk");
  passanger = 30;
  }
  else{
  passanger += station1;
  console.log(station1 + "passangers from station 1 have gotten onto the bus.");
  }

  var station2 = parseInt(prompt("How many people will get on the bus?"));
  if (passanger + station2 >= 30);{
  console.log("the bus is full", passanger + station2 - 30 , "will have to walk");
  passanger = 30;
  }
  else{
  passanger += station2;
  console.log(station2 + "People have gotten on the bus.");
  }

  var station3 = parseInt(prompt("How many people will get on the bus?"));
  if (passanger + station3 >= 30){
  console.log("The bus is full", passanger + station1 + station2 + station3 - 30 , "will have to walk");
  }

  passanger = 30;
  else{
  passanger += station3;
  console.log(station3 + "Are now on the bus, next stop the fourth Station!");
  } 
  console.log("The bus has arrived with" +passanger + "people on board");

Hi @Avrg,

I have replied to your similar request on the forum. It is solved over there.

Thanks.

Hello everyone,

On the Javascript course, Javascript programming, practice exercice - loop.
On the last question the answer is this but actually when you run the program, when you put your number again it doesn’t count again from 0 to your number. Or did I understood the question wrong?

var inp = prompt(“Enter a number”);
let num = parseInt(inp);
counter = 0;
while (counter < num) {
console.log(counter);
counter++;
}
var num = 0;
while (num != 10) {
let inp = prompt(“Enter a number”);
num = parseInt(inp);
}
console.log(“You entered 10, the program will end now!”);

Yes, it doesn’t because the second prompt is coming from this condition –

This loop keeps going on and it does not go the upper while loop. YOu can try changing the logic so that the upper while loop keeps going on but when you enter the number 10, the counting stops.

Hope this helps. :smiley:

Hi everyone,

I’m taking JavaScript programming course and doing the first exercises about console.log & variables and was so confusing to me some questions because as someone mentioned before, the course didn’t covered it.

Then, Malik says that we should search on the internet for more info. so, now I’m wondering, How we should search on the internet to obtain “prompts” as result? specifically for question 9. (A script that requests and accepts two integers)?

Sorry if I ask this but I don’t want to continue without understanding the whole thing because I think that the next exercises would be like this.

Hope you can help me!

Hi @alejandravisa,

Yes, searching for answers on the internet is itself a skill. The difference between a new programmer and an advanced programmer is the ability to search for answers for problems all autonomously. We’d like to build that skill for you through these exercises. After a few months, you’ll realise how important it was to acquire a skill like that.

Having said that, for finding Javascript inbuilt functions or keywords, you would have to look at its documentation. Suppose, in your case, you’d like to search how to use the “prompt” function in Javascript, you’d go ahead and search for “Prompt Javascript” on google. Most likely the top result will be a W3Schools link to its documentation or Developer Mozilla documentation.

Here, you can see how the function is used, what are it’s arguments and also test out the functionality.

Reading documentation and answers online is a critical job of a developer. We are here to guide you in that regard, so feel free to ask and we will help you. :smiley:

Hope this gives you a perspective.

Happy Learning!