Newbie Trying to Build a Javascript Age Calculator - javascript

I'm a new self-learner and have recently taken on javascript. I have an assignment (from an online code camp) that I just cant seem to make pass. I feel like I understand the basics of it, but I can't write it in a functional way. Can anyone help me here?
The question is in the attached image.
My code looks a little something like:
function ageCalculator(name, yearOfBirth, currentYear) {
var age = currentYear - yearOfBirth;
return (name + "is" + age + "years old.");
console.log(ageCalculator("Miranda", 1983, 2015));
}
I would appreciate any help! Thank you!

You're calling the function ageCalculator just after the return statement. Anything just after the return statement won't be called.
Just pull that call outside.
function ageCalculator(name, yearOfBirth, currentYear) {
var age = currentYear - yearOfBirth;
return (name + " is " + age + " years old.");
}
console.log(ageCalculator("Miranda", 1983, 2015));

Call the function outside the function declaration.
function ageCalculator(name, yearOfBirth, currentYear) {
var age = currentYear - yearOfBirth;
return (name + " is " + age + " years old.");
}
console.log(ageCalculator("Miranda", 1983, 2015));

Whenever you return, the function stops immediately: it'll never get to your console.log the way it is now. Call console.log and the function itself outside your function, you don't want an infinite recursive loop.
Also make sure to add proper spacing:
function ageCalculator(name, yearOfBirth, currentYear) {
var age = currentYear - yearOfBirth;
return (name + " is " + age + " years old.");
}
console.log(ageCalculator("Miranda", 1983, 2015));

Related

My if and else statement always think it's -and [duplicate]

This question already has answers here:
IF Statement Always True
(3 answers)
Closed 4 years ago.
I have a selector on my page that has -and or -or. I'd like to change the content of a div depending on what users choose with -And or -Or.
My if and else statements aren't working right now, well it's almost working it just always add -And. It looks as if it always see's -And?
First time I'm trying to use an if and else statement and I think I made mistake.
<script>
function Andor' + count + '(selTag) {
var x = selTag.options[selTag.selectedIndex].text;
if (x = '-and'){
document.getElementById("and-or-' + count + '").innerHTML = " " + x + " ";
} else {
document.getElementById("and-or-' + count + '").innerHTML = " " + x + " (";
}
}
</script>
You use one =, which is 'assign'. You want === (or ==) for 'equals'.
You do the same as: var example = 'foo';. You set the value to a string ('-and'), which always results in true, which makes it look like it's true.
What you want is example=='foo' to check if the content of example equals 'foo'.
Suggested reading material: https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a

basic constructor object help - JS

Trying to learn Objects in JS and having a bit of trouble trying to get this to work. Its just a simple Object Constructor but seems to be the bane of my life today. Unfortunately I dont have anyone at hand to turn to that can help me out. Also if anyone knows of good tutorials with real life working examples instead of the just how functions, loops etc work then Id really appreciate it.
function car(model, doors, color, speed){
this.model = model;
this.doors = doors;
this.color = color;
this.speed = speed;
}
var powerCar = new car ("M3", "4 door", "phoenix", "220pmh");
console.log("This " + powerCar.model + "has " + powerCar.doors + "has a top speed of " powerCar.speed);
Fixed it.
The problem is here:
console.log("This " + powerCar.model + "has " + powerCar.doors + "has a top speed of " powerCar.speed);
You need to add a + sign between "speed of" and powerCar.speed.
you can also traverse all the properties by for loop like below:
for (var prop in powerCar) {
console.log(prop + ': '+powerCar[prop])
}

Why don't my numbers add correctly

