undefined function in hmtl/javascript - javascript

Just playing around with functions..
The live server shows undefined.
However; when I just run the javascript code-the correct greetings show.
Why is this?
What can I do to fix it?
const today = new Date();
let now = today.getHours(); //0-23
let time;
/*morning: 0300 - 1100
*morning >= 0300 && morning < 11
*afternoon 1100-1900
*afternoon >= 11 && afternoon < 19
*evening 1900 - 0300
*evening >= 19 && evening < 3
*/
if (now >= 5 && now < 11) {
time = 'morning';
} else if (now >= 11 && now < 17) {
time = 'afternoon';
} else if (now >= 17 && now < 23) {
time = 'night';
} else {
time = 'sleep';
}
//console.log(time);
function greeting() {
if (time === 'morning') {
console.log('Good Morning!');
console.log('I love you!');
console.log("You're doing GREAT!");
console.log('Nice butt!');
} else if (time ==='afternoon') {
console.log('Good afternoon!');
console.log("You're a genius!");
console.log('Just keep swimming!');
console.log('Progress not perfection!');
} else if (time === 'night') {
console.log('Goodnight, Princess!');
console.log('Time for a nightcap.');
console.log ('Sleep tight!');
console.log ('Get NAKED!');
} else {
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
}
}
//greeting();
document.write('<h1>' + greeting() + '</h1>');
??

console.log() prints to the console (F12 in normal browsers). You probably want to replace all console.log() with document.write('<yourTag>Message</yourTag>');.
Otherwise, if you want to use the output of the function the you can create an empty string variable and concat your messages and return them.
function functionName() {
let result = '';
result = result + 'my Test';
result = result + ' my Test 2';
return result;
}
Of course you can use structures control structures (if/else) with both ways.

greeting() method does not return anything that's why you are getting undefined
function greeting(){
return 'Say Hello!'
}
Now this will shows you
<h1>Say Hello!</h1>

Related

Last value of my FizzBuzz function returns undefined

I'm trying to run the FizzBuzz problem solving, it seems to be working, however when it reaches the user input number it returns undefined.
Here is my code:
const prompt = require("prompt-sync")();
let userChoice = parseInt(prompt("Please enter a number: "));
function fizzBuzz () {
for (let i=1 ; i <= userChoice; i++){
if (i % 3 === 0 && i % 5 ===0) {
console.log("FizzBuzz");
}
else if (i % 3 === 0){
console.log("Fizz");
}
else if (i % 5 === 0){
console.log("Buzz");
}
else {
console.log(i);
}
}
}
console.log(fizzBuzz(userChoice));
This is the outcome in the console :
You need to actually return a value from your fizzBuzz() function. At the moment, that function is returning nothing, so when you log its return value, you're getting undefined.
function fizzBuzz (userChoice) {
let output = [ ];
for (let i=1 ; i <= userChoice; i++){
if (i % 3 === 0 && i % 5 ===0) {
output.push("FizzBuzz");
}
else if (i % 3 === 0){
output.push("Fizz");
}
else if (i % 5 === 0){
output.push("Buzz");
}
else {
output.push(i);
}
}
return output.join("\n");
}
Alternatively, just don't wrap your actual function call in a console.log, so your last line will simply be:
fizzBuzz(userChoice);

JS between function

I need help with this between function. My clock here is 24 hours.
I always get the "else" option. What is wrong with the code?
// Get time
var today = new Date();
var time = today.getHours();
console.log(time); // 8 o clock right now when I am testing
// Between function
Number.prototype.between = function(first, last) {
return first < last ? this >= first && this <= last : this >= last && this <= first;
};
// Do something
if (time.between(7, 9)) {
console.log("between(7, 9)",time.between(7, 9))
}
if (time.between(9, 15)) {
console.log("between(9, 15)",time.between(9, 15))
}
if (time.between(15, 18)) {
console.log("between(15, 18)",time.between(15, 18))
}
// and so on
else {
console.log("else")
}
The else only connects the last if. Just write else ifs
// Get time
var today = new Date();
var time = today.getHours();
console.log(time); // 8 o clock right now when I am testing
// Between function
Number.prototype.between = function(first, last) {
return first < last ? this >= first && this <= last : this >= last && this <= first;
};
// Do something
if (time.between(7, 9)) {
console.log("between(7, 9)",time.between(7, 9))
}
else if (time.between(9, 15)) {
console.log("between(9, 15)",time.between(9, 15))
}
else if (time.between(15, 18)) {
console.log("between(15, 18)",time.between(15, 18))
}
// and so on
else {
console.log("else")
}

