I need help with JavaScript

Hi @Malik!

Yes, you’re right. I’ll start doing some research by my own. Thanks for the links, I’ll search answers there too.

But I refer, to the point that, How can I search on internet for that question? I mean, If there weren’t the Ivan’s answers, how can I search and get into the right answer which is “prompts”.

Hi everyone !

I have a question. :face_with_monocle:
I’m doing the exercise 24-bus ride of Boolean’s & if/else statements.
On Ivan’s answer I saw two things:

  • there’s the value of passenger in every station. (passenger = 30). Is that necessary? Because if I don’t write it, it works fine. :+1:
  • Also, in station3, in console.log says:
    console.log("The bus is full.", passenger + station3 - 30, " have to walk.");
    but I realized that if you sum the 3 stations + passengers( already registered), the people that “have to walk” would be more.

This is my code:

var station1 = (prompt("Station1! How many people go on the bus?"));
if(passenger + station1 >= 30 ) {
console.log("The bus is full.", passenger + station1 - 30, " must walk.");
} else {
passenger += station1;
console.log(station1 + " people are on the bus.");
};

var station2 = (prompt("Station2! How many people go on the bus?"));
if(passenger + station2 >= 30 ) {
console.log("The bus is full.", passenger + station2 - 30, " must walk.");
} else {
passenger += station2;
console.log(station2 + " people are on the bus.");
};

var station3 = (prompt("Station3! How many people go on the bus?"));
if(passenger + station3 >= 30 ) {
passenger += station3;
console.log(passenger);
console.log("The bus is full.", passenger - 30, " must walk.");
} else {
passenger += station3;
console.log(station3 + " people are on the bus.");
};

Is that correct? or I’m making some mistake?

Thanks for your time.

Yes, this is necessary because every time the bus becomes full you have to make sure that you store that quantity in the variable passengers. Perhaps you saw it working without it because you checked the scenario when the bus is not full. The last console.log() which is - console.log("The bus has arrived with " + passenger + " people on board!"); will see a difference in value if you do not assign passenger = 30 after every filling the bus.

The number of walking people is contextual to that station as is not the cumulative amount of people from all the stations.

This condition is incorrect. Imagine a scenario when station1 has 35 people. You would be basically saying 35 people are on the bus. Which is not possible.

Similarly with the other stations as well. And also, by removing the passenger=30 condition, you are not updating the value of passengers on the bus for the next station. You need to update the number of passengers on the bus if it full otherwise, it won’t work.

Hope this helps. Try different scenarios for the program and check it out. You’ll learn a lot by dabbling in all the different scenarios. It’s gonna be fun. :slight_smile:

Happy Learning @alejandravisa !

1 Like

I’m on the Javascript Practice exercise - Functions & Arrays, but I don’t see any help thread on that page.

I’m working on the question:

In this exercise, you will manipulate a list of the entire English alphabet.

alphabet = ["O", "U", "P", "H", "Q", "T", "Z", "Y", "E", "S", "F", "C" , "R", "V", "J", "L", "X", "A", "M", "G", "D", "I", "B", "W", " K "," N "]

  1. Use the built-in methods in arrays to sort the list.
  2. You should then print the first character in the list, but it should be a lowercase letter. ( Hint: toLowerCase() )

I figured out the correct answer as stated by the Solution sheet:

alphabet.sort();

However, the result I get is this:

(26) [" K ", " N ", “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “L”, “M”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”]

I’m not clear on why K and N are out of order while the rest of the array is correctly sorted?

Ha! just answered my own question… spaces in front of K and N in the string…

1 Like

ohh you’re right! I didn’t saw the scenario if station 1 has 35 people. :grin:
Thanks a lot @Malik.

1 Like

Can anyone assist me please?

I am now on lesson 26 , whenever I write my code in the text editor, I open up the page in Chrome, but nothing appears, even when I follow the lessons exactly, does anybody have any ideas what I’m missing here?

image

1 Like

Already solved through PM :nerd_face:

Carlos Z

Heyy, all my text is white in Atom and nothing is working as im writing code, does anyone had this issue and know a solution?

1 Like

Hey @GustavBirgersson, hope you ok.

Have you save the file has .js or .html?? Atom will highlight the syntax once he know what programming language you are referring to.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Thank you very much! That was the problem and it solved it :slight_smile:

1 Like

Hello , how we can talk in telegram or facebook ?

here…on forum…

Below is question 24. I do not understand the solution given, can anyone guide me on this ?

  1. Bus ride.
    You should write a script that checks if there is room on a bus on a bus route with
    three stops.
    ● For each stop, the user must enter how many passengers are going on the
    bus. There is room for up to 30 passengers on the bus.
    ● If the bus is full, no one can get on.
    ● If more people want to go on, than there are remaining seats on the bus, only
    a few new passengers will get a seat.
    We assume that all passengers go to the last station, so we do not have to take into
    account that someone gets off the bus along the way.
    Example when running:
    Solution:
    Station 1! How many people go on the bus?

14
Station 2! How many people go on the bus?
13
Station 3! How many people go on the bus?
5
The bus is full . 2 must walk .


var passenger = 0;
var station1 = parseInt(prompt(“Station 1! How many people go on the bus?”));
if (passenger + station1 >= 30) {
console.log(“The buss is full”, passenger + station1 - 30, “have to
walk.”); passenger = 30;
}
else {
passenger += station1;
console.log(station1 + " passengers are going on the bus.");

  1. what is parseInt and parseFloat for ? What is the difference with just putting just the below syntax ?

var station1 = prompt(“Station 1! How many people go on the bus?”);

  1. For the below solution, why is there a need to put ( passenger=30; ) ?

{
console.log(“The buss is full”, passenger + station1 - 30, “have to
walk.”); passenger = 30;
}

  1. What does (passenger += station1 ) mean ?
    Can i just type like: console.log( passenger + “passengers are going on the bus”); ?

else {
passenger += station1;
console.log(station1 + " passengers are going on the bus.");
}

1 Like

ParseInt is used for converting strings to integers. And parseFloat is meant to convert strings or integers to decimal point numbers (for eg- 2.2 , 5.4)

It basically means passenger = passenger + station1

1 Like

Hi guys. I am doing the exercise below and thought I had it but it didn’t work, so i went to the solutions and saw this. I have not seen it before and hasn’t been in any of the videos. Could someone help pls.

image
Capture1

1 Like

Hi Malik,

What does below \n mean and do ? example a syntax below:
board += “\n”;

Thanks

The same thing.

board = board + "\n"

1 Like

The input from the prompt will save the response in a string. But we want in a format of a number. So, if you do parseInt, its basically converts string to a number

2 Likes

Hi Malik, can you help me with this? I am really stuck here. I am doing chapter 3 exercise : recursion

We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to see whether it’s divisible by two. Here’s another way to define whether a positive whole number is even or odd:
• Zero is even.
• One is odd.
• For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description. The function should accept a single parameter (a positive, whole number) and return a Boolean. Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a way to fix this?

Solution:
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}

console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → false

The problem is i dont understand the below syntax
else return isEven(n - 2);

Can you please explain to me so i can understand this?

1 Like