Here's the code I'm running
function howOldAreYou(day,month,year) {
var age;
age = (day) + (2015-year) + (month*12);
return age
};
document.write(howOldAreYou(parseFloat(prompt("yo day"))),(parseFloat(prompt("yo month"))),(parseFloat(prompt("yo year"))));
I know I got the age formula wrong, but I should get some added number back, and instead I'll get this: If I put it "1" in the first prompt, "2" in the second, and "3" in the third, I'll get this on the document "NaN23". I feel like it's a small parenthesis problem, but I can't figure it out, and help is appreciated.
Because the howOldAreYou function is being called after the first prompt, so you're passing, for example: howOldAreYou(1, undefined, undefined)
Design choices aside, you can make what you're doing work like this:
function howOldAreYou(day,month,year) {
var age;
age = (day) + (2015-year) + (month*12);
return age
};
var day = parseFloat(prompt("yo day"));
var month = parseFloat(prompt("yo month"));
var year = parseFloat(prompt("yo year"));
document.write(howOldAreYou(day, month, year));
Or, to keep it how you originally had it, the correct format would be:
document.write(
howOldAreYou(parseFloat(prompt("yo day")),
parseFloat(prompt("yo month")),
parseFloat(prompt("yo year"))));

Unable to clear the Input Fields

I have a very basic code. However I am not able to clear the of the Year with the id (userYOB) on clicking the alert ok. The code works only for the which asks for year. Its not just the clearing of but I also want to bring the place holder back once alert is clicked ok.
Thanks for help.
[fiddle] http://jsfiddle.net/vineetgnair/j8zjjj9r/26/
var d = new Date();
var currentYear = d.getFullYear();
function test() {
var userYearOfBirth = document.getElementById("userYOB").value;
var authorisedAge = 19;
var currentAge = parseInt(currentYear - userYearOfBirth);
//console.log(currentAge);
if (currentAge < authorisedAge) {
alert("You are not authorised to visit the site");
document.getElementById("userYOB").value = " ";
} else {
alert("Welcome to the site!");
userYearOfBirth = " ";
}
}
In else part say
document.getElementById("userYOB").value= " ";
instead of
userYearOfBirth = " ";
Because userYearOfBirth is a simple string so updating it doesn't update the value of textbox.
Please add the following lines after the else part,
document.getElementById("userYOB").value= " ";
document.getElementById("userMOB").value= " ";
document.getElementById("userDOB").value= " ";
This will clear the fields and bring back the placeholder. The reason why
userYearOfBirth = " "
doesn't work is because in the following line the variable actually holds the value not the element itself,
var userYearOfBirth = document.getElementById("userYOB").value;
It would be more advisable if you, replaced the above line with,
var userYearOfBirth = document.getElementById("userYOB");
now you have the reference to the element in the variable. You can easily change the elements value by,
userYearOfBirth.value = " ";
Personally, I prefer the latter method cause its less code. I hope this helps!! :)

New to Javascript, im wondering why

My code doesnet add number like 2+2=4 , its adds it like 2+2=22.
I was wondering how I could change this so that it recognizes that my variables are numbers.
var gra=0;
var graTwo=0;
var graThree=0;
var stu = prompt("Who are you grading?");
var gra = prompt("Oh, what is " +stu+ "'s grade?")
if(gra>80) {
alert("Congrats, " +stu+ ". Have a gold star!")
}
else {
alert("Wow, thats awful " +stu+ ", try again")
};
var stuTwo = prompt("Who are you grading?")
var graTwo = prompt(" Oh, what is " +stuTwo+ " 's grade?")
if(graTwo>80) {
alert("Congrats, " +stuTwo+ ". Have a gold star!")
}
else {
alert("Wow, thats awful " +stuTwo+ ", try again")
};
var stuThree = prompt("Who are you grading?");
var graThree = prompt("Oh, what is" +graThree+ "'s grade?")
if(graThree>80) {
alert("Congrats, " +stuThree+ ". Have a gold star!");
}
else {
alert("Wow, thats awful " +stuThree+ ", try again")
};
var add = (gra+graTwo+graThree);
alert(add)
alert("The average grade of your class is ")
You aren't having trouble because of the pluses. You are having trouble because you are assigning graThree to a prompt, which is a string. Use parseInt as the other answer suggests.
Also, you are prompting prompt("Oh, what is" + graThree+ "'s grade?") when you should be using the variable stuThree.
You need to use parseInt or parseFloat.
The prompt function returns string types so your addition concatenates strings instead of adding numbers.
You should always be sure of your variable types before performing any action on them.
Javascript can be a little too nice sometimes.

Categories

Resources