Javascript Student - Question - Grade Calculator

[GIT BASH IMAGE][1]
https://i.stack.imgur.com/Akyjx.png
Below I've tried adding in some number 1-100 inside the function itself for example;
Function grade(85)
I've also tried assigning a variable let = 85.
I guess I'm not understanding what they mean by "Receive a score out of 100".
I need help getting this number plugged into this script here. I have included the gitbash error that I'm receiving which makes me think that it's something that needs to be included with the function.
Thank you
//Grade Calculator
/*
Using the grade function below do the following:
1. Receive a score out of 100
2. Return the corresponding letter grade following this grade scale:
90-100 = A
80-89 = B
70-79 = C
60-69 = D
below 60 = F
*/
function grade(){
if(grade = 100 && grade >=90 )
{
return 'you got an A';
}
if(grade >= 80 && grade <= 89 )
{
return 'you got a B';
}
if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
if(grade >= 60 && grade <= 69 )
{
return 'You got a D';
}
if(grade <= 60 )
{
return 'you got a F';
}
}
[1]: https://i.stack.imgur.com/Akyjx.png
You don't need the condition for max
function grade(score){
if(score >=90 )
{
return 'you got an A';
}
else if(score >= 80)
{
return 'you got a B';
}
else if(score >= 70)
{
return 'you got a C';
}
else if(score >= 60)
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(78));
I believe what you have done is almost correct. They want you to pass the score out of 100 and tell the grade. So after some major tweaks in your code it looks like this.
function grade(score){
if(score == 100 || score >=90 )
{
return 'you got an A';
}
else if(score >= 80 && score <= 89 )
{
return 'you got a B';
}
else if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
else if(score >= 60 && score <= 69 )
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(85));
Change the score in the grade calling function at the bottom [grade(85);] and check for change in the grade.
Hope this solves your question.
You did not defined the function argument. provide an argument to the function . you refereed the function name in the function body that is the problem with your code
function grade(mark){
if(mark = 100 && mark >=90 )
{
return 'you got an A';
}
if(mark >= 80 && mark <= 89 )
{
return 'you got a B';
}
if(mark >= 70 && mark <= 79 )
{
return 'you got a C';
}
if(mark >= 60 && mark <= 69 )
{
return 'You got a D';
}
if(mark <= 60 )
{
return 'you got a F';
}
}

how to count up to a specific number in javascript, fizzbuzz

I'm trying to solve a problem similar to the popular fizzbuzz or pingpong question. I already figured out how to write this portion of the code, however I also need to make a counter in which the user inputs their number and the page gives back 1 to the number they input, as well as the fizzbuzz/popcorn numbers. I think the code is something along the lines of
for (i = 1; i <= 20; i++) {...}
But I don't know how to combine this with my other code
var pingpong = function(number) {
if (number % 15 === 0) {
return 'pingpong';
} else if (number % 5 === 0) {
return 'pong';
} else if (number % 3 === 0) {
return 'ping';
} else {
return false;
}
};
Sorry, I know this is a super beginner question, but I'm just starting out and am having a hard time figuring out how everything works together.
You are close. To read more about JavaScripts loops.
var pingpong = function(number) {
if (number % 15 === 0) {
return 'pingpong';
} else if (number % 5 === 0) {
return 'pong';
} else if (number % 3 === 0) {
return 'ping';
} else {
return false;
}
};
for (var i = 1; i <= 20; i++) {
var num = pingpong(i);
// Just show it on the screen
document.body.innerHTML += i + ': ' + num + '<br />';
}

for loop in javaScript not running

I have this fizzbuzz game I am working on. it works properly with the for loop commented out as in this example, but If I uncomment the for loop only the fizz condition works, and nothing else. I have a pen here: http://codepen.io/lucky500/pen/GJjVEO
//for (i = 1; i < 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
resultBox.innerHTML = "fizzbuzz";
} else if (i % 3 === 0) {
resultBox.innerHTML = "fizz";
} else if (i % 5 === 0) {
resultBox.innerHTML = "buzz";
} else if (i > 100) {
alert("Please enter a number from 1 to 100");
} else {
resultBox.innerHTML = i;
}
// clear input
input.value = " ";
}
//}
You're overwriting the content each time with:
resultBox.innerHTML = ...
You need to concat the results instead:
resultBox.innerHTML += ...
That's why you only see one (the last) output.
See it here: http://codepen.io/anon/pen/mJMKOe
(you need to fix the input though)

Categories