Mar Mar - 6 months ago 42
AngularJS Question

FizzBuzz in angular?

I am trying to learn Angular and I am trying to make a fizzBuzz however; I don't get result with 'both' conditions:

Here is the app:

var myApp = angular.module("myApp", []);

myApp.controller("game", ["$scope", function($scope) {

$scope.number = 0;
$scope.result = "";

$scope.fizzBuzz = function(){

if($scope.number){

if($scope.number % 3 == 0)
{
return $scope.result = "Fizz";
}
else if($scope.number % 5 == 0)
{
return $scope.result = "Buzz";
}
else if(($scope.number % 3 == 0) && ($scope.number % 5 == 0))
{
return $scope.result = "FizzBuzz";
alert($scope.result);
}
}else{
return "Enter a Number";
}

};

}]);


and binding:

<div ng-controller="game">

<input type="text" ng-model="number">

<h1>{{fizzBuzz(result)}}</h1>

</div>


It can't get 'FizzBuzz'but 'Fizz' and 'Buzz' ..

That works with plain js good. I just can't see if this is a sytax error..?

Live: http://jsfiddle.net/huguxvmx/1/

Thnx in advance!

Answer

You should set up this condition

if(($scope.number % 3 == 0) && ($scope.number % 5 == 0))

on the first place and replace & on &&.

This is working JSFiddle.