How do I write a if and if-else statement in Angular project? It's only the "else" that is returning, even though the initial conditions are true.
export class HomeComponent implements OnInit {
productWorth: number;
unit: number;
pricePerProduct:any = this.changepricePerProduct();
result: number;
constructor() {}
ngOnInit() {}
changepricePerProduct() {
if (this.productWorth >= 200 && (this.productWorth) <= 2000) {
return 150;
} else if (this.productWorth >= 2001 && (this.productWorth) <= 5000) {
return 450
} else if (this.productWorth >= 5001 && (this.productWorth) <= 10000) {
return 900
} else if (this.productWorth >= 10001 && (this.productWorth) <= 20000) {
return 1200
} else if (this.productWorth >= 20000) {
return 1500
} else {
return 0
}
}
multiply() {
this.result = this.pricePerProduct * this.unit;
}
}
Your code doesn't show any kind of change of the productWorth field. Hence we can just assume, that it keeps being undefined, which will always result into 0.
Please don't mind me giving you a recommendation along. Remove this horror if-else construct. Instead you can do this:
// must be sorted
var myPriceTable = {
200: 150,
2001: 450,
5001: 900,
10001: 1200,
20001: 1500
}
function changepricePerProduct(productWorth) {
if (typeof productWorth !== 'number') return 0;
var floorKey = Object.keys(myPriceTable).findLast(key => key < productWorth);
return floorKey ? myPriceTable[floorKey] : 0;
}
console.log(changepricePerProduct(undefined));
console.log(changepricePerProduct(5));
console.log(changepricePerProduct(222));
console.log(changepricePerProduct(5555));
console.log(changepricePerProduct(22222));
changepricePerProduct() {
return this.productWorth >= 200 && (this.productWorth) <= 2000 ? 150 :
(this.productWorth >= 2001 && (this.productWorth) <= 5000)?450:
(this.productWorth >= 5001 && (this.productWorth) <= 10000)?900:
(this.productWorth >= 10001 && (this.productWorth) <= 20000)?1200:
(this.productWorth >= 20000)?1500:0 }
Related
[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';
}
}
I can't get theright result, "Weird" on stdin, 18 and 20. Everything looks good to me, however something must be off.
if (N % 2 == 1) {
console.log("Weird");
}
else if ((N % 2 == 0) && (2 >= N <= 5)) {
console.log("Not Weird");
}
else if ((N % 2 == 0) && (5 <= N <= 20)) {
console.log("Weird");
}
else if ((N % 2 == 0) && (N > 20)) {
console.log("Not Weird");
}
else {
console.log("Weird");
}
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const N = parseInt(readLine(), 10);
if (N%2==1) {
console.log("Weird");
}
else if ((N % 2 == 0) && (2 >= N <= 5)) {
console.log("Not Weird");
}
else if ((N % 2 == 0) && (5 <= N && N <= 20)) {
console.log("Weird");
}
else if ((N % 2 == 0) && (N > 20)) {
console.log("Not Weird");
}
else{
console.log("Weird");
}
}
I ve added the whole code. In the main function, in the second else if condition, there seems to be the problem. When n is given 18 or 20, I can not get the right output which should be "Weird"
You can't be doing two conditions in the same time
if (5<=N<=20) {}
Will evaluate 5<=N first which produces either true/false which are when compared to numbers will evaluate to (1/0) respectively. Then the second part ( <= 20) will be evaluated.
Combine two conditions only with AND / OR operators
if (5 <= N && N <= 20) {}
This will solve your problem.
I basically just can't figure this one out and don't want to hack it.
It will look so messy if I do this for 52 weeks in a year.
Any tips?
Update
This question is not about getting the year's current week.
This question is about getting weeks elapsed since a date defined.
I want the next week's workout to show up on 6th day of the current week ;)
Example
My days since start are 99: 2018-05-30 18:39:29.
Some of your examples are showing me on week 15.
My code however shows week 16, which is right. See the caveat?
calculateUsersCurrentWorkoutWeek: function(timestamp) {
let daysSinceSignup = moment().diff(timestamp, "days");
if (daysSinceSignup <= 6) {
return 1;
} else if (daysSinceSignup > 6 && daysSinceSignup <= 13) {
return 2;
} else if (daysSinceSignup > 13 && daysSinceSignup <= 20) {
return 3;
} else if (daysSinceSignup > 20 && daysSinceSignup <= 27) {
return 4;
} else if (daysSinceSignup > 27 && daysSinceSignup <= 34) {
return 5;
} else if (daysSinceSignup > 34 && daysSinceSignup <= 41) {
return 6;
} else if (daysSinceSignup > 41 && daysSinceSignup <= 48) {
return 7;
} else if (daysSinceSignup > 48 && daysSinceSignup <= 55) {
return 8;
} else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {
return 9;
} else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {
return 10;
} else if (daysSinceSignup > 62 && daysSinceSignup <= 69) {
return 11;
} else if (daysSinceSignup > 69 && daysSinceSignup <= 76) {
return 12;
} else if (daysSinceSignup > 76 && daysSinceSignup <= 83) {
return 13;
} else if (daysSinceSignup > 83 && daysSinceSignup <= 90) {
return 14;
} else if (daysSinceSignup > 90 && daysSinceSignup <= 97) {
return 15;
} else {
return 16;
}
}
Divide by 7 and ceil?
const daysSinceSignup = moment().diff(timestamp, "days");
return Math.ceil(daysSinceSignup / 7);
Did you miss out on coffee this morning? ;-)
Note: The function above will tell you "the number of the week since timestamp" which is what I understand from your text, but your code sample does not reflect it.
To get the same result of your code sample, you'll need to .floor() and add 1 instead of .ceil():
const daysSinceSignup = moment().diff(timestamp, "days");
return Math.floor(daysSinceSignup / 7) + 1;
As #Michel said, divide by 7 and floor + 1
calculateUsersCurrentWorkoutWeek: function(timestamp) {
let daysSinceSignup = moment().diff(timestamp, "days");
return Math.floor(daysSinceSignup / 7) + 1;
}
What you are looking for is
return Math.ceil((daysSinceStartup + 1) / 7);
This will give the same results as your code for any real number between -1 and 62 (because around 62 you forgot to replace the copied ranges). It returns 1 for 6 days, and 2 for 6.01 days, just as the original code.
The easy way could also be to create a integer array in ascending order so that the values contain the upper limit of the comparison. Then for each values of daysSinceSignup you can find the index and add 1 to it since you require the return values as 1, 2, 3, ... and so on. This will be useful if you do not have uniform interval, like currently the interval is always 7 so the division and ceil approach could also work but if the interval is not uniform then this approach could be really useful.
var indexArray = [6,13,20,27,34,41,48,55,62,69,76,83,90,97];
function calculateUsersCurrentWorkoutWeek() {
let daysSinceSignup = 14;
var elemIndex = indexArray.findIndex((item)=> item > daysSinceSignup);
return elemIndex+1;
}
console.log(calculateUsersCurrentWorkoutWeek());
You can put your datas in array and use array.find:
var datas = [
{ min: -Infinity, max: 6, ret: 1 },
{ min: 6, max: 13, ret: 2 },
{ min: 13, max: 20, ret: 3 },
{ min: 20, max: 27, ret: 4 }
];
function calculateUsersCurrentWorkoutWeek(daysSinceSignup) {
return datas.find(({min, max}) =>
daysSinceSignup > min && daysSinceSignup <= max
).ret;
}
console.log(calculateUsersCurrentWorkoutWeek(5));
console.log(calculateUsersCurrentWorkoutWeek(7));
console.log(calculateUsersCurrentWorkoutWeek(14));
I have this function that is supposed to loop through these additional functions while they won't take the totals up past 100. However, the iteration stops and I need it to finish up with the hunger and danger value either being 100 or 0 depending on which. The numbers that pass through are first: (50,50) second: (0, 100). I can't seem to figure out how to change the addition/subtractions amount when the if condition is no longer met. My consoles are all displaying the last iteration before going over the conditional values.
The problem is:
Create a function called frodo. frodo will take in two parameters:
startingHungerValue (Number) and startingDangerValue (Number).
frodo will need to store those values on internal variables.
frodo will then return an object with two methods:
The first method will be called dinnerOverFire.
dinnerOverFire will decrease hunger by 25 and will increase danger by 40.
The second method will be called hidingInBush.
hidingInBush will increase hunger by 35 and decrease danger by 20.
Both methods need to return an object structured like this:
{
hunger: (modified hunger value),
danger: (modified danger value)
}
NOTE: Neither hunger nor danger should be able to exceed 100 or drop below 0.
function frodo(startingHungerValue, startingDangerValue) {
var shv = startingHungerValue;
var sdv = startingDangerValue;
console.log('startingHungerValue:', shv, 'startingDANGERvalue:', sdv)
return {
dinnerOverFire: () => {
if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv - 25,
danger: sdv + 40
}
}
},
hidingInBush: () => {
if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv + 35,
danger: sdv - 20
}
}
}
}
}
Not directly what you asked for, but I have a feeling you want to do something like below. It creates a class Frodo that you can call methods on like eat or hide.
You can see it in action here: jsfiddle
function Frodo(startingHungerValue, startingDangerValue) {
var self = this; //this could change per scope, self keeps a reference to the basic of the class
//store levels
self.hunger = startingHungerValue;
self.danger = startingDangerValue;
//show levels method
self.showLevels = function(){
console.log('My hungerlevel: '+ self.hunger);
console.log('My dangerlevel: '+ self.danger);
console.log('------');
}
//dinner method
self.dinnerOverFire = function() {
var newHunger = self.hunger - 25;
var newDanger = self.danger + 40;
if (newHunger<0) {
newHunger = 0; //no negatives
}
if (self.hunger==0) {
console.log('I\'m not hungry! No dinner!');
console.log('------');
return;
}
if (newDanger>100) {
console.log('Eating now would kill me! No dinner!');
console.log('------');
return;
}
self.hunger = newHunger;
self.danger = newDanger;
console.log('Thanks for dinner!');
self.showLevels();
}
//hiding method
self.hideInBush = function() {
var newHunger = self.hunger + 35;
var newDanger = self.danger - 20;
if (newDanger<0) {
newDanger = 0; //no negatives
}
if (newHunger>100) {
console.log('Hiding now would kill me! No hiding!');
console.log('------');
return;
}
if (self.danger==0) {
console.log('I\'m not scared at all! No hiding!');
console.log('------');
return;
}
self.hunger = newHunger;
self.danger = newDanger;
console.log('Thanks, I feel safer already!');
self.showLevels();
}
//initial message
console.log('Hi, i\'m frodo!');
self.showLevels();
}
//run your frodo
var frodo = new Frodo(50,50);
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
This would output:
Hi, i'm frodo!
My hungerlevel: 50
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 25
My dangerlevel: 90
------
Thanks, I feel safer already!
My hungerlevel: 60
My dangerlevel: 70
------
Eating now would kill me! No dinner!
------
Thanks, I feel safer already!
My hungerlevel: 95
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 70
My dangerlevel: 90
------
Hiding now would kill me! No hiding!
------
Eating now would kill me! No dinner!
------
Which already shows a problem with the current way. At the end he's to scared to eat and to hungry to hide.
Do you mean, adding an else block to the if condition?
function frodo(startingHungerValue, startingDangerValue) {
var shv = startingHungerValue;
var sdv = startingDangerValue;
console.log('startingHungerValue:', shv, 'startingDANGERvalue:', sdv)
return {
dinnerOverFire: () => {
if ((shv === 50 && shv === 50)){
return {
hunger: shv - 50,
danger: sdv + 50
}
}
else if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv + 100,
danger: sdv - 100
}
}
},
hidingInBush: () => {
if ((shv === 50 && shv === 50)){
return {
hunger: shv - 50,
danger: sdv + 50
}
}
else if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv + 100,
danger: sdv - 100
}
}
}
}
}
console.log(frodo(50, 50).dinnerOverFire()); // 0,100
console.log(frodo(0, 100).dinnerOverFire()); // 100,0
.as-console {
height: 100%;
}
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
function fizzBuzz (start, end) {
for ( var i = start; i <= end; 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);
};
};
};
fizzBuzz (1,10);
Trying to execute FizzBuzz function. I thought it was a syntax issue, maybe I'm overlooking something fundamental?
It is a syntax issue:
function fizzBuzz (start, end) {
for ( var i = start; i <= end; 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);
}
}
}
fizzBuzz(1,10);
This is how it should look. You have unnecessary extra semicolons and wrong quotes, although only the latter truly broke your